You are here

class LayoutBuilderFieldSerializer in Acquia Content Hub 8.2

Subscribes to entity field serialization to handle layout builder fields.

Hierarchy

Expanded class hierarchy of LayoutBuilderFieldSerializer

1 string reference to 'LayoutBuilderFieldSerializer'
acquia_contenthub.services.yml in ./acquia_contenthub.services.yml
acquia_contenthub.services.yml
1 service uses LayoutBuilderFieldSerializer
layout_builder.field.cdf.serializer in ./acquia_contenthub.services.yml
Drupal\acquia_contenthub\EventSubscriber\SerializeContentField\LayoutBuilderFieldSerializer

File

src/EventSubscriber/SerializeContentField/LayoutBuilderFieldSerializer.php, line 14

Namespace

Drupal\acquia_contenthub\EventSubscriber\SerializeContentField
View source
class LayoutBuilderFieldSerializer implements EventSubscriberInterface {
  use ContentFieldMetadataTrait;
  use LayoutBuilderDataHandlerTrait;
  const FIELD_TYPE = 'layout_section';

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManager
   */
  protected $entityTypeManager;

  /**
   * LayoutBuilderFieldSerializer constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   *   The entity type manager.
   */
  public function __construct(EntityTypeManagerInterface $entityTypeManager) {
    $this->entityTypeManager = $entityTypeManager;
  }

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

  /**
   * Prepare layout builder field.
   *
   * @param \Drupal\acquia_contenthub\Event\SerializeCdfEntityFieldEvent $event
   *   The content entity field serialization event.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public function onSerializeContentField(SerializeCdfEntityFieldEvent $event) {
    $event_field_type = $event
      ->getField()
      ->getFieldDefinition()
      ->getType();
    if ($event_field_type !== self::FIELD_TYPE) {
      return;
    }
    $this
      ->setFieldMetaData($event);
    $data = [];

    /** @var \Drupal\Core\Entity\TranslatableInterface $entity */
    $entity = $event
      ->getEntity();
    foreach ($entity
      ->getTranslationLanguages() as $langcode => $language) {
      $field = $event
        ->getFieldTranslation($langcode);
      if ($field
        ->isEmpty()) {
        $data['value'][$langcode] = [];
        continue;
      }
      $sections = [];
      foreach ($field as $item) {
        $sections[] = $item
          ->getValue()['section'];
      }
      $sections = $this
        ->serializeSections(...$sections);
      foreach ($sections as $section) {
        $data['value'][$langcode][] = [
          'section' => $section,
        ];
      }
    }
    $event
      ->setFieldData($data);
    $event
      ->stopPropagation();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ContentFieldMetadataTrait::setFieldMetaData protected function Sets field metadata.
LayoutBuilderDataHandlerTrait::getComponentConfiguration protected function Gets configuration for a Layout Builder component.
LayoutBuilderDataHandlerTrait::serializeComponents protected function Prepares component to be serialized.
LayoutBuilderDataHandlerTrait::serializeSections protected function Prepares Layout Builder sections to be serialized.
LayoutBuilderDataHandlerTrait::unserializeComponents protected function Prepares Layout Builder components to be unserialized.
LayoutBuilderDataHandlerTrait::unserializeSections protected function Prepares Layout Builder sections to be unserialized.
LayoutBuilderFieldSerializer::$entityTypeManager protected property The entity type manager. Overrides LayoutBuilderDataHandlerTrait::$entityTypeManager
LayoutBuilderFieldSerializer::FIELD_TYPE constant
LayoutBuilderFieldSerializer::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
LayoutBuilderFieldSerializer::onSerializeContentField public function Prepare layout builder field.
LayoutBuilderFieldSerializer::__construct public function LayoutBuilderFieldSerializer constructor.