From d368b42837ce5580507e722275c7a4eda01e3ca6 Mon Sep 17 00:00:00 2001 From: Olayinka Azeez Date: Mon, 11 Mar 2024 21:02:51 +0100 Subject: [PATCH 1/5] use related type of block relations to form browser edit url --- src/Repositories/Behaviors/HandleBlocks.php | 19 ------------- src/Repositories/Behaviors/HandleBrowsers.php | 28 +++++++++++++++++-- 2 files changed, 25 insertions(+), 22 deletions(-) diff --git a/src/Repositories/Behaviors/HandleBlocks.php b/src/Repositories/Behaviors/HandleBlocks.php index d453bde92..965d8b786 100644 --- a/src/Repositories/Behaviors/HandleBlocks.php +++ b/src/Repositories/Behaviors/HandleBlocks.php @@ -450,25 +450,6 @@ protected function getBlockBrowsers($block) return Collection::make($block['content']['browsers'])->mapWithKeys(function ($ids, $relation) use ($block) { if ($this->hasRelatedTable() && $block->getRelated($relation)->isNotEmpty()) { $items = $this->getFormFieldsForRelatedBrowser($block, $relation); - foreach ($items as &$item) { - if (!isset($item['edit'])) { - try { - $item['edit'] = moduleRoute( - $relation, - config('twill.block_editor.browser_route_prefixes.' . $relation), - 'edit', - $item['id'] - ); - } catch (RouteNotFoundException $e) { - report($e); - Log::notice( - "Twill warning: The url for the \"{$relation}\" browser items can't " . - "be resolved. You might be missing a {$relation} key in your " . - 'twill.block_editor.browser_route_prefixes configuration.' - ); - } - } - } } else { try { $relationRepository = $this->getModelRepository($relation); diff --git a/src/Repositories/Behaviors/HandleBrowsers.php b/src/Repositories/Behaviors/HandleBrowsers.php index 1576a2af1..d081cb57e 100644 --- a/src/Repositories/Behaviors/HandleBrowsers.php +++ b/src/Repositories/Behaviors/HandleBrowsers.php @@ -230,9 +230,8 @@ public function getFormFieldsForRelatedBrowser($object, $relation, $titleKey = ' 'id' => $relatedElement->id, 'name' => $relatedElement->titleInBrowser ?? $relatedElement->$titleKey, 'endpointType' => $relatedElement->getMorphClass(), - ] + (empty($relatedElement->adminEditUrl) ? [] : [ - 'edit' => $relatedElement->adminEditUrl, - ]) + (classHasTrait($relatedElement, HasMedias::class) ? [ + 'edit' => $this->getAdminEditUrl($relatedElement), + ] + (classHasTrait($relatedElement, HasMedias::class) ? [ 'thumbnail' => $relatedElement->defaultCmsImage(['w' => 100, 'h' => 100, 'fit' => 'crop']), ] : []) : []; })->reject(function ($item) { @@ -240,6 +239,29 @@ public function getFormFieldsForRelatedBrowser($object, $relation, $titleKey = ' })->values()->toArray(); } + /** + * @param $object + * @return mixed|string + */ + private function getAdminEditUrl($object): mixed + { + if (!empty($object->adminEditUrl)) { + return $object->adminEditUrl; + } + + $relatedType = $object->getRelation('pivot')->related_type; + $relation = strpos($relatedType, '\\') + ? getModuleNameByModel($relatedType) + : $relatedType; + + return moduleRoute( + $relation, + config('twill.block_editor.browser_route_prefixes.' . $relation), + 'edit', + $object->id + ); + } + /** * Get all browser' detail info from the $browsers attribute. * The missing information will be inferred by convention of Twill. From d0cc397ba3b525e9f7f758ac02329e3d00e62e76 Mon Sep 17 00:00:00 2001 From: Olayinka Azeez Date: Tue, 12 Mar 2024 10:12:43 +0100 Subject: [PATCH 2/5] fix failing tests --- src/Repositories/Behaviors/HandleBrowsers.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Repositories/Behaviors/HandleBrowsers.php b/src/Repositories/Behaviors/HandleBrowsers.php index d081cb57e..c8acb319b 100644 --- a/src/Repositories/Behaviors/HandleBrowsers.php +++ b/src/Repositories/Behaviors/HandleBrowsers.php @@ -250,7 +250,7 @@ private function getAdminEditUrl($object): mixed } $relatedType = $object->getRelation('pivot')->related_type; - $relation = strpos($relatedType, '\\') + $relation = str_contains($relatedType, '\\') ? getModuleNameByModel($relatedType) : $relatedType; From dc2e5cad3aae5fe018da8383d5a7465fc91e03f1 Mon Sep 17 00:00:00 2001 From: Olayinka Azeez Date: Fri, 22 Mar 2024 18:14:59 +0100 Subject: [PATCH 3/5] refactored in the case module has different name from model --- src/Helpers/modules_helpers.php | 6 +++++- src/Repositories/Behaviors/HandleBlocks.php | 9 ++------- src/Repositories/Behaviors/HandleBrowsers.php | 11 ++++------- 3 files changed, 11 insertions(+), 15 deletions(-) diff --git a/src/Helpers/modules_helpers.php b/src/Helpers/modules_helpers.php index a3f119bf7..254852d36 100644 --- a/src/Helpers/modules_helpers.php +++ b/src/Helpers/modules_helpers.php @@ -51,7 +51,11 @@ function getModelByModuleName($moduleName) if (!function_exists('getModuleNameByModel')) { function getModuleNameByModel($model) { - return Str::plural(lcfirst(class_basename($model))); + try { + return TwillCapsules::getCapsuleForModel($model)->getModule(); + } catch (NoCapsuleFoundException) { + return Str::plural(lcfirst(class_basename($model))); + } } } diff --git a/src/Repositories/Behaviors/HandleBlocks.php b/src/Repositories/Behaviors/HandleBlocks.php index 965d8b786..507e50c2f 100644 --- a/src/Repositories/Behaviors/HandleBlocks.php +++ b/src/Repositories/Behaviors/HandleBlocks.php @@ -465,16 +465,11 @@ protected function getBlockBrowsers($block) $items = Collection::make(array_values($sortedRelatedItems))->filter(function ($value) { return is_object($value); - })->map(function ($relatedElement) use ($relation) { + })->map(function ($relatedElement) { return [ 'id' => $relatedElement->id, 'name' => $relatedElement->titleInBrowser ?? $relatedElement->title, - 'edit' => moduleRoute( - $relation, - config('twill.block_editor.browser_route_prefixes.' . $relation), - 'edit', - $relatedElement->id - ), + 'edit' => $this->getAdminEditUrl($relatedElement), ] + (classHasTrait($relatedElement, HasMedias::class) ? [ 'thumbnail' => $relatedElement->defaultCmsImage(['w' => 100, 'h' => 100]), ] : []); diff --git a/src/Repositories/Behaviors/HandleBrowsers.php b/src/Repositories/Behaviors/HandleBrowsers.php index c8acb319b..d5a1583ab 100644 --- a/src/Repositories/Behaviors/HandleBrowsers.php +++ b/src/Repositories/Behaviors/HandleBrowsers.php @@ -243,20 +243,17 @@ public function getFormFieldsForRelatedBrowser($object, $relation, $titleKey = ' * @param $object * @return mixed|string */ - private function getAdminEditUrl($object): mixed + public function getAdminEditUrl($object): mixed { if (!empty($object->adminEditUrl)) { return $object->adminEditUrl; } - $relatedType = $object->getRelation('pivot')->related_type; - $relation = str_contains($relatedType, '\\') - ? getModuleNameByModel($relatedType) - : $relatedType; + $module = getModuleNameByModel($object); return moduleRoute( - $relation, - config('twill.block_editor.browser_route_prefixes.' . $relation), + $module, + config('twill.block_editor.browser_route_prefixes.' . $module), 'edit', $object->id ); From a2c5df608f933690edfb7f4b7d4eefedce654211 Mon Sep 17 00:00:00 2001 From: Olayinka Azeez Date: Fri, 22 Mar 2024 18:36:51 +0100 Subject: [PATCH 4/5] fixed edit URL for getFormFieldsForBrowser --- src/Repositories/Behaviors/HandleBrowsers.php | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/Repositories/Behaviors/HandleBrowsers.php b/src/Repositories/Behaviors/HandleBrowsers.php index d5a1583ab..455c50f76 100644 --- a/src/Repositories/Behaviors/HandleBrowsers.php +++ b/src/Repositories/Behaviors/HandleBrowsers.php @@ -201,12 +201,7 @@ function ($relatedElement) use ($titleKey, $routePrefix, $relation, $moduleName, return [ 'id' => $relatedElement->id, 'name' => $relatedElement->titleInBrowser ?? $relatedElement->$titleKey, - 'edit' => $relatedElement->adminEditUrl ?? moduleRoute( - $moduleName ?? $relation, - $routePrefix ?? '', - 'edit', - $relatedElement->id - ), + 'edit' => $this->getAdminEditUrl($relatedElement), 'endpointType' => $relatedElement->getMorphClass(), ] + (classHasTrait($relatedElement, HasMedias::class) ? [ 'thumbnail' => $relatedElement->defaultCmsImage(['w' => 100, 'h' => 100, 'fit' => 'crop']), From 0ad05106e3190f0c1ca9a159dcae0f2df382b9ae Mon Sep 17 00:00:00 2001 From: Olayinka Azeez Date: Mon, 25 Mar 2024 12:28:01 +0100 Subject: [PATCH 5/5] fixed edit URL for getFormFieldsForBrowser --- src/Repositories/Behaviors/HandleBrowsers.php | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/src/Repositories/Behaviors/HandleBrowsers.php b/src/Repositories/Behaviors/HandleBrowsers.php index 455c50f76..1a1faf44c 100644 --- a/src/Repositories/Behaviors/HandleBrowsers.php +++ b/src/Repositories/Behaviors/HandleBrowsers.php @@ -188,20 +188,13 @@ public function getFormFieldsForBrowser( ) { $fields = $this->getRelatedElementsAsCollection($object, $relation); - $isMorphTo = method_exists($object, $relation) && $object->$relation() instanceof MorphTo; - if ($fields->isNotEmpty()) { return $fields->map( - function ($relatedElement) use ($titleKey, $routePrefix, $relation, $moduleName, $isMorphTo) { - if ($isMorphTo && !$moduleName) { - // @todo: Maybe there is an existing helper for this? - $moduleName = Str::plural(Arr::last(explode('\\', get_class($relatedElement)))); - } - + function ($relatedElement) use ($titleKey, $routePrefix, $moduleName) { return [ 'id' => $relatedElement->id, 'name' => $relatedElement->titleInBrowser ?? $relatedElement->$titleKey, - 'edit' => $this->getAdminEditUrl($relatedElement), + 'edit' => $this->getAdminEditUrl($relatedElement, $routePrefix, $moduleName), 'endpointType' => $relatedElement->getMorphClass(), ] + (classHasTrait($relatedElement, HasMedias::class) ? [ 'thumbnail' => $relatedElement->defaultCmsImage(['w' => 100, 'h' => 100, 'fit' => 'crop']), @@ -238,17 +231,17 @@ public function getFormFieldsForRelatedBrowser($object, $relation, $titleKey = ' * @param $object * @return mixed|string */ - public function getAdminEditUrl($object): mixed + public function getAdminEditUrl($object, $routePrefix = null, $moduleName = null): mixed { if (!empty($object->adminEditUrl)) { return $object->adminEditUrl; } - $module = getModuleNameByModel($object); + $moduleName = $moduleName ?? getModuleNameByModel($object); return moduleRoute( - $module, - config('twill.block_editor.browser_route_prefixes.' . $module), + $moduleName, + $routePrefix ?? config('twill.block_editor.browser_route_prefixes.' . $moduleName), 'edit', $object->id );