Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: edit url for block browser items #2533

Open
wants to merge 7 commits into
base: 3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/Helpers/modules_helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}
}
}

Expand Down
28 changes: 2 additions & 26 deletions src/Repositories/Behaviors/HandleBlocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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]),
] : []);
Expand Down
41 changes: 24 additions & 17 deletions src/Repositories/Behaviors/HandleBrowsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']),
Expand All @@ -230,16 +218,35 @@ 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) {
return empty($item);
})->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.
Expand Down
Loading