<?php
namespace App\EventListener;
use App\Model\Product\Car;
use Pimcore\Event\Model\DataObjectEvent;
use Pimcore\Model\DataObject\ClassDefinition;
use Pimcore\Model\DataObject\ClassDefinition\Data\Objectbricks;
use Pimcore\Model\DataObject\Product;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
use Symfony\Component\EventDispatcher\GenericEvent;
final class OnPreSendDataEventListener {
public function onPreSendData(GenericEvent $event)
{
return;
$object = $event->getArgument("object");
if(!$object instanceof Product) {
return;
}
// get current category
$category = $object->getCategorie_exact();
if($category == null) {
return;
}
$categoryKey = strtolower($category->getKey());
$data = $event->getArgument("data");
$this->hideObjectBrickByCategory($categoryKey, $data);
$event->setArgument("data", $data);
}
private function hideObjectBrickByCategory($category, $data)
{
$category = str_replace(' ', '', $category);
$categroy = strtolower($category);
$availableTechData = [$category];
$data = $this->modifyData($availableTechData, $data);
return $data;
}
public function modifyData($availableTechData, $data)
{
foreach($data['layout']->childs[0]->childs[1]->childs[4]->childs as $key => $value) {
if ($value instanceof ObjectBricks) {
$cleanTitle = str_replace(' ', '', $value->title);
$cleanTitle = strtolower($cleanTitle);
$cleanName = str_replace(' ', '', $value->name);
$cleanName = strtolower($cleanName);
if(in_array($cleanTitle, $availableTechData) || in_array($cleanName, $availableTechData)) {
continue;
}
if ($cleanTitle == "techdatageneral" || $cleanName == "techdatageneral" ) {
continue;
}
$value->invisible = true;
}
}
return $data;
}
}