-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added new
ExplorerEntityListEvent
dispatched on all AjaxExplo…
…rer controllers when they use EntityListManager to allow overriding
- Loading branch information
1 parent
f7bac41
commit d475cb6
Showing
13 changed files
with
256 additions
and
52 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
lib/RoadizCoreBundle/src/Explorer/Event/ExplorerEntityListEvent.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RZ\Roadiz\CoreBundle\Explorer\Event; | ||
|
||
use RZ\Roadiz\Core\AbstractEntities\PersistableInterface; | ||
use Symfony\Contracts\EventDispatcher\Event; | ||
|
||
class ExplorerEntityListEvent extends Event | ||
{ | ||
/** | ||
* @param class-string<PersistableInterface> $entityName | ||
*/ | ||
public function __construct( | ||
private string $entityName, | ||
private array $criteria = [], | ||
private array $ordering = [], | ||
) { | ||
} | ||
|
||
/** | ||
* @return class-string<PersistableInterface> | ||
*/ | ||
public function getEntityName(): string | ||
{ | ||
return $this->entityName; | ||
} | ||
|
||
/** | ||
* @param class-string<PersistableInterface> $entityName | ||
*/ | ||
public function setEntityName(string $entityName): ExplorerEntityListEvent | ||
{ | ||
$this->entityName = $entityName; | ||
|
||
return $this; | ||
} | ||
|
||
public function getCriteria(): array | ||
{ | ||
return $this->criteria; | ||
} | ||
|
||
public function setCriteria(array $criteria): ExplorerEntityListEvent | ||
{ | ||
$this->criteria = $criteria; | ||
|
||
return $this; | ||
} | ||
|
||
public function getOrdering(): array | ||
{ | ||
return $this->ordering; | ||
} | ||
|
||
public function setOrdering(array $ordering): ExplorerEntityListEvent | ||
{ | ||
$this->ordering = $ordering; | ||
|
||
return $this; | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
lib/RoadizCoreBundle/src/Explorer/EventSubscriber/NodeExplorerEntityListEventSubscriber.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RZ\Roadiz\CoreBundle\Explorer\EventSubscriber; | ||
|
||
use RZ\Roadiz\CoreBundle\Entity\Node; | ||
use RZ\Roadiz\CoreBundle\Entity\NodeType; | ||
use RZ\Roadiz\CoreBundle\Explorer\Event\ExplorerEntityListEvent; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
|
||
final class NodeExplorerEntityListEventSubscriber implements EventSubscriberInterface | ||
{ | ||
public static function getSubscribedEvents(): array | ||
{ | ||
return [ | ||
ExplorerEntityListEvent::class => ['onNodeExplorerEntityList', 100], | ||
]; | ||
} | ||
|
||
public function onNodeExplorerEntityList(ExplorerEntityListEvent $event): void | ||
{ | ||
$entity = $event->getEntityName(); | ||
|
||
if (Node::class !== $entity) { | ||
return; | ||
} | ||
|
||
$criteria = $event->getCriteria(); | ||
$ordering = $event->getOrdering(); | ||
|
||
if ( | ||
!isset($criteria['nodeType']) | ||
|| 1 !== count($criteria['nodeType']) | ||
|| !$criteria['nodeType'][0] instanceof NodeType | ||
) { | ||
return; | ||
} | ||
|
||
$event->setEntityName($criteria['nodeType'][0]->getSourceEntityFullQualifiedClassName()); | ||
unset($criteria['nodeType']); | ||
$nodeFields = ['position', 'visible', 'locked', 'status', 'nodeName', 'createdAt', 'updatedAt']; | ||
|
||
// Prefix all criteria array keys names with "node." and recompose criteria array | ||
$event->setCriteria(array_combine( | ||
array_map(function ($key) use ($nodeFields) { | ||
if (in_array($key, $nodeFields)) { | ||
return 'node.'.$key; | ||
} | ||
|
||
return $key; | ||
}, array_keys($criteria)), | ||
array_values($criteria) | ||
)); | ||
$event->setOrdering(array_combine( | ||
array_map(function ($key) use ($nodeFields) { | ||
if (in_array($key, $nodeFields)) { | ||
return 'node.'.$key; | ||
} | ||
|
||
return $key; | ||
}, array_keys($ordering)), | ||
array_values($ordering) | ||
)); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
lib/Rozier/src/AjaxControllers/AbstractAjaxExplorerController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Themes\Rozier\AjaxControllers; | ||
|
||
use RZ\Roadiz\CoreBundle\Explorer\Event\ExplorerEntityListEvent; | ||
use RZ\Roadiz\CoreBundle\Explorer\ExplorerItemFactoryInterface; | ||
use RZ\Roadiz\CoreBundle\ListManager\EntityListManagerInterface; | ||
use Symfony\Component\Serializer\SerializerInterface; | ||
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; | ||
|
||
abstract class AbstractAjaxExplorerController extends AbstractAjaxController | ||
{ | ||
public function __construct( | ||
protected readonly ExplorerItemFactoryInterface $explorerItemFactory, | ||
protected readonly EventDispatcherInterface $eventDispatcher, | ||
SerializerInterface $serializer, | ||
) { | ||
parent::__construct($serializer); | ||
} | ||
|
||
public function createEntityListManager(string $entity, array $criteria = [], array $ordering = []): EntityListManagerInterface | ||
{ | ||
$event = $this->eventDispatcher->dispatch(new ExplorerEntityListEvent($entity, $criteria, $ordering)); | ||
|
||
return parent::createEntityListManager($event->getEntityName(), $event->getCriteria(), $event->getOrdering()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.