<?php
namespace App\EventListeners;
use App\Interfaces\UniqueElementInterface;
use Pimcore\Event\Model\DataObjectEvent;
use Pimcore\Model\DataObject\Fieldcollection;
use Pimcore\Model\DataObject\GameOptionStep;
class GameOptionsListener
{
public function onPreUpdate(DataObjectEvent $event)
{
$gameOption = $event->getObject();
if (!$gameOption instanceof GameOptionStep) {
return;
}
$options = $gameOption->getGameOptions() ?? new Fieldcollection();
$dataModified = false;
foreach ($options->getItems() as $option) {
if ($option instanceof UniqueElementInterface) {
if (empty($option->getUniqueId())) {
$option->setUniqueId(uniqid());
$dataModified = true;
}
}
if ($dataModified == true) {
$gameOption->setGameOptions($options);
}
}
}
}