You are here

public function PathFieldSerializer::onSerializeContentField in Acquia Content Hub 8.2

Manipulate the path properties.

Parameters

\Drupal\acquia_contenthub\Event\SerializeCdfEntityFieldEvent $event: The content entity field serialization event.

Overrides FallbackFieldSerializer::onSerializeContentField

File

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

Class

PathFieldSerializer
Serializes path fields.

Namespace

Drupal\acquia_contenthub\EventSubscriber\SerializeContentField

Code

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();
}