Created
December 7, 2022 12:16
-
-
Save LukasLoeffler/89723bcc88174a172ce4d22f2156ab89 to your computer and use it in GitHub Desktop.
Select PimcoreLayout by property
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\EventListener; | |
use Symfony\Component\EventDispatcher\GenericEvent; | |
use Pimcore\Model\DataObject\ClassDefinition\CustomLayout; | |
use Pimcore\Model\DataObject\Service; | |
use Pimcore\Model\DataObject\Product; | |
use Pimcore\Model\DataObject\Folder; | |
class MyEventListener { | |
public function selectCustomLayout(GenericEvent $event) { | |
$object = $event->getArgument("object"); | |
if($object instanceof \Pimcore\Model\DataObject\Artikel) { | |
//data element that is send to Pimcore backend UI | |
$data = $event->getArgument("data"); | |
$layoutName = $object->getProperty("layout"); | |
if ($layoutName != null) { | |
$customLayoutToSelect = CustomLayout::getByName($layoutName)->getId(); | |
$data = $this->doModifyCustomLayouts($data, $object, $customLayoutToSelect); | |
} else { | |
$data = $this->doModifyCustomLayouts($data, $object, ); | |
} | |
$event->setArgument("data", $data); | |
} | |
} | |
/** | |
* | |
*/ | |
private function doModifyCustomLayouts($data, $object, $customLayoutToSelect = null) { | |
if($customLayoutToSelect != null) { | |
//set current layout to subcategory layout | |
$data['currentLayoutId'] = $customLayoutToSelect; | |
$customLayout = CustomLayout::getById($customLayoutToSelect); | |
$data['layout'] = $customLayout->getLayoutDefinitions(); | |
Service::enrichLayoutDefinition($data["layout"], $object); | |
} | |
$validLayouts = $data["validLayouts"]; | |
foreach($validLayouts as $key => $validLayout) { | |
if($validLayout['id'] !== $customLayoutToSelect) { | |
unset($validLayouts[$key]); | |
} | |
} | |
$data["validLayouts"] = array_values($validLayouts); | |
return $data; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment