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

Add preBatchAction to Admin Extension and add a pre_batch_action event #7579

Merged

Conversation

7ochem
Copy link
Contributor

@7ochem 7ochem commented Nov 1, 2021

Subject

This adds a preBatchAction() method to the AbstractAdminExtension and AdminExtensionInterface (in a backwards compatible way). This enables the AdminEventExtension to dispatch a BatchActionEvent of type pre_batch_action. Both the extension and the event provide points to hook into within your own code, just like this is possible for pre/post persist/update/delete.

I am targeting 4.x as this is a feature that is backwards compatible (made in a BC way)

Fixes #7516 "Implement an event for batch actions"
Relates to #6550 "Delete Event isn't triggered in Batch delete"

Changelog

### Added
- Added `AbstractAdminExtension::preBatchAction()` and `AdminExtensionInterface::preBatchAction()` (as annotation for BC) to have an extension point on batch actions.
- Added `AdminEventExtension::preBatchAction()` that dispatches a `sonata.admin.event.batch_action.pre_batch_action` event with a BatchActionEvent object containing the data
- Added BatchActionEvent class as a transport for (newly introduced) batch action events

To do

  • Update the documentation;

Copy link
Member

@VincentLanglet VincentLanglet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems nice

/**
* @return mixed[]
*/
public function &getIdx(): array
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public function &getIdx(): array
public function getIdx(): array

?

Copy link
Contributor Author

@7ochem 7ochem Nov 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The array of IDs selected for the batch actions is passed by reference everywhere. Also to the AbstractAdmin::preBatchAction() for example. This enables you to manipulate the IDs. So here it should also be returned by reference, hence the & in front of the function name.

This enables you to manipulate the array of IDs in your event listener/subscriber:

$idx = &$event->getIdx();
$idx[] = 123;

Above will result in the batch action also being executed on object with ID 123. Of course this is an example and normally you'll have some business logic in there. And you could also remove IDs of course.

This can already be done in an Admin's preBatchAction() method.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't know this syntax. Thanks for the explanation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't be better to have getters and setters?

Copy link
Contributor Author

@7ochem 7ochem Nov 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like:

$idx = $event->getIdx();
$idx[] = 123;
$event->setIdx($idx);

That could be possible. However, I tried to be consistent with the concept of passing by reference that is already in the existing code for preBatchAction()

In my opinion I think it would be better to have an array object so the whole passing by reference is not necessary, but that would be breaking compatibility for the existing code of preBatchAction(). Maybe this is a good idea for 5.x

@7ochem 7ochem force-pushed the add-pre-batch-action-event branch from 6ff9656 to 1fc8bc4 Compare November 4, 2021 20:53
@7ochem 7ochem marked this pull request as ready for review November 4, 2021 20:58
@7ochem 7ochem requested a review from VincentLanglet November 4, 2021 20:58
src/Event/BatchActionEvent.php Outdated Show resolved Hide resolved
src/Event/BatchActionEvent.php Outdated Show resolved Hide resolved
@7ochem 7ochem force-pushed the add-pre-batch-action-event branch from 1fc8bc4 to 7776c37 Compare November 5, 2021 07:52
@7ochem 7ochem requested a review from core23 November 5, 2021 07:53
VincentLanglet
VincentLanglet previously approved these changes Nov 5, 2021
@7ochem 7ochem requested a review from jordisala1991 November 7, 2021 19:50
jordisala1991
jordisala1991 previously approved these changes Nov 7, 2021
@VincentLanglet
Copy link
Member

Please review again @core23

* @param mixed[] $idx
* @phpstan-param AdminInterface<T> $admin
*/
public function preBatchAction(AdminInterface $admin, string $actionName, ProxyQueryInterface $query, array &$idx, bool $allElements = false): void
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need the default value for allElements? There is no call without that argument.

Copy link
Member

@VincentLanglet VincentLanglet Nov 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to keep the consistency with

public function preBatchAction(string $actionName, ProxyQueryInterface $query, array &$idx, bool $allElements = false): void;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is also no call for the AdminInterface::preBatchAction that does not provide the allElements parameter:

$this->admin->preBatchAction($action, $query, $idx, $allElements);

It looks more like some kind of bug, that we provide a default value here. We should ignore the current consistency in favor of advertising the correct arguments.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be confusing for the user to have two different method signatures.

What about keeping it like this with a NEXT MAJOR on both method to remove the default argument ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be confusing for the user to have two different method signatures.

What about keeping it like this with a NEXT MAJOR on both method to remove the default argument ?

This can be done on the stable branch
https://3v4l.org/V0cUU

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed @7ochem can you do it ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just so I understand this correctly:

I remove the default from the interface?

- * @method void preBatchAction(AdminInterface $admin, string $actionName, ProxyQueryInterface $query, array &$idx, bool $allElements = false)
+ * @method void preBatchAction(AdminInterface $admin, string $actionName, ProxyQueryInterface $query, array &$idx, bool $allElements)

But keep it on the AbstractAdminExtension implementation, here:

public function preBatchAction(AdminInterface $admin, string $actionName, ProxyQueryInterface $query, array &$idx, bool $allElements = false): void

Am I correct @core23 ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think he want to remove it in both the Interface and the implementation (and remove it too for the AdminINterface)

Copy link
Contributor Author

@7ochem 7ochem Nov 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think he want to remove it in both the Interface and the implementation

👍🏻 Ok, I'll change that and push it

(and remove it too for the AdminINterface)

Removing it inside the existing method in the AdminInterface is a BC break. Also, in my opinion, that would be outside of the scope of this PR. However I would be happy to issue a new PR afterwards for this change.

@7ochem 7ochem dismissed stale reviews from jordisala1991 and VincentLanglet via 1519dbc November 9, 2021 15:13
@7ochem 7ochem force-pushed the add-pre-batch-action-event branch 2 times, most recently from 1519dbc to 3035f70 Compare November 9, 2021 15:15
This adds a preBatchAction() method to the AbstractAdminExtension and
AdminExtensionInterface (BC). This enables the AdminEventExtension to
dispatch a BatchActionEvent of type pre_batch_action. Both the extension
and the event provide points to hook into within your own code, just
like this is possible for pre/post persist/update/delete.

Fixes sonata-project#7516 "Implement an event for batch actions"
Relates to sonata-project#6550 "Delete Event isn't triggered in Batch delete"
@7ochem 7ochem force-pushed the add-pre-batch-action-event branch from 3035f70 to 39ba323 Compare November 9, 2021 15:17
@VincentLanglet VincentLanglet merged commit c6a99fc into sonata-project:4.x Nov 9, 2021
@VincentLanglet
Copy link
Member

Thanks

@7ochem 7ochem deleted the add-pre-batch-action-event branch November 11, 2021 09:15
@7ochem
Copy link
Contributor Author

7ochem commented Nov 11, 2021

Thanks

And thank you all for reviewing and accepting the PR! 🎉

7ochem added a commit to 7ochem/SonataAdminBundle that referenced this pull request Nov 25, 2021
A pre batch action event was added to 4.x through sonata-project#7579. This
processes all the NEXT_MAJOR comments for 5.x for this specific
feature
7ochem added a commit to 7ochem/SonataAdminBundle that referenced this pull request Nov 26, 2021
A pre batch action event was added to 4.x through sonata-project#7579. This
processes all the NEXT_MAJOR comments for 5.x for this specific
feature
VincentLanglet pushed a commit that referenced this pull request Nov 26, 2021
A pre batch action event was added to 4.x through #7579. This
processes all the NEXT_MAJOR comments for 5.x for this specific
feature
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement an event for batch actions
4 participants