vendor/ibexa/taxonomy/src/lib/Event/Subscriber/FieldType/TaxonomyEntryIdentifierConverter.php line 38

Open in your IDE?
  1. <?php
  2. /**
  3.  * @copyright Copyright (C) Ibexa AS. All rights reserved.
  4.  * @license For full copyright and license information view LICENSE file distributed with this source code.
  5.  */
  6. declare(strict_types=1);
  7. namespace Ibexa\Taxonomy\Event\Subscriber\FieldType;
  8. use Ibexa\Contracts\Taxonomy\Service\TaxonomyServiceInterface;
  9. use Ibexa\Migration\Event\FieldValueFromHashEvent;
  10. use Ibexa\Migration\Event\MigrationEvents;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. final class TaxonomyEntryIdentifierConverter implements EventSubscriberInterface
  13. {
  14.     private string $fieldTypeIdentifier;
  15.     private TaxonomyServiceInterface $taxonomyService;
  16.     public function __construct(string $fieldTypeIdentifierTaxonomyServiceInterface $taxonomyService)
  17.     {
  18.         $this->fieldTypeIdentifier $fieldTypeIdentifier;
  19.         $this->taxonomyService $taxonomyService;
  20.     }
  21.     public static function getSubscribedEvents(): array
  22.     {
  23.         return [
  24.             MigrationEvents::BEFORE_FIELD_VALUE_FROM_HASH => [
  25.                 'convertIdentifiersIntoIds',
  26.                 -1,
  27.             ],
  28.         ];
  29.     }
  30.     public function convertIdentifiersIntoIds(FieldValueFromHashEvent $event): void
  31.     {
  32.         if ($event->getFieldTypeIdentifier() !== $this->fieldTypeIdentifier) {
  33.             return;
  34.         }
  35.         $hash $event->getHash();
  36.         if (
  37.             !is_array($hash)
  38.             || isset($hash['taxonomy_entry'])
  39.             || !isset($hash['taxonomy_entry_identifier'])) {
  40.             return;
  41.         }
  42.         $identifier $hash['taxonomy_entry_identifier'];
  43.         if (isset($hash['taxonomy'])) {
  44.             $entry $this->taxonomyService->loadEntryByIdentifier((string)$identifier$hash['taxonomy']);
  45.         } else {
  46.             $entry $this->taxonomyService->loadEntryByIdentifier((string)$identifier);
  47.         }
  48.         $hash['taxonomy_entry'] = $entry;
  49.         unset($hash['taxonomy_entry_identifier']);
  50.         $event->setHash($hash);
  51.     }
  52. }