From 125b615366286905f31e66917cbd8360c5e937f7 Mon Sep 17 00:00:00 2001 From: SEMAD01 Date: Fri, 29 Sep 2023 16:11:33 +0300 Subject: [PATCH 1/4] [Improvements] allow add custom perspective element trees in perspective editor --- 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..e345198 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..3db3b41 --- /dev/null +++ b/src/Event/ElementTree/IconEvents.php @@ -0,0 +1,24 @@ +elementTrees; + } + + public function setElementTreeIcons($elementTrees): void + { + $this->elementTrees = $elementTrees; + } +} From 24fbb0abd0f6a72e7dbeb020189ced44da76e533 Mon Sep 17 00:00:00 2001 From: samyemad Date: Fri, 29 Sep 2023 13:12:15 +0000 Subject: [PATCH 2/4] Apply php-cs-fixer changes --- src/Controller/PerspectiveController.php | 2 +- src/Event/ElementTree/Model/IconAddEvent.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Controller/PerspectiveController.php b/src/Controller/PerspectiveController.php index e345198..8e31bd8 100644 --- a/src/Controller/PerspectiveController.php +++ b/src/Controller/PerspectiveController.php @@ -288,7 +288,7 @@ protected function buildElementTree(TreeHelper $treeHelper, $config, $position = 'type' => 'elementTreeElement', 'leaf' => true, 'allowDrag' => true, - 'iconCls' => $elementTreeIcons[$element['type']] ?? '', + 'iconCls' => $elementTreeIcons[$element['type']] ?? '', 'config' => $element, 'cls' => $disabledClass, 'writeable' => $config['writeable'], diff --git a/src/Event/ElementTree/Model/IconAddEvent.php b/src/Event/ElementTree/Model/IconAddEvent.php index b1d071e..72d198f 100644 --- a/src/Event/ElementTree/Model/IconAddEvent.php +++ b/src/Event/ElementTree/Model/IconAddEvent.php @@ -29,6 +29,6 @@ public function getElementTreeIcons(): array public function setElementTreeIcons($elementTrees): void { - $this->elementTrees = $elementTrees; + $this->elementTrees = $elementTrees; } } From 05966ab7311c4b6f88d92536304025cdf583f122 Mon Sep 17 00:00:00 2001 From: SEMAD01 Date: Fri, 29 Sep 2023 19:04:07 +0300 Subject: [PATCH 3/4] [Improvement] unify the naming convention in event names --- src/Event/ElementTree/IconEvents.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Event/ElementTree/IconEvents.php b/src/Event/ElementTree/IconEvents.php index 3db3b41..10e1a25 100644 --- a/src/Event/ElementTree/IconEvents.php +++ b/src/Event/ElementTree/IconEvents.php @@ -20,5 +20,5 @@ final class IconEvents /** * @var string */ - public const ADD_ELEMENT_TREE_ICON = 'pimcore.perspective.editor.add.elementTreeIcon'; + public const ADD_ELEMENT_TREE_ICON = 'pimcore.perspectiveEditor.elementTreeIcon.add'; } From 9f6927d63d593e9fe2ef742edead80ec4b1827b2 Mon Sep 17 00:00:00 2001 From: SEMAD01 Date: Thu, 12 Oct 2023 11:42:05 +0300 Subject: [PATCH 4/4] [bug] add type hint to parameter elementTrees --- src/Event/ElementTree/Model/IconAddEvent.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Event/ElementTree/Model/IconAddEvent.php b/src/Event/ElementTree/Model/IconAddEvent.php index 72d198f..7cd27c1 100644 --- a/src/Event/ElementTree/Model/IconAddEvent.php +++ b/src/Event/ElementTree/Model/IconAddEvent.php @@ -27,7 +27,7 @@ public function getElementTreeIcons(): array return $this->elementTrees; } - public function setElementTreeIcons($elementTrees): void + public function setElementTreeIcons(array $elementTrees): void { $this->elementTrees = $elementTrees; }