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 d453bde92..507e50c2f 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); @@ -484,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 1576a2af1..1a1faf44c 100644 --- a/src/Repositories/Behaviors/HandleBrowsers.php +++ b/src/Repositories/Behaviors/HandleBrowsers.php @@ -188,25 +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' => $relatedElement->adminEditUrl ?? moduleRoute( - $moduleName ?? $relation, - $routePrefix ?? '', - 'edit', - $relatedElement->id - ), + 'edit' => $this->getAdminEditUrl($relatedElement, $routePrefix, $moduleName), 'endpointType' => $relatedElement->getMorphClass(), ] + (classHasTrait($relatedElement, HasMedias::class) ? [ 'thumbnail' => $relatedElement->defaultCmsImage(['w' => 100, 'h' => 100, 'fit' => 'crop']), @@ -230,9 +218,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 +227,26 @@ public function getFormFieldsForRelatedBrowser($object, $relation, $titleKey = ' })->values()->toArray(); } + /** + * @param $object + * @return mixed|string + */ + public function getAdminEditUrl($object, $routePrefix = null, $moduleName = null): mixed + { + if (!empty($object->adminEditUrl)) { + return $object->adminEditUrl; + } + + $moduleName = $moduleName ?? getModuleNameByModel($object); + + return moduleRoute( + $moduleName, + $routePrefix ?? config('twill.block_editor.browser_route_prefixes.' . $moduleName), + 'edit', + $object->id + ); + } + /** * Get all browser' detail info from the $browsers attribute. * The missing information will be inferred by convention of Twill.