class LayoutBuilderFieldSerializer in Acquia Content Hub 8.2
Subscribes to entity field serialization to handle layout builder fields.
Hierarchy
- class \Drupal\acquia_contenthub\EventSubscriber\SerializeContentField\LayoutBuilderFieldSerializer implements \Symfony\Component\EventDispatcher\EventSubscriberInterface uses ContentFieldMetadataTrait, LayoutBuilderDataHandlerTrait
Expanded class hierarchy of LayoutBuilderFieldSerializer
1 string reference to 'LayoutBuilderFieldSerializer'
1 service uses LayoutBuilderFieldSerializer
File
- src/
EventSubscriber/ SerializeContentField/ LayoutBuilderFieldSerializer.php, line 14
Namespace
Drupal\acquia_contenthub\EventSubscriber\SerializeContentFieldView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ContentFieldMetadataTrait:: |
protected | function | Sets field metadata. | |
LayoutBuilderDataHandlerTrait:: |
protected | function | Gets configuration for a Layout Builder component. | |
LayoutBuilderDataHandlerTrait:: |
protected | function | Prepares component to be serialized. | |
LayoutBuilderDataHandlerTrait:: |
protected | function | Prepares Layout Builder sections to be serialized. | |
LayoutBuilderDataHandlerTrait:: |
protected | function | Prepares Layout Builder components to be unserialized. | |
LayoutBuilderDataHandlerTrait:: |
protected | function | Prepares Layout Builder sections to be unserialized. | |
LayoutBuilderFieldSerializer:: |
protected | property |
The entity type manager. Overrides LayoutBuilderDataHandlerTrait:: |
|
LayoutBuilderFieldSerializer:: |
constant | |||
LayoutBuilderFieldSerializer:: |
public static | function | Returns an array of event names this subscriber wants to listen to. | |
LayoutBuilderFieldSerializer:: |
public | function | Prepare layout builder field. | |
LayoutBuilderFieldSerializer:: |
public | function | LayoutBuilderFieldSerializer constructor. |