You are here

class PathFieldSerializer in Acquia Content Hub 8.2

Serializes path fields.

@package Drupal\acquia_contenthub\EventSubscriber\SerializeContentField

Hierarchy

  • class \Drupal\acquia_contenthub\EventSubscriber\SerializeContentField\FallbackFieldSerializer implements \Symfony\Component\EventDispatcher\EventSubscriberInterface uses ContentFieldMetadataTrait
    • class \Drupal\acquia_contenthub\EventSubscriber\SerializeContentField\PathFieldSerializer implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of PathFieldSerializer

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

File

src/EventSubscriber/SerializeContentField/PathFieldSerializer.php, line 16

Namespace

Drupal\acquia_contenthub\EventSubscriber\SerializeContentField
View source
class PathFieldSerializer extends FallbackFieldSerializer implements EventSubscriberInterface {

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

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

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

  /**
   * Manipulate the path properties.
   *
   * @param \Drupal\acquia_contenthub\Event\SerializeCdfEntityFieldEvent $event
   *   The content entity field serialization event.
   */
  public function onSerializeContentField(SerializeCdfEntityFieldEvent $event) {
    if ($event
      ->getEntity()
      ->getEntityTypeId() === 'path_alias' && $event
      ->getFieldName() === 'path') {
      parent::onSerializeContentField($event);
      $values = $event
        ->getFieldData();
      $langcode = $event
        ->getEntity()
        ->language()
        ->getId();
      $path = $values['value'][$langcode]['value'];
      $params = Url::fromUserInput($path)
        ->getRouteParameters();
      if ($params) {
        foreach ($params as $key => $value) {
          if ($this->manager
            ->hasDefinition($key)) {
            $entity = $this->manager
              ->getStorage($key)
              ->load($value);
            if ($path === "/{$entity->toUrl()->getInternalPath()}") {
              $values['value'][$langcode]['value'] = $entity
                ->uuid();
              $event
                ->setFieldData($values);
              $event
                ->stopPropagation();
              return;
            }
          }
        }
      }
    }
    if ('path' !== $event
      ->getField()
      ->getFieldDefinition()
      ->getType()) {
      return;
    }
    $event
      ->stopPropagation();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ContentFieldMetadataTrait::setFieldMetaData protected function Sets field metadata.
PathFieldSerializer::$manager protected property The entity type manager.
PathFieldSerializer::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to. Overrides FallbackFieldSerializer::getSubscribedEvents
PathFieldSerializer::onSerializeContentField public function Manipulate the path properties. Overrides FallbackFieldSerializer::onSerializeContentField
PathFieldSerializer::__construct public function PathFieldSerializer constructor.