Skip to content

Commit

Permalink
Rewroke route parameters to have unique route
Browse files Browse the repository at this point in the history
  • Loading branch information
mpoiriert committed Oct 15, 2024
1 parent d3cf270 commit 3f926b0
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 37 deletions.
28 changes: 16 additions & 12 deletions app/src/Sonata/Admin/UserAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,21 +222,25 @@ public function getActions(): array
'addRoles' => (new AdminAction('addRoles', true))
->setController(AddRolesAminAction::class)
->setBatchController(AddRolesAminAction::class),
] + iterator_to_array($this->prepareSetPreferredLocaleActions());
}

private function prepareSetPreferredLocaleActions(): iterable
{
foreach (['en', 'fr'] as $locale) {
yield $name = \sprintf('setPreferredLocale%s', ucfirst($locale)) => (new AdminAction($name, true))
->setLabel(strtoupper($locale))
'setPreferredLocale' => (new AdminAction('setPreferredLocale', true))
->setIcon('fa fa-language')
->setController(SetPreferredLocaleAction::class)
->setRoutePattern(
\sprintf('%s/preferred-locale/{_locale}', $this->getRouterIdParameter())
\sprintf(
'%s/preferred-locale/{_locale}',
$this->getRouterIdParameter(),
)
)
->addRouteParam('_locale', $locale)
;
}
->setActionsCallback(
static function (AdminAction $action): iterable {
foreach (['en', 'fr'] as $locale) {
yield (clone $action)
->setLabel(strtoupper($locale))
->setRouteParameters(['_locale' => $locale])
;
}
}
),
];
}
}
54 changes: 39 additions & 15 deletions packages/sonata-extra-bundle/ActionableAdmin/AdminAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,18 @@ class AdminAction

private ?string $routePattern = null;

private array $routeParams = [];
private array $routeParameters = [];

private string $access;

private string|false|null $label;
private string|false|null $translationDomain = null;

/**
* @var callable
*/
private $actionsCallback;

public function __construct(
private string $name,
private bool $targetEntity,
Expand All @@ -49,6 +54,8 @@ public function __construct(
->replace('_', '-')
->toString()
;

$this->actionsCallback = fn () => [$this];
}

public function getName(): string
Expand Down Expand Up @@ -97,18 +104,6 @@ public function setRoutePattern(?string $routePattern): self
return $this;
}

public function getRouteParams(): array
{
return $this->routeParams;
}

public function addRouteParam(string $key, mixed $value): self
{
$this->routeParams[$key] = $value;

return $this;
}

public function getLabel(): bool|string|null
{
return $this->label;
Expand Down Expand Up @@ -227,10 +222,39 @@ public function isForAction(string $action): bool
return $this->forActions[$action] ?? $this->forActions['_default'];
}

public function getActions(): iterable
{
return \call_user_func($this->actionsCallback, $this);
}

public function getActionsCallback(): callable
{
return $this->actionsCallback;
}

public function setActionsCallback(callable $actionsCallback): self
{
$this->actionsCallback = $actionsCallback;

return $this;
}

public function getRouteParameters(): array
{
return $this->routeParameters;
}

public function setRouteParameters(array $routeParameters): self
{
$this->routeParameters = $routeParameters;

return $this;
}

public function generateUrl(AdminInterface $admin, mixed $subject = null): string
{
return null !== $subject
? $admin->generateObjectUrl($this->name, $subject, $this->routeParams)
: $admin->generateUrl($this->name, $this->routeParams);
? $admin->generateObjectUrl($this->name, $subject, $this->routeParameters)
: $admin->generateUrl($this->name, $this->routeParameters);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
{% for action in item.action.actions %}
<li>
<a class="sonata-action-element" href="{{ item.action.generateUrl(admin, object) }}">
{% if item.action.icon %}
<i class="{{ item.action.icon }}" aria-hidden="true"></i>
<a class="sonata-action-element" href="{{ action.generateUrl(admin, object) }}">
{% if action.icon %}
<i class="{{ action.icon }}" aria-hidden="true"></i>
{% endif %}
{{ item.action|translate_label }}
{{ action|translate_label }}
</a>
</li>
{% endfor %}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
{% if admin.hasAccess(actions.action.name, object) %}
<a class="btn btn-sm btn-default" href="{{ actions.action.generateUrl(admin, object) }}">
{% if actions.action.icon %}
<i class="{{ actions.action.icon }}" aria-hidden="true"></i>
{% endif %}
{{ actions.action|translate_label }}
</a>
{% for action in actions.action.actions %}
<a class="btn btn-sm btn-default" href="{{ action.generateUrl(admin, object) }}">
{% if action.icon %}
<i class="{{ action.icon }}" aria-hidden="true"></i>
{% endif %}
{{ action|translate_label }}
</a>
{% endfor %}
{% endif %}

0 comments on commit 3f926b0

Please sign in to comment.