From 57d60931280337392f94227df43a7b622bf4976d Mon Sep 17 00:00:00 2001 From: Avinash Kumar Date: Wed, 24 Jan 2024 05:53:37 +0530 Subject: [PATCH] Fix parseInternalLinks helper issues (#2338) * 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 --- src/TwillUtil.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) 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(); }