vendor/ibexa/taxonomy/src/lib/Event/Subscriber/FieldType/TaxonomyEntryAssignmentIdentifierConverter.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 TaxonomyEntryAssignmentIdentifierConverter 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_entries'])
  39.             || !isset($hash['taxonomy_entries_identifiers'])
  40.         ) {
  41.             return;
  42.         }
  43.         $identifiers $hash['taxonomy_entries_identifiers'];
  44.         $entries = [];
  45.         foreach ($identifiers as $identifier) {
  46.             if (isset($hash['taxonomy'])) {
  47.                 $entry $this->taxonomyService->loadEntryByIdentifier((string)$identifier$hash['taxonomy']);
  48.             } else {
  49.                 $entry $this->taxonomyService->loadEntryByIdentifier((string)$identifier);
  50.             }
  51.             $entries[] = $entry;
  52.         }
  53.         $hash['taxonomy_entries'] = $entries;
  54.         unset($hash['taxonomy_entries_identifiers']);
  55.         $event->setHash($hash);
  56.     }
  57. }