Skip to content

Commit

Permalink
[Psdp 327] allow adding another element in perspective tree in perspe…
Browse files Browse the repository at this point in the history
…ctive 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 <[email protected]>
  • Loading branch information
samyemad and samyemad authored Oct 12, 2023
1 parent 1c1f93e commit 66c8252
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 11 deletions.
37 changes: 26 additions & 11 deletions src/Controller/PerspectiveController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

/**
Expand All @@ -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)
{
}

Expand Down Expand Up @@ -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'],
Expand All @@ -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
*
Expand Down
24 changes: 24 additions & 0 deletions src/Event/ElementTree/IconEvents.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\PerspectiveEditorBundle\Event\ElementTree;

final class IconEvents
{
/**
* @var string
*/
public const ADD_ELEMENT_TREE_ICON = 'pimcore.perspectiveEditor.elementTreeIcon.add';
}
34 changes: 34 additions & 0 deletions src/Event/ElementTree/Model/IconAddEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\PerspectiveEditorBundle\Event\ElementTree\Model;

class IconAddEvent
{
public function __construct(
private array $elementTrees
) {
}

public function getElementTreeIcons(): array
{
return $this->elementTrees;
}

public function setElementTreeIcons(array $elementTrees): void
{
$this->elementTrees = $elementTrees;
}
}

0 comments on commit 66c8252

Please sign in to comment.