You are here

public function MenuLinkParent::transform in Drupal 8

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

Find the parent link GUID.

Overrides ProcessPluginBase::transform

File

core/modules/migrate/src/Plugin/migrate/process/MenuLinkParent.php, line 123

Class

MenuLinkParent
This plugin figures out menu link parent plugin IDs.

Namespace

Drupal\migrate\Plugin\migrate\process

Code

public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
  $parent_id = array_shift($value);
  if (!$parent_id) {

    // Top level item.
    return '';
  }

  // This BC layer is included because if the plugin constructor was called
  // in the legacy way with a migration_lookup process plugin, it may have
  // been preconfigured with a different migration to look up against. While
  // this is unlikely, for maximum BC we will continue to use the plugin to do
  // the lookup if it is provided, and support for this will be removed in
  // Drupal 9.
  if ($this->migrationPlugin) {
    try {
      $already_migrated_id = $this->migrationPlugin
        ->transform($parent_id, $migrate_executable, $row, $destination_property);
    } catch (MigrateSkipRowException $e) {
    }
  }
  else {
    $lookup_result = $this->migrateLookup
      ->lookup($this->migration
      ->id(), [
      $parent_id,
    ]);
    if ($lookup_result) {
      $already_migrated_id = $lookup_result[0]['id'];
    }
  }
  if (!empty($already_migrated_id) && ($link = $this->menuLinkStorage
    ->load($already_migrated_id))) {
    return $link
      ->getPluginId();
  }
  if (isset($value[1])) {
    list($menu_name, $parent_link_path) = $value;
    $links = [];
    if (UrlHelper::isExternal($parent_link_path)) {
      $links = $this->menuLinkStorage
        ->loadByProperties([
        'link__uri' => $parent_link_path,
      ]);
    }
    else {
      $url = Url::fromUserInput("/{$parent_link_path}");
      if ($url
        ->isRouted()) {
        $links = $this->menuLinkManager
          ->loadLinksByRoute($url
          ->getRouteName(), $url
          ->getRouteParameters(), $menu_name);
      }
    }
    if (count($links) == 1) {

      /** @var \Drupal\Core\Menu\MenuLinkInterface $link */
      $link = reset($links);
      return $link
        ->getPluginId();
    }
  }
  throw new MigrateSkipRowException(sprintf("No parent link found for plid '%d' in menu '%s'.", $parent_id, $value[0]));
}