diff --git a/src/TwillUtil.php b/src/TwillUtil.php index aa45ffb5d..cb93efe35 100644 --- a/src/TwillUtil.php +++ b/src/TwillUtil.php @@ -3,6 +3,7 @@ namespace A17\Twill; use A17\Twill\Models\Contracts\TwillLinkableModel; +use Illuminate\Database\Eloquent\Relations\Relation; use Illuminate\Support\Facades\Session; /** @@ -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(); }