Skip to content

Commit

Permalink
AdminAction improvements - allow set route pattern and params. Admin …
Browse files Browse the repository at this point in the history
…test helper trait
  • Loading branch information
DumitracheAdrian committed Oct 3, 2024
1 parent 82dffeb commit 9e8a519
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 10 deletions.
2 changes: 1 addition & 1 deletion app/src/Sonata/Admin/UserAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ protected function configureFormFields(FormMapper $form): void
public function getActions(): array
{
return [
'makeAdin' => (new AdminAction('makeAdmin', true))
'makeAdmin' => (new AdminAction('makeAdmin', true))
->setController(MakeAdminAction::class)
->setIcon('fa fa-user-plus')
->setBatchController(MakeAdminAction::class),
Expand Down
36 changes: 36 additions & 0 deletions packages/sonata-extra-bundle/ActionableAdmin/AdminAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Draw\Bundle\SonataExtraBundle\ActionableAdmin;

use Sonata\AdminBundle\Admin\AdminInterface;
use Symfony\Component\DependencyInjection\Attribute\Exclude;
use Symfony\Component\String\UnicodeString;

Expand All @@ -22,6 +23,10 @@ class AdminAction

private string $urlSuffix;

private ?string $routePattern = null;

private array $routeParams = [];

private string $access;

private string|false|null $label;
Expand Down Expand Up @@ -80,6 +85,30 @@ public function setUrlSuffix(string $urlSuffix): self
return $this;
}

public function getRoutePattern(): ?string
{
return $this->routePattern;
}

public function setRoutePattern(?string $routePattern): self
{
$this->routePattern = $routePattern;

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 @@ -197,4 +226,11 @@ public function isForAction(string $action): bool
{
return $this->forActions[$action] ?? $this->forActions['_default'];
}

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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Draw\Bundle\SonataExtraBundle\ActionableAdmin\Extension;

use Draw\Bundle\SonataExtraBundle\ActionableAdmin\AdminAction;
use Draw\Bundle\SonataExtraBundle\ActionableAdmin\AdminActionLoader;
use Sonata\AdminBundle\Admin\AbstractAdminExtension;
use Sonata\AdminBundle\Admin\AdminInterface;
Expand Down Expand Up @@ -36,14 +37,10 @@ public function configureRoutes(AdminInterface $admin, RouteCollectionInterface
$defaults['_controller'] = $action->getController();
}

$pattern = $action->getTargetEntity()
? $admin->getRouterIdParameter().'/'.$action->getUrlSuffix()
: $action->getUrlSuffix();

$collection
->add(
$action->getName(),
$pattern,
$this->getRoutePattern($admin, $action),
defaults: $defaults
)
;
Expand Down Expand Up @@ -144,4 +141,15 @@ public function configureActionButtons(

return $list;
}

private function getRoutePattern(AdminInterface $admin, AdminAction $action): string
{
if (null !== $pattern = $action->getRoutePattern()) {
return $pattern;
}

return $action->getTargetEntity()
? \sprintf('%s/%s', $admin->getRouterIdParameter(), $action->getUrlSuffix())
: $action->getUrlSuffix();
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<li>
<a class="sonata-action-element" href="{{ object is defined ? admin.generateObjectUrl(item.action.name, object) : admin.generateUrl(item.action.name) }}">
<a class="sonata-action-element" href="{{ item.action.generateUrl(admin, object) }}">
{% if item.action.icon %}
<i class="{{ item.action.icon }}" aria-hidden="true"></i>
{% endif %}
{{ item.action|translate_label }}
</a>
</li>
</li>
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{% if admin.hasAccess(actions.action.name, object) %}
<a class="btn btn-sm btn-default" href="{{ admin.generateObjectUrl(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>
{% endif %}
{% endif %}
96 changes: 96 additions & 0 deletions packages/sonata-extra-bundle/Test/AdminTestHelperTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

declare(strict_types=1);

namespace Draw\Bundle\SonataExtraBundle\Test;

use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\DomCrawler\Link;

trait AdminTestHelperTrait
{
protected function selectListObjectLink(
Crawler $crawler,
int|string $identifier,
string $action,
): Link {
return $crawler
->filter(\sprintf('.sonata-ba-list-field-actions[objectid="%s"]', $identifier))
->selectLink($action)
->link()
;
}

protected function selectMenuObjectLink(
Crawler $crawler,
string $action,
): Link {
return $crawler
->filter('ul.dropdown-menu')
->selectLink($action)
->link()
;
}

protected function submitBatchAction(
KernelBrowser $client,
array $indexes,
string $action,
string $button = 'OK',
): void {
$crawler = $client->submitForm(
$button,
[
'idx' => $indexes,
'action' => $action,
]
);

$client->submit(
$crawler->selectButton('Yes, execute')->form()
);
}

protected function appendDefaultFiltersToListUrl(string $listUrl): string
{
return \sprintf(
'%s%s%s',
$listUrl,
null === parse_url($listUrl, \PHP_URL_QUERY) ? '?' : '&',
http_build_query(
[
'filter' => [
'_page' => 1,
'_per_page' => 25,
],
]
)
);
}

protected function filterListByIdentifier(
KernelBrowser $client,
string $listUrl,
int|string $identifier,
string $identifierName = 'id',
): Crawler {
return $client->request(
'GET',
\sprintf(
'%s?%s',
$listUrl,
http_build_query(
[
'filter' => [
$identifierName => [
'type' => 3,
'value' => $identifier,
],
],
]
)
)
);
}
}
1 change: 1 addition & 0 deletions packages/sonata-extra-bundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"require": {
"php": ">=8.2",
"draw/dependency-injection": "^0.12",
"symfony/browser-kit": "^6.4.0",
"symfony/framework-bundle": "^6.4.0",
"symfony/expression-language": "^6.4.0",
"symfony/string": "^6.4.0"
Expand Down

0 comments on commit 9e8a519

Please sign in to comment.