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

[Feature Request] link() helper on CrudColumn #5317

Merged
merged 14 commits into from
Oct 31, 2023
1 change: 1 addition & 0 deletions src/app/Library/CrudPanel/CrudColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
* @method self priority(int $value)
* @method self key(string $value)
* @method self upload(bool $value)
* @method self linkTo(string $routeName, ?string $target = null)
tabacitu marked this conversation as resolved.
Show resolved Hide resolved
*/
class CrudColumn
{
Expand Down
5 changes: 1 addition & 4 deletions src/app/Library/CrudPanel/Traits/Relationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -360,11 +360,8 @@ private function modelMethodIsRelationship($model, $method)
/**
* Check if it's possible that attribute is in the relation string when
* the last part of the string is not a method on the chained relations.
*
* @param array $field
* @return bool
*/
private function isAttributeInRelationString($field)
public function isAttributeInRelationString(array $field): bool
{
if (! str_contains($field['entity'], '.')) {
return false;
Expand Down
43 changes: 43 additions & 0 deletions src/macros.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,49 @@
});
}

if (! CrudColumn::hasMacro('linkTo')) {
CrudColumn::macro('linkTo', function (string|array $routeNameOrConfiguration, ?string $target = null): static {
pxpm marked this conversation as resolved.
Show resolved Hide resolved
if (is_array($routeNameOrConfiguration)) {
$routeName = $routeNameOrConfiguration['routeName'] ?? null;
$target = $routeNameOrConfiguration['target'] ?? $target;
} else {
$routeName = $routeNameOrConfiguration;
}
pxpm marked this conversation as resolved.
Show resolved Hide resolved

$route = Route::getRoutes()->getByName($routeName);

if (! $route) {
throw new \Exception("Route [{$routeName}] not found while building the link for column [{$this->name}].");
}

$parameters = $route->parameterNames();

if (count($parameters) > 1) {
throw new \Exception("Route {$routeName} requires multiple parameters. Please define the wrapper link manually.");
}

$wrapper = $this->attributes['wrapper'] ?? [];
$wrapper['target'] ??= $target;

$wrapper['href'] = function ($crud, $column, $entry, $related_key) use ($routeName, $parameters) {
if (count($parameters) === 1) {
$entity = $crud->isAttributeInRelationString($column) ? Str::before($column['entity'], '.') : $column['entity'];
$parameterValue = $related_key ?? $entry->{$entity}?->getKey();
if (! $parameterValue) {
return null;
}

return route($routeName, [$parameters[0] => $parameterValue]);
}

return route($routeName);
};
$this->wrapper($wrapper);

return $this;
});
}

/**
* The route macro allows developers to generate the routes for a CrudController,
* for all operations, using a simple syntax: Route::crud().
Expand Down