You are here

class LayoutBuilderFieldUnserializer in Acquia Content Hub 8.2

Layout builder field unserializer fallback subscriber.

Hierarchy

Expanded class hierarchy of LayoutBuilderFieldUnserializer

1 string reference to 'LayoutBuilderFieldUnserializer'
acquia_contenthub.services.yml in ./acquia_contenthub.services.yml
acquia_contenthub.services.yml
1 service uses LayoutBuilderFieldUnserializer
layout_builder.field.cdf.unserializer in ./acquia_contenthub.services.yml
Drupal\acquia_contenthub\EventSubscriber\UnserializeContentField\LayoutBuilderFieldUnserializer

File

src/EventSubscriber/UnserializeContentField/LayoutBuilderFieldUnserializer.php, line 14

Namespace

Drupal\acquia_contenthub\EventSubscriber\UnserializeContentField
View source
class LayoutBuilderFieldUnserializer implements EventSubscriberInterface {
  use LayoutBuilderDataHandlerTrait;

  /**
   * Layout section field type definition.
   *
   * @var string
   */
  protected $fieldType = '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::UNSERIALIZE_CONTENT_ENTITY_FIELD] = [
      'onUnserializeContentField',
    ];
    return $events;
  }

  /**
   * Handling for Layout Builder sections.
   *
   * @param \Drupal\acquia_contenthub\Event\UnserializeCdfEntityFieldEvent $event
   *   The unserialize event.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public function onUnserializeContentField(UnserializeCdfEntityFieldEvent $event) {
    $event_field_type = $event
      ->getFieldMetadata()['type'];
    if ($event_field_type !== $this->fieldType) {
      return;
    }
    $field = $event
      ->getField();
    $values = [];
    if (!empty($field['value'])) {
      foreach ($field['value'] as $langcode => $sections) {
        foreach ($this
          ->unserializeSections($sections) as $section) {
          $values[$langcode][$event
            ->getFieldName()][] = [
            'section' => $section,
          ];
        }
      }
      $event
        ->setValue($values);
    }
    $event
      ->stopPropagation();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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.
LayoutBuilderFieldUnserializer::$entityTypeManager protected property The entity type manager. Overrides LayoutBuilderDataHandlerTrait::$entityTypeManager
LayoutBuilderFieldUnserializer::$fieldType protected property Layout section field type definition.
LayoutBuilderFieldUnserializer::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
LayoutBuilderFieldUnserializer::onUnserializeContentField public function Handling for Layout Builder sections.
LayoutBuilderFieldUnserializer::__construct public function LayoutBuilderFieldSerializer constructor.