You are here

class TextItemField in Acquia Content Hub 8.2

Manual handling for text item fields filter references.

Hierarchy

Expanded class hierarchy of TextItemField

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

File

src/EventSubscriber/UnserializeContentField/TextItemField.php, line 12

Namespace

Drupal\acquia_contenthub\EventSubscriber\UnserializeContentField
View source
class TextItemField implements EventSubscriberInterface {
  use FieldEntityDependencyTrait;

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

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

  /**
   * Extract the stored filter_format uuid and retrieve the entity id.
   *
   * @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) {
    if (!in_array($event
      ->getFieldMetadata()['type'], $this->fieldTypes)) {
      return;
    }
    $field = $event
      ->getField();
    $values = [];

    // Return early if no attr values are set.
    if (empty($field['value'])) {
      return;
    }
    foreach ($field['value'] as $langcode => $value) {
      foreach ($value as $delta => &$item) {
        if (!$item['format']) {
          continue;
        }
        $filter = $this
          ->getEntity($item['format'], $event);
        $item['format'] = $filter
          ->id();
      }
      $values[$langcode][$event
        ->getFieldName()] = $value;
    }
    $event
      ->setValue($values);
    $event
      ->stopPropagation();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FieldEntityDependencyTrait::getEntity protected function Get an entity from the dependency stack.
TextItemField::$fieldTypes protected property Field types to unserialize.
TextItemField::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
TextItemField::onUnserializeContentField public function Extract the stored filter_format uuid and retrieve the entity id.