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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/reference/events.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,10 @@ Block events help you customize your templates. Available events are :

If you want more information about block events, you should check the
`"Event" section of block bundle documentation <https://docs.sonata-project.org/projects/SonataBlockBundle/en/3.x/reference/events>`_.

BatchActionEvent
^^^^^^^^^^^^^^^^

This event is dispatched when a batch action is being executed. The event name is:

- ``sonata.admin.event.batch_action.pre_batch_action``
9 changes: 9 additions & 0 deletions src/Admin/AbstractAdminExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,15 @@ public function configureBatchActions(AdminInterface $admin, array $actions): ar
return $actions;
}

// NEXT_MAJOR: Remove the PHPDoc block as the interface will then specify the types
7ochem marked this conversation as resolved.
Show resolved Hide resolved
/**
* @param mixed[] $idx
* @phpstan-param AdminInterface<T> $admin
*/
public function preBatchAction(AdminInterface $admin, string $actionName, ProxyQueryInterface $query, array &$idx, bool $allElements): void
{
}

public function configureExportFields(AdminInterface $admin, array $fields): array
{
return $fields;
Expand Down
11 changes: 11 additions & 0 deletions src/Admin/AdminExtensionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
/**
* @author Thomas Rabaix <[email protected]>
*
* NEXT_MAJOR: Uncomment the actual definition of below methods inside the interface and remove these annotations.
*
* @method void preBatchAction(AdminInterface $admin, string $actionName, ProxyQueryInterface $query, array &$idx, bool $allElements)
*
* @phpstan-template T of object
*/
interface AdminExtensionInterface
Expand Down Expand Up @@ -127,6 +131,13 @@ public function getAccessMapping(AdminInterface $admin): array;
*/
public function configureBatchActions(AdminInterface $admin, array $actions): array;

// NEXT_MAJOR: Uncomment the method definition
///**
// * @param mixed[] $idx
// * @phpstan-param AdminInterface<T> $admin
// */
//public function preBatchAction(AdminInterface $admin, string $actionName, ProxyQueryInterface $query, array &$idx, bool $allElements): void;

/**
* Get a chance to modify export fields.
*
Expand Down
7 changes: 7 additions & 0 deletions src/Controller/CRUDController.php
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,13 @@ public function batchAction(Request $request): Response
$query->setMaxResults(null);

$this->admin->preBatchAction($action, $query, $idx, $allElements);
foreach ($this->admin->getExtensions() as $extension) {
// NEXT_MAJOR: Remove the if-statement around the call to `$extension->preBatchAction()`
// @phpstan-ignore-next-line
if (method_exists($extension, 'preBatchAction')) {
$extension->preBatchAction($this->admin, $action, $query, $idx, $allElements);
}
}

if (\count($idx) > 0) {
$this->admin->getModelManager()->addIdentifiersToQuery($this->admin->getClass(), $query, $idx);
Expand Down
8 changes: 8 additions & 0 deletions src/Event/AdminEventExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,12 @@ public function postRemove(AdminInterface $admin, object $object): void
'sonata.admin.event.persistence.post_remove'
);
}

public function preBatchAction(AdminInterface $admin, string $actionName, ProxyQueryInterface $query, array &$idx, bool $allElements): void
{
$this->eventDispatcher->dispatch(
new BatchActionEvent($admin, BatchActionEvent::TYPE_PRE_BATCH_ACTION, $actionName, $query, $idx, $allElements),
'sonata.admin.event.batch_action.pre_batch_action'
);
}
}
120 changes: 120 additions & 0 deletions src/Event/BatchActionEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\AdminBundle\Event;

use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
use Symfony\Contracts\EventDispatcher\Event;

/**
* This event is sent by hook:
* - preBatchAction.
*
* You can register the listener to the event dispatcher by using:
* - sonata.admin.event.batch_action.pre_batch_action)
*
* @author Jochem Klaver <[email protected]>
*
* @phpstan-template T of object
*/
final class BatchActionEvent extends Event
{
public const TYPE_PRE_BATCH_ACTION = 'pre_batch_action';

/**
* @var AdminInterface<object>
* @phpstan-var AdminInterface<T>
*/
private $admin;

/**
* @var string
* @phpstan-var self::TYPE_*
*/
private $type;

/**
* @var string
*/
private $actionName;

/**
* @var ProxyQueryInterface
*/
private $proxyQuery;

/**
* @var mixed[]
*/
private $idx;

/**
* @var bool
*/
private $allElements;

/**
* @param mixed[] $idx
* @phpstan-param AdminInterface<T> $admin
* @phpstan-param self::TYPE_* $type
*/
public function __construct(AdminInterface $admin, string $type, string $actionName, ProxyQueryInterface $proxyQuery, array &$idx, bool $allElements)
{
$this->admin = $admin;
$this->type = $type;
$this->actionName = $actionName;
$this->proxyQuery = $proxyQuery;
$this->idx = &$idx;
$this->allElements = $allElements;
}

/**
* @phpstan-return AdminInterface<T>
*/
public function getAdmin(): AdminInterface
{
return $this->admin;
}

/**
* @phpstan-return self::TYPE_*
*/
public function getType(): string
{
return $this->type;
}

public function getActionName(): string
{
return $this->actionName;
}

public function getProxyQuery(): ProxyQueryInterface
{
return $this->proxyQuery;
}

/**
* @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

{
return $this->idx;
}

public function isAllElements(): bool
{
return $this->allElements;
}
}
35 changes: 35 additions & 0 deletions tests/Event/AdminEventExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
use Sonata\AdminBundle\Event\AdminEventExtension;
use Sonata\AdminBundle\Event\BatchActionEvent;
use Sonata\AdminBundle\Event\ConfigureEvent;
use Sonata\AdminBundle\Event\ConfigureQueryEvent;
use Sonata\AdminBundle\Event\PersistenceEvent;
Expand Down Expand Up @@ -189,4 +190,38 @@ public function testPostRemove(): void
static::equalTo('sonata.admin.event.persistence.post_remove'),
])->postRemove($this->createMock(AdminInterface::class), new \stdClass());
}

public function testPreBatchAction(): void
{
$admin = $this->createMock(AdminInterface::class);
$proxyQuery = $this->createMock(ProxyQueryInterface::class);
$idx = [1, 2, 3];

$this->getExtension([
static::callback(
static function (Event $event) use (&$idx): bool {
if (!$event instanceof BatchActionEvent) {
return false;
}

// @phpstan-ignore-next-line
if (BatchActionEvent::TYPE_PRE_BATCH_ACTION !== $event->getType()) {
return false;
}

if ('delete' !== $event->getActionName()) {
return false;
}

$idx[] = 4; // Test if this was passed by reference correctly everywhere
if ($event->getIdx() !== $idx) {
return false;
}

return true;
}
),
static::equalTo('sonata.admin.event.batch_action.pre_batch_action'),
])->preBatchAction($admin, 'delete', $proxyQuery, $idx, false);
}
}