Skip to content

Commit

Permalink
Fix parseInternalLinks helper issues (#2338)
Browse files Browse the repository at this point in the history
* parseInternalLinks corrections for double digit ids

* multiple # presence handling in single block

* Support models with a defined morphmaps in WYSIWYG link browser rendering helper

---------

Co-authored-by: Quentin Renard <[email protected]>
  • Loading branch information
avinash403 and ifox authored Jan 24, 2024
1 parent 3713e9e commit 57d6093
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/TwillUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace A17\Twill;

use A17\Twill\Models\Contracts\TwillLinkableModel;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Facades\Session;

/**
Expand Down Expand Up @@ -57,13 +58,22 @@ public function clearTempStore(): void
public function parseInternalLinks(string $content): string
{
return preg_replace_callback(
'/(#twillInternalLink::(.*)#(\d))/',
'/(#twillInternalLink::(.*?)#(\d+))/',
function (array $data) {
if (isset($data[2], $data[3])) {
$modelClass = $data[2];
$id = $data[3];

if (array_key_exists($modelClass, Relation::morphMap())) {
$modelClass = Relation::morphMap()[$modelClass];
}

$model = $modelClass::published()->where('id', $id)->first();

if (!$model) {
return url('404');
}

if ($model instanceof TwillLinkableModel) {
return $model->getFullUrl();
}
Expand Down

0 comments on commit 57d6093

Please sign in to comment.