From 66c8252ebeae03857ff5cecbebb591828feefd3b Mon Sep 17 00:00:00 2001 From: samy emad Date: Thu, 12 Oct 2023 11:46:33 +0300 Subject: [PATCH] [Psdp 327] allow adding another element in perspective tree in perspective editor (#148) * [Improvements] allow add custom perspective element trees in perspective editor * Apply php-cs-fixer changes * [Improvement] unify the naming convention in event names * [bug] add type hint to parameter elementTrees --------- Co-authored-by: samyemad --- src/Controller/PerspectiveController.php | 37 ++++++++++++++------ src/Event/ElementTree/IconEvents.php | 24 +++++++++++++ src/Event/ElementTree/Model/IconAddEvent.php | 34 ++++++++++++++++++ 3 files changed, 84 insertions(+), 11 deletions(-) create mode 100644 src/Event/ElementTree/IconEvents.php create mode 100644 src/Event/ElementTree/Model/IconAddEvent.php diff --git a/src/Controller/PerspectiveController.php b/src/Controller/PerspectiveController.php index 58b2330..8e31bd8 100644 --- a/src/Controller/PerspectiveController.php +++ b/src/Controller/PerspectiveController.php @@ -16,6 +16,8 @@ namespace Pimcore\Bundle\PerspectiveEditorBundle\Controller; use Pimcore\Bundle\AdminBundle\Security\CsrfProtectionHandler; +use Pimcore\Bundle\PerspectiveEditorBundle\Event\ElementTree\IconEvents; +use Pimcore\Bundle\PerspectiveEditorBundle\Event\ElementTree\Model\IconAddEvent; use Pimcore\Bundle\PerspectiveEditorBundle\PimcorePerspectiveEditorBundle; use Pimcore\Bundle\PerspectiveEditorBundle\Services\PerspectiveAccessor; use Pimcore\Bundle\PerspectiveEditorBundle\Services\TreeHelper; @@ -24,6 +26,7 @@ use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Annotation\Route; +use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; use Symfony\Contracts\Translation\TranslatorInterface; /** @@ -35,7 +38,7 @@ class PerspectiveController extends UserAwareController { protected $disabledCssClass = 'pimcore_tree_node_disabled'; - public function __construct(protected TranslatorInterface $translator) + public function __construct(protected TranslatorInterface $translator, protected EventDispatcherInterface $eventDispatcher) { } @@ -275,24 +278,17 @@ protected function buildElementTree(TreeHelper $treeHelper, $config, $position = return []; } $disabledClass = $config['writeable'] ? '' : $this->disabledCssClass; - - $treeIcons = [ - 'documents' => 'pimcore_icon_document', - 'assets' => 'pimcore_icon_asset', - 'objects' => 'pimcore_icon_object', - 'customview' => 'pimcore_icon_custom_views' - ]; - + $elementTreeIcons = $this->getElementTreeIcons(); $tree = []; foreach ($config['elementTree'] as $element) { if ($position === ($element['position'] ?? 'left')) { $tree[] = [ 'id' => $treeHelper->createUuid(), - 'text' => $element['type'], + 'text' => preg_replace("/[\-_]/", ' ', $element['type']), 'type' => 'elementTreeElement', 'leaf' => true, 'allowDrag' => true, - 'iconCls' => $treeIcons[$element['type']], + 'iconCls' => $elementTreeIcons[$element['type']] ?? '', 'config' => $element, 'cls' => $disabledClass, 'writeable' => $config['writeable'], @@ -307,6 +303,25 @@ protected function buildElementTree(TreeHelper $treeHelper, $config, $position = return $tree; } + /** + * get All Icons related to perspective element tree + * + * @return array + */ + private function getElementTreeIcons(): array + { + $elementTreeIcons = [ + 'documents' => 'pimcore_icon_document', + 'assets' => 'pimcore_icon_asset', + 'objects' => 'pimcore_icon_object', + 'customview' => 'pimcore_icon_custom_views' + ]; + $elementTreeIconAddEvent = new IconAddEvent($elementTreeIcons); + $this->eventDispatcher->dispatch($elementTreeIconAddEvent, IconEvents::ADD_ELEMENT_TREE_ICON); + + return $elementTreeIconAddEvent->getElementTreeIcons(); + } + /** * @param array $config * diff --git a/src/Event/ElementTree/IconEvents.php b/src/Event/ElementTree/IconEvents.php new file mode 100644 index 0000000..10e1a25 --- /dev/null +++ b/src/Event/ElementTree/IconEvents.php @@ -0,0 +1,24 @@ +elementTrees; + } + + public function setElementTreeIcons(array $elementTrees): void + { + $this->elementTrees = $elementTrees; + } +}