bundles/PfcExportBundle/EventListener/TemplateListener.php line 22

Open in your IDE?
  1. <?php
  2. namespace PfcExportBundle\EventListener;
  3. use Pimcore\Event\Model\DataObjectEvent;
  4. use Pimcore\Event\Model\ElementEventInterface;
  5. use Pimcore\Model\DataObject;
  6. use Pimcore\Model\DataObject\ClassDefinition\CustomLayout;
  7. use Pimcore\Model\DataObject\Service;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\EventDispatcher\GenericEvent;
  10. use Symfony\Component\Yaml\Yaml;
  11. class TemplateListener implements EventSubscriberInterface
  12. {
  13.     const TEMPLATE_LAYOUT PIMCORE_PROJECT_ROOT '/bundles/PfcExportBundle/Resources/config/pfcExport/export/config.yml';
  14.     /**
  15.      * @param GenericEvent $event
  16.      */
  17.     public function checkPermissions(GenericEvent $event): void
  18.     {
  19.         $object $event->getArgument("object");
  20.         //data element that is send to Pimcore backend UI
  21.         $data $event->getArgument("data");
  22.         $data['hasPreview'] = false;
  23.         
  24.         if ($object instanceof DataObject\Template) {
  25.             $parsedYaml Yaml::parse(file_get_contents(self::TEMPLATE_LAYOUT));
  26.             $currentLayout explode(","$parsedYaml["pfc_export"]['template_layout']);
  27.             if(!in_array($data['currentLayoutId'], $currentLayout)) {
  28.                 // Set Model Layout when product is a Model and Sku Layout when it is variant (sku)
  29.                 $data $this->f_setLayout($object$data);
  30.                 $event->setArgument("data"$data);
  31.             }
  32.         }
  33.     }
  34.     /**
  35.      * @param $object
  36.      * @param $data
  37.      * @return mixed
  38.      */
  39.     public function f_setLayout($object$data): array
  40.     {
  41.         $parsedYaml Yaml::parse(file_get_contents(self::TEMPLATE_LAYOUT));
  42.         DataObject\Template::setGetInheritedValues(true);
  43.         if ($object->getTemplateType() == "leadTime") {
  44.             $customLayoutToSelect $parsedYaml["pfc_export"]['template_layout'];
  45.         } else {
  46.             $customLayoutToSelect "";
  47.         }
  48.         if ($customLayoutToSelect != null) {
  49.             //set current layout to subcategory layout
  50.             $data['currentLayoutId'] = $customLayoutToSelect;
  51.             $customLayout CustomLayout::getById($customLayoutToSelect);
  52.             $data['layout'] = $customLayout->getLayoutDefinitions();
  53.             Service::enrichLayoutDefinition($data["layout"], $object);
  54.         }
  55.         if (!empty($layoutsToRemove)) {
  56.             //remove master layout from valid layouts
  57.             $validLayouts $data["validLayouts"];
  58.             foreach ($validLayouts as $key => $validLayout) {
  59.                 if (in_array($validLayout['id'], $layoutsToRemove)) {
  60.                     unset($validLayouts[$key]);
  61.                 }
  62.             }
  63.             $data["validLayouts"] = array_values($validLayouts);
  64.         }
  65.         return $data;
  66.     }
  67.     /**
  68.      * @return array
  69.      */
  70.     public static function getSubscribedEvents(): array
  71.     {
  72.         return [
  73. //            'workflow.Product_Workflow.guard' => ['guardPublish'],
  74. //            'workflow.Product_Workflow.leave' => ['onLeave'],
  75.         ];
  76.     }
  77. }