Skip to content

Commit

Permalink
[SonataExtraBundle] DefaultActionExtension registered manually
Browse files Browse the repository at this point in the history
  • Loading branch information
mpoiriert committed Aug 10, 2024
1 parent 0cc23b9 commit 7f647dd
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
9 changes: 9 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ services:
Draw\Component\Tester\Command\GenerateTraitCommand: ~
Draw\Component\Tester\Command\GenerateAssertsDocumentationPageCommand: ~

# Sonata Admin services >>>
Draw\Bundle\SonataExtraBundle\ActionableAdmin\Extension\DefaultActionExtension:
class: 'Draw\Bundle\SonataExtraBundle\ActionableAdmin\Extension\DefaultActionExtension'
arguments:
$actions: ['delete']
tags:
- { name: sonata.admin.extension, global: true }
# Sonata Admin services <<<

App\:
resource: '../app/src'
exclude:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,37 @@

use Draw\Bundle\SonataExtraBundle\ActionableAdmin\AdminAction\DeleteAdminAction;
use Sonata\AdminBundle\Admin\AbstractAdminExtension;
use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;

#[AutoconfigureTag('sonata.admin.extension', attributes: ['global' => true])]
class DefaultActionExtension extends AbstractAdminExtension implements ActionableAdminExtensionInterface
{
private const SUPPORTED_ACTIONS = [
'delete' => DeleteAdminAction::class,
];

private array $actions;

public function __construct(
array $actions = []
) {
$this->actions = array_values(
array_intersect(
$actions,
array_keys(self::SUPPORTED_ACTIONS)
)
);
}

public function getActions(array $actions): array
{
if (\array_key_exists('delete', $actions)) {
return $actions;
}
foreach ($this->actions as $action) {
if (\array_key_exists($action, $actions)) {
continue;
}

$actionClass = self::SUPPORTED_ACTIONS[$action];

$actions['delete'] = new DeleteAdminAction();
$actions[$action] = new $actionClass();
}

return $actions;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ execution.notification.skipped.reason:
error: 'error'

execution.action.delete:
label: 'Delete'
label: 'Delete'

0 comments on commit 7f647dd

Please sign in to comment.