From e15501f604bd677a74126810aa98ef5896dfef5a Mon Sep 17 00:00:00 2001 From: Mathieu Wambre Date: Tue, 2 May 2023 15:33:22 +0200 Subject: [PATCH] [Fix][#10] Allow multiple attributes --- README.md | 2 +- src/Annotation/Sonata/Access.php | 2 +- src/Annotation/Sonata/ActionButton.php | 2 +- src/Annotation/Sonata/AddChild.php | 2 +- src/Annotation/Sonata/AddRoute.php | 2 +- src/Annotation/Sonata/DashboardAction.php | 2 +- src/Annotation/Sonata/ListAction.php | 2 +- src/Annotation/Sonata/RemoveRoute.php | 2 +- tests/Reader/ListReaderTest.php | 52 +++++++++++++++++++- tests/Resources/Model/Entity/EmptyEntity.php | 2 +- 10 files changed, 59 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 9a11b14..12327e7 100644 --- a/README.md +++ b/README.md @@ -886,7 +886,7 @@ class Category ### ActionButton -This will add button next to your add button in a list view. [Here](https://sonata-project.org/bundles/admin/3-x/doc/cookbook/recipe_custom_action.html#custom-action-without-entity) +This will add button next to your add button in a list view. [Here](https://docs.sonata-project.org/projects/SonataAdminBundle/en/4.x/reference/dashboard/#configuring-what-actions-are-available-for-each-item-on-the-dashboard) you can find how the template should look like. ```php diff --git a/src/Annotation/Sonata/Access.php b/src/Annotation/Sonata/Access.php index dacf94b..3514ad2 100644 --- a/src/Annotation/Sonata/Access.php +++ b/src/Annotation/Sonata/Access.php @@ -19,7 +19,7 @@ * @author Marko Kunic * @author Mathieu Wambre */ -#[Attribute(Attribute::TARGET_CLASS)] +#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)] final class Access extends AbstractAnnotation { diff --git a/src/Annotation/Sonata/ActionButton.php b/src/Annotation/Sonata/ActionButton.php index 469e96d..9d56fb0 100644 --- a/src/Annotation/Sonata/ActionButton.php +++ b/src/Annotation/Sonata/ActionButton.php @@ -18,7 +18,7 @@ * @author Marko Kunic * @author Mathieu Wambre */ -#[Attribute(Attribute::TARGET_CLASS)] +#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)] final class ActionButton extends AbstractAction { } diff --git a/src/Annotation/Sonata/AddChild.php b/src/Annotation/Sonata/AddChild.php index 446cfd3..11ffdff 100644 --- a/src/Annotation/Sonata/AddChild.php +++ b/src/Annotation/Sonata/AddChild.php @@ -19,7 +19,7 @@ * @author Marko Kunic * @author Mathieu Wambre */ -#[Attribute(Attribute::TARGET_CLASS)] +#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)] final class AddChild extends AbstractAnnotation { diff --git a/src/Annotation/Sonata/AddRoute.php b/src/Annotation/Sonata/AddRoute.php index b0a0278..ccda810 100644 --- a/src/Annotation/Sonata/AddRoute.php +++ b/src/Annotation/Sonata/AddRoute.php @@ -20,7 +20,7 @@ * @author Marko Kunic * @author Mathieu Wambre */ -#[Attribute(Attribute::TARGET_CLASS)] +#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)] final class AddRoute extends AbstractAnnotation implements RouteAnnotationInterface { diff --git a/src/Annotation/Sonata/DashboardAction.php b/src/Annotation/Sonata/DashboardAction.php index 920ea5c..6adfcec 100644 --- a/src/Annotation/Sonata/DashboardAction.php +++ b/src/Annotation/Sonata/DashboardAction.php @@ -18,7 +18,7 @@ * @author Marko Kunic * @author Mathieu Wambre */ -#[Attribute(Attribute::TARGET_CLASS)] +#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)] final class DashboardAction extends AbstractAction { } diff --git a/src/Annotation/Sonata/ListAction.php b/src/Annotation/Sonata/ListAction.php index 736d51a..87c790f 100644 --- a/src/Annotation/Sonata/ListAction.php +++ b/src/Annotation/Sonata/ListAction.php @@ -19,7 +19,7 @@ * @author Marko Kunic * @author Mathieu Wambre */ -#[Attribute(Attribute::TARGET_CLASS)] +#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)] final class ListAction extends AbstractAnnotation { diff --git a/src/Annotation/Sonata/RemoveRoute.php b/src/Annotation/Sonata/RemoveRoute.php index 711e9bd..8f4f028 100644 --- a/src/Annotation/Sonata/RemoveRoute.php +++ b/src/Annotation/Sonata/RemoveRoute.php @@ -20,7 +20,7 @@ * @author Marko Kunic * @author Mathieu Wambre */ -#[Attribute(Attribute::TARGET_CLASS)] +#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)] final class RemoveRoute extends AbstractAnnotation implements RouteAnnotationInterface { diff --git a/tests/Reader/ListReaderTest.php b/tests/Reader/ListReaderTest.php index 6f0ba8c..0907026 100644 --- a/tests/Reader/ListReaderTest.php +++ b/tests/Reader/ListReaderTest.php @@ -10,7 +10,6 @@ use Neimheadh\SonataAnnotationBundle\Annotation\Sonata\ListField; use Neimheadh\SonataAnnotationBundle\AnnotationReader; use Neimheadh\SonataAnnotationBundle\Exception\MissingAnnotationArgumentException; -use Neimheadh\SonataAnnotationBundle\Reader\FormReader; use Neimheadh\SonataAnnotationBundle\Reader\ListReader; use Neimheadh\SonataAnnotationBundle\Tests\Resources\Extension\CreateNewAnnotationAdminTrait; use Neimheadh\SonataAnnotationBundle\Tests\Resources\Model\Entity\EmptyEntity; @@ -268,6 +267,40 @@ public function shouldAdminAnnotationWorks(): void $this->assertEquals(['id', 'name', '_actions'], $mapper->keys()); } + /** + * Test multiple list action works. + * + * @link https://github.com/neimheadh/SonataAnnotationBundle/issues/10 + * @test + * @functionnal + * + * @return void + */ + public function shouldAllowMultipleListActionAttribute(): void + { + $reader = new ListReader(new AnnotationReader()); + $class = new ReflectionClass(MultipleListAction::class); + $mapper = $this->createNewListMapper(); + + $reader->configureFields($class, $mapper); + $actions = $mapper->get('_actions'); + + $this->assertEquals([ + 'show' => [ + 'template' => '@SonataAdmin/CRUD/list__action_show.html.twig', + ], + 'edit' => [ + 'template' => '@SonataAdmin/CRUD/list__action_edit.html.twig', + ], + 'delete' => [ + 'template' => '@SonataAdmin/CRUD/list__action_delete.html.twig', + ], + 'import' => [ + 'template' => 'import_list_button.html.twig', + ], + ], $actions->getOption('actions')); + } + /** * Create new empty list mapper. * @@ -284,11 +317,16 @@ private function createNewListMapper( /** @var ListBuilderInterface $listBuilder */ $listBuilder = $container->get('sonata.admin.builder.orm_list'); - return new ListMapper( + $mapper = new ListMapper( $listBuilder, new FieldDescriptionCollection(), $this->createNewAnnotationAdmin($class), ); + + $mapper->getAdmin()->hasListFieldDescription('_actions') + && $mapper->getAdmin()->removeListFieldDescription('_actions'); + + return $mapper; } } @@ -345,4 +383,14 @@ public function getField2(): string class BadListAction { +} + + +#[ListAction(name: "show")] +#[ListAction(name: "edit")] +#[ListAction(name: "delete")] +#[ListAction(name: "import", options: ['template' => 'import_list_button.html.twig'])] +class MultipleListAction +{ + } \ No newline at end of file diff --git a/tests/Resources/Model/Entity/EmptyEntity.php b/tests/Resources/Model/Entity/EmptyEntity.php index f6df39c..28d3a6a 100644 --- a/tests/Resources/Model/Entity/EmptyEntity.php +++ b/tests/Resources/Model/Entity/EmptyEntity.php @@ -2,7 +2,6 @@ namespace Neimheadh\SonataAnnotationBundle\Tests\Resources\Model\Entity; -use Neimheadh\SonataAnnotationBundle\Annotation\Sonata; use Doctrine\ORM\Mapping as ORM; /** @@ -20,4 +19,5 @@ class EmptyEntity * @var int */ private int $_id; + } \ No newline at end of file