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 3 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 @@
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 @@

$items = Collection::make(array_values($sortedRelatedItems))->filter(function ($value) {
return is_object($value);
})->map(function ($relatedElement) use ($relation) {
})->map(function ($relatedElement) {

Check warning on line 468 in src/Repositories/Behaviors/HandleBlocks.php

View check run for this annotation

Codecov / codecov/patch

src/Repositories/Behaviors/HandleBlocks.php#L468

Added line #L468 was not covered by tests
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),

Check warning on line 472 in src/Repositories/Behaviors/HandleBlocks.php

View check run for this annotation

Codecov / codecov/patch

src/Repositories/Behaviors/HandleBlocks.php#L472

Added line #L472 was not covered by tests
] + (classHasTrait($relatedElement, HasMedias::class) ? [
'thumbnail' => $relatedElement->defaultCmsImage(['w' => 100, 'h' => 100]),
] : []);
Expand Down
25 changes: 22 additions & 3 deletions src/Repositories/Behaviors/HandleBrowsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,16 +230,35 @@
'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): mixed
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public function getAdminEditUrl($object): mixed
public function getAdminEditUrl($object, ?string $routePrefix): mixed

{
if (!empty($object->adminEditUrl)) {
return $object->adminEditUrl;

Check warning on line 249 in src/Repositories/Behaviors/HandleBrowsers.php

View check run for this annotation

Codecov / codecov/patch

src/Repositories/Behaviors/HandleBrowsers.php#L249

Added line #L249 was not covered by tests
}

$module = getModuleNameByModel($object);

return moduleRoute(
$module,
config('twill.block_editor.browser_route_prefixes.' . $module),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
config('twill.block_editor.browser_route_prefixes.' . $module),
$routePrefix ?? config('twill.block_editor.browser_route_prefixes.' . $module),

'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