src/EventListeners/GameOptionsListener.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\EventListeners;
  3. use App\Interfaces\UniqueElementInterface;
  4. use Pimcore\Event\Model\DataObjectEvent;
  5. use Pimcore\Model\DataObject\Fieldcollection;
  6. use Pimcore\Model\DataObject\GameOptionStep;
  7. class GameOptionsListener
  8. {
  9.     public function onPreUpdate(DataObjectEvent $event)
  10.     {
  11.         $gameOption $event->getObject();
  12.         if (!$gameOption instanceof GameOptionStep) {
  13.             return;
  14.         }
  15.         $options $gameOption->getGameOptions() ?? new Fieldcollection();
  16.         $dataModified false;
  17.         foreach ($options->getItems() as $option) {
  18.             if ($option instanceof UniqueElementInterface) {
  19.                 if (empty($option->getUniqueId())) {
  20.                     $option->setUniqueId(uniqid());
  21.                     $dataModified true;
  22.                 }
  23.             }
  24.             if ($dataModified == true) {
  25.                 $gameOption->setGameOptions($options);
  26.             }
  27.         }
  28.     }
  29. }