You are here

protected function ContentImporter::alterMenuLink in Content Synchronization 3.0.x

Same name and namespace in other branches
  1. 8.2 src/Importer/ContentImporter.php \Drupal\content_sync\Importer\ContentImporter::alterMenuLink()

Replaces a link to a node with an actual one.

Parameters

array $decoded_entity: Array of entity values.

Return value

array Array of entity values with the link values changed.

1 call to ContentImporter::alterMenuLink()
ContentImporter::importEntity in src/Importer/ContentImporter.php

File

src/Importer/ContentImporter.php, line 146

Class

ContentImporter

Namespace

Drupal\content_sync\Importer

Code

protected function alterMenuLink(array $decoded_entity) {
  $referenced_entity_uuid = reset($decoded_entity["_content_sync"]["menu_entity_link"]);
  $referenced_entity_type = key($decoded_entity["_content_sync"]["menu_entity_link"]);
  if (!preg_match('/^internal:/', $decoded_entity["link"][0]["uri"]) && ($referenced_entity = \Drupal::service('entity.repository')
    ->loadEntityByUuid($referenced_entity_type, $referenced_entity_uuid))) {
    $url = $referenced_entity
      ->toUrl();

    // Convert entity URIs to the entity scheme, if the path matches a route
    // of the form "entity.$entity_type_id.canonical".
    // @see \Drupal\Core\Url::fromEntityUri()
    if ($url
      ->isRouted()) {
      $route_name = $url
        ->getRouteName();
      foreach (array_keys($this->entityTypeManager
        ->getDefinitions()) as $entity_type_id) {
        if ($route_name == "entity.{$entity_type_id}.canonical" && isset($url
          ->getRouteParameters()[$entity_type_id])) {
          $uri = "entity:{$entity_type_id}/" . $url
            ->getRouteParameters()[$entity_type_id];
        }
      }
    }
    else {

      //$uri = $url->toUriString();
      $uri = $url
        ->getUri();
    }
    $decoded_entity["link"][0]["uri"] = $uri;
  }
  return $decoded_entity;
}