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 2 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
19 changes: 0 additions & 19 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 Down
28 changes: 25 additions & 3 deletions src/Repositories/Behaviors/HandleBrowsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,16 +230,38 @@
'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
*/
private function getAdminEditUrl($object): 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
}

$relatedType = $object->getRelation('pivot')->related_type;
$relation = str_contains($relatedType, '\\')
? getModuleNameByModel($relatedType)
: $relatedType;

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

View check run for this annotation

Codecov / codecov/patch

src/Repositories/Behaviors/HandleBrowsers.php#L255

Added line #L255 was not covered by tests

return moduleRoute(
$relation,
config('twill.block_editor.browser_route_prefixes.' . $relation),
'edit',
$object->id
);
}
Copy link
Contributor

@Tofandel Tofandel Mar 21, 2024

Choose a reason for hiding this comment

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

Suggested change
$relatedType = $object->getRelation('pivot')->related_type;
$relation = str_contains($relatedType, '\\')
? getModuleNameByModel($relatedType)
: $relatedType;
return moduleRoute(
$relation,
config('twill.block_editor.browser_route_prefixes.' . $relation),
'edit',
$object->id
);
}
$module = getModuleNameByModel($object);
return moduleRoute(
$module,
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