You are here

public function LinkUri::transform in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/menu_link_content/src/Plugin/migrate/process/LinkUri.php \Drupal\menu_link_content\Plugin\migrate\process\LinkUri::transform()

Performs the associated process.

Parameters

mixed $value: The value to be transformed.

\Drupal\migrate\MigrateExecutableInterface $migrate_executable: The migration in which this process is being executed.

\Drupal\migrate\Row $row: The row from the source to process. Normally, just transforming the value is adequate but very rarely you might need to change two columns at the same time or something like that.

string $destination_property: The destination property currently worked on. This is only used together with the $row above.

Return value

string|array The newly transformed value.

Overrides ProcessPluginBase::transform

File

core/modules/menu_link_content/src/Plugin/migrate/process/LinkUri.php, line 90

Class

LinkUri
Generates an internal URI from the source value.

Namespace

Drupal\menu_link_content\Plugin\migrate\process

Code

public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
  if (is_array($value)) {
    $value = reset($value);
    @trigger_error('Passing an array as source value into the link_uri migrate process plugin is deprecated in drupal:8.8.0. The possibility to pass an array as source value to the plugin will be removed in drupal:9.0.0. Pass a string value instead. See https://www.drupal.org/node/3043694', E_USER_DEPRECATED);
  }
  $path = ltrim($value, '/');
  if (parse_url($path, PHP_URL_SCHEME) === NULL) {
    if ($path == '<front>') {
      $path = '';
    }
    elseif ($path == '<nolink>') {
      return 'route:<nolink>';
    }
    $path = 'internal:/' . $path;

    // 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()
    $url = Url::fromUri($path);
    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])) {
          return "entity:{$entity_type_id}/" . $url
            ->getRouteParameters()[$entity_type_id];
        }
      }
    }
    else {

      // If the URL is not routed, we might want to get something back to do
      // other processing. If this is the case, the "validate_route"
      // configuration option can be set to FALSE to return the URI.
      if (!$this->configuration['validate_route']) {
        return $url
          ->getUri();
      }
      else {
        throw new MigrateException(sprintf('The path "%s" failed validation.', $path));
      }
    }
  }
  return $path;
}