src/EventListener/AdminLayoutModificationListener.php line 20

Open in your IDE?
  1. <?php
  2. /**
  3.  * Dieser Quellcode ist geistiges Eigentum der Weblizards GmbH
  4.  * und darf ohne vorheriges schriftliches Einverständnis nicht
  5.  * vervielfältigt werden.
  6.  *
  7.  * @copyright  Copyright (c) Weblizards GmbH <https://www.weblizards.de>
  8.  * @license    https://www.gnu.org/licenses/gpl-3.0 GPLv3
  9.  */
  10. namespace App\EventListener;
  11. use Pimcore\Model\DataObject;
  12. use Pimcore\Model\DataObject\ClassDefinition;
  13. use Symfony\Component\EventDispatcher\GenericEvent;
  14. class AdminLayoutModificationListener
  15. {
  16.     public function onObjectGetPreSendData(GenericEvent $event)
  17.     {
  18.         /** @var array $data */
  19.         $data $event->getArgument('data');
  20.         if (($data['currentLayoutId'] ?? null) == -1) {
  21.             // admin layout, don't modify anything
  22.             return;
  23.         }
  24.         /** @var DataObject $object */
  25.         $object $event->getArgument('object');
  26.         $layout $data['layout'] ?? null;
  27.         if (! $layout instanceof ClassDefinition\Layout) {
  28.             // sanity check, get out
  29.             return;
  30.         }
  31.         if ($object instanceof DataObject\Erpitem && $object->getMadeByImport()) {
  32.             if (($erpLayout $this->findClassDefinitionLayoutInLayout($layout'Basics')) !== null) {
  33.                 $this->modifyDefinitionDataInLayout($object$erpLayout, static function (DataObject\Erpitem $erpItemClassDefinition\Data $item) {
  34.                     // never disable the override fields
  35.                     if ($item->getName() == 'madeByImport' || str_ends_with($item->getName(), 'Override')) {
  36.                         return;
  37.                     }
  38.                     // don't disable overridden fields
  39.                     foreach (['drainWeight''weightBrutto''weightNetto''packageType'] as $fieldName) {
  40.                         if ($item->getName() == $fieldName && $erpItem->{'get'.ucfirst($fieldName).'Override'}()) {
  41.                             return;
  42.                         }
  43.                     }
  44.                     $item->setNoteditable(true);
  45.                 });
  46.             }
  47.         } elseif ($object instanceof DataObject\Product) {
  48.             // Different custom layouts for object and variant for markets DACH and EAST
  49.             // https://papayawhip-important-idols.awork.io/tasks/f829491f-9927-ed11-ae83-14cb65275e5b/details
  50.             if (preg_match('~^/Products/(?<market>.*?)/~'$object->getPath(), $matches)) {
  51.                 $layoutId $matches['market'] . (($object->getType() == DataObject\AbstractObject::OBJECT_TYPE_VARIANT) ? 'VAR' 'OBJ');
  52.                 $data $this->doModifyCustomLayouts($data$object$layoutId);
  53.             }
  54.         }
  55.         $event->setArgument('data'$data);
  56.     }
  57.     /**
  58.      * @param ClassDefinition\Layout $layout
  59.      * @param string $name
  60.      *
  61.      * @return ClassDefinition\Data|null
  62.      */
  63.     protected function findClassDefinitionDataInLayout(ClassDefinition\Layout $layoutstring $name): ?ClassDefinition\Data
  64.     {
  65.         foreach ($layout->getChildren() as $item) {
  66.             if ($item instanceof ClassDefinition\Data && $item->getName() == $name) {
  67.                 return $item;
  68.             } elseif ($item instanceof ClassDefinition\Layout) {
  69.                 if (($data $this->findClassDefinitionDataInLayout($item$name)) !== null) {
  70.                     return $data;
  71.                 }
  72.             } elseif ($item instanceof ClassDefinition\Data\Localizedfields) {
  73.                 foreach ($item->getChildren() as $localizedItem) {
  74.                     if ($localizedItem instanceof ClassDefinition\Data && $localizedItem->getName() == $name) {
  75.                         return $localizedItem;
  76.                     }
  77.                 }
  78.             }
  79.         }
  80.         return null;
  81.     }
  82.     /**
  83.      * @param DataObject\Concrete $object
  84.      * @param ClassDefinition\Layout $layout
  85.      * @param callable $callback
  86.      *
  87.      * @return void
  88.      */
  89.     protected function modifyDefinitionDataInLayout(DataObject\Concrete $objectClassDefinition\Layout $layout, callable $callback)
  90.     {
  91.         foreach ($layout->getChildren() as $item) {
  92.             if ($item instanceof ClassDefinition\Data\Localizedfields) {
  93.                 foreach ($item->getChildren() as $localizedItem) {
  94.                     if ($localizedItem instanceof ClassDefinition\Data) {
  95.                         $callback($object$localizedItem);
  96.                     }
  97.                 }
  98.             } elseif ($item instanceof ClassDefinition\Data) {
  99.                 $callback($object$item);
  100.             } elseif ($item instanceof ClassDefinition\Layout) {
  101.                 $this->modifyDefinitionDataInLayout($object$item$callback);
  102.             }
  103.         }
  104.     }
  105.     /**
  106.      * @param ClassDefinition\Layout $layout
  107.      * @param string $name
  108.      *
  109.      * @return ClassDefinition\Layout|null
  110.      */
  111.     protected function findClassDefinitionLayoutInLayout(ClassDefinition\Layout $layoutstring $name): ?ClassDefinition\Layout
  112.     {
  113.         if ($layout->getName() == $name) {
  114.             return $layout;
  115.         }
  116.         foreach ($layout->getChildren() as $item) {
  117.             if ($item instanceof ClassDefinition\Layout) {
  118.                 if (($data $this->findClassDefinitionLayoutInLayout($item$name)) !== null) {
  119.                     return $data;
  120.                 }
  121.             }
  122.         }
  123.         return null;
  124.     }
  125.     /**
  126.      * Modify custom layouts
  127.      *
  128.      * Taken from https://pimcore.com/docs/pimcore/current/Development_Documentation/Best_Practice/Showing_Custom_Layouts_based_on_Object_Data.html
  129.      *
  130.      * @param array $data
  131.      * @param DataObject\Concrete $object
  132.      * @param string|null $customLayoutToSelect
  133.      * @param array $layoutsToRemove
  134.      *
  135.      * @return array
  136.      */
  137.     private function doModifyCustomLayouts(array $dataDataObject\Concrete $objectstring $customLayoutToSelect null, array $layoutsToRemove = [])
  138.     {
  139.         if ($customLayoutToSelect != null) {
  140.             if (($customLayout ClassDefinition\CustomLayout::getById($customLayoutToSelect)) === null) {
  141.                 // NOTE: don't throw an exception but keep default layout
  142.                 // https://papayawhip-important-idols.awork.io/tasks/3269cb00-3e7e-4ebc-be77-cc5637bfaec9/details
  143.                 return $data;
  144.             }
  145.             $data['currentLayoutId'] = $customLayoutToSelect;
  146.             $data['layout'] = $customLayout->getLayoutDefinitions();
  147.             DataObject\Service::enrichLayoutDefinition($data['layout'], $object);
  148.         }
  149.         if (!empty($layoutsToRemove)) {
  150.             $validLayouts $data['validLayouts'];
  151.             foreach ($validLayouts as $key => $validLayout) {
  152.                 if (in_array($validLayout['id'], $layoutsToRemove)) {
  153.                     unset($validLayouts[$key]);
  154.                 }
  155.             }
  156.             $data['validLayouts'] = array_values($validLayouts);
  157.         }
  158.         return $data;
  159.     }
  160. }