-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Add preBatchAction to Admin Extension and add a pre_batch_action event #7579
Conversation
There was a problem hiding this 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public function &getIdx(): array | |
public function getIdx(): array |
?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
6ff9656
to
1fc8bc4
Compare
1fc8bc4
to
7776c37
Compare
Please review again @core23 |
src/Admin/AbstractAdminExtension.php
Outdated
* @param mixed[] $idx | ||
* @phpstan-param AdminInterface<T> $admin | ||
*/ | ||
public function preBatchAction(AdminInterface $admin, string $actionName, ProxyQueryInterface $query, array &$idx, bool $allElements = false): void |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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.
1519dbc
1519dbc
to
3035f70
Compare
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"
3035f70
to
39ba323
Compare
Thanks |
And thank you all for reviewing and accepting the PR! 🎉 |
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
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
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
Subject
This adds a
preBatchAction()
method to theAbstractAdminExtension
andAdminExtensionInterface
(in a backwards compatible way). This enables the AdminEventExtension to dispatch aBatchActionEvent
of typepre_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
To do