You are here

class TextItemFieldSerializer in Acquia Content Hub 8.2

Subscribes to entity field serialization to extract the filter format uuid.

Hierarchy

Expanded class hierarchy of TextItemFieldSerializer

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

File

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

Namespace

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

  /**
   * Field types to serialize.
   *
   * @var array
   */
  protected $fieldTypes = [
    'text_with_summary',
    'text',
    'text_long',
  ];

  /**
   * The filter format entity storage handler.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $storage;

  /**
   * TextItemFieldSerializer constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   *
   * @throws \Exception
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
    $this->storage = $entity_type_manager
      ->getStorage('filter_format');
  }

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

  /**
   * Extract filter format UUID and place it into the serialized field values.
   *
   * @param \Drupal\acquia_contenthub\Event\SerializeCdfEntityFieldEvent $event
   *   The content entity field serialization event.
   */
  public function onSerializeContentField(SerializeCdfEntityFieldEvent $event) {
    if (!in_array($event
      ->getField()
      ->getFieldDefinition()
      ->getType(), $this->fieldTypes)) {
      return;
    }
    $this
      ->setFieldMetaData($event);
    $data = [];
    $data['field_type'] = $event
      ->getField()
      ->getFieldDefinition()
      ->getType();

    /** @var \Drupal\Core\Entity\TranslatableInterface $entity */
    $entity = $event
      ->getEntity();
    foreach ($entity
      ->getTranslationLanguages() as $langcode => $language) {
      $field = $event
        ->getFieldTranslation($langcode);
      foreach ($field as $item) {
        $values = $item
          ->getValue();
        if (!empty($values['format']) && ($format = $this->storage
          ->load($values['format']))) {
          $values['format'] = $format
            ->uuid();
        }
        $data['value'][$langcode][] = $values;
      }
    }
    $event
      ->setFieldData($data);
    $event
      ->stopPropagation();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ContentFieldMetadataTrait::setFieldMetaData protected function Sets field metadata.
TextItemFieldSerializer::$fieldTypes protected property Field types to serialize.
TextItemFieldSerializer::$storage protected property The filter format entity storage handler.
TextItemFieldSerializer::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
TextItemFieldSerializer::onSerializeContentField public function Extract filter format UUID and place it into the serialized field values.
TextItemFieldSerializer::__construct public function TextItemFieldSerializer constructor.