You are here

class FallbackFieldSerializer in Acquia Content Hub 8.2

Subscribes to entity field serialization to handle basic field values.

Hierarchy

Expanded class hierarchy of FallbackFieldSerializer

1 file declares its use of FallbackFieldSerializer
EntityMetatagsSerializer.php in modules/acquia_contenthub_metatag/src/EventSubscriber/SerializeContentField/EntityMetatagsSerializer.php
1 string reference to 'FallbackFieldSerializer'
acquia_contenthub.services.yml in ./acquia_contenthub.services.yml
acquia_contenthub.services.yml
1 service uses FallbackFieldSerializer
fallback.field.cdf.serializer in ./acquia_contenthub.services.yml
Drupal\acquia_contenthub\EventSubscriber\SerializeContentField\FallbackFieldSerializer

File

src/EventSubscriber/SerializeContentField/FallbackFieldSerializer.php, line 13

Namespace

Drupal\acquia_contenthub\EventSubscriber\SerializeContentField
View source
class FallbackFieldSerializer implements EventSubscriberInterface {
  use ContentFieldMetadataTrait;

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

  /**
   * Directly reference the field's value property.
   *
   * @param \Drupal\acquia_contenthub\Event\SerializeCdfEntityFieldEvent $event
   *   The content entity field serialization event.
   */
  public function onSerializeContentField(SerializeCdfEntityFieldEvent $event) {
    $this
      ->setFieldMetaData($event);
    $data = [];

    /** @var \Drupal\Core\Entity\TranslatableInterface $entity */
    $entity = $event
      ->getEntity();
    foreach ($entity
      ->getTranslationLanguages() as $langcode => $language) {
      $field = $event
        ->getFieldTranslation($langcode);

      // Set the translation value to represent null field data.
      if (empty(count($field))) {
        $data['value'][$langcode] = [];
        continue;
      }
      foreach ($field as $item) {
        if ($field
          ->getFieldDefinition()
          ->getFieldStorageDefinition()
          ->getCardinality() == FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) {
          $data['value'][$langcode][] = $item
            ->getValue();
        }
        else {
          $data['value'][$langcode] = $item
            ->getValue();
        }
      }
    }
    $event
      ->setFieldData($data);
    $event
      ->stopPropagation();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ContentFieldMetadataTrait::setFieldMetaData protected function Sets field metadata.
FallbackFieldSerializer::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to. 3
FallbackFieldSerializer::onSerializeContentField public function Directly reference the field's value property. 3