You are here

FallbackFieldUnserializer.php in Acquia Content Hub 8.2

File

src/EventSubscriber/UnserializeContentField/FallbackFieldUnserializer.php
View source
<?php

namespace Drupal\acquia_contenthub\EventSubscriber\UnserializeContentField;

use Drupal\acquia_contenthub\AcquiaContentHubEvents;
use Drupal\acquia_contenthub\Event\UnserializeCdfEntityFieldEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
 * Generic field unserializer fallback subscriber.
 */
class FallbackFieldUnserializer implements EventSubscriberInterface {

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[AcquiaContentHubEvents::UNSERIALIZE_CONTENT_ENTITY_FIELD] = [
      'onUnserializeContentField',
    ];
    return $events;
  }

  /**
   * Naive handling for fields that don't have special handling.
   *
   * @param \Drupal\acquia_contenthub\Event\UnserializeCdfEntityFieldEvent $event
   *   The unserialize event.
   */
  public function onUnserializeContentField(UnserializeCdfEntityFieldEvent $event) {
    $field = $event
      ->getField();
    $values = [];
    if (!empty($field['value'])) {
      foreach ($field['value'] as $langcode => $value) {
        $values[$langcode][$event
          ->getFieldName()] = $value;
      }
      $event
        ->setValue($values);
    }
    $event
      ->stopPropagation();
  }

}

Classes

Namesort descending Description
FallbackFieldUnserializer Generic field unserializer fallback subscriber.