function MenuLinkParent::transform

Same name in other branches
  1. 9 core/modules/migrate/src/Plugin/migrate/process/MenuLinkParent.php \Drupal\migrate\Plugin\migrate\process\MenuLinkParent::transform()
  2. 8.9.x core/modules/migrate/src/Plugin/migrate/process/MenuLinkParent.php \Drupal\migrate\Plugin\migrate\process\MenuLinkParent::transform()
  3. 10 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 136

Class

MenuLinkParent
Determines the parent of a menu link.

Namespace

Drupal\migrate\Plugin\migrate\process

Code

public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
    $parent_id = array_shift($value);
    // Handle root elements of a menu.
    if (!$parent_id) {
        return '';
    }
    $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();
    }
    // Parent could not be determined by ID, so we try to determine by the
    // combination of the menu name and parent link path.
    if (isset($value[1])) {
        [
            $menu_name,
            $parent_link_path,
        ] = $value;
        // If the parent link path is external, URL will be useless because the
        // link will definitely not correspond to a Drupal route.
        if (UrlHelper::isExternal($parent_link_path)) {
            $links = $this->menuLinkStorage
                ->loadByProperties([
                'menu_name' => $menu_name,
                'link.uri' => $parent_link_path,
            ]);
        }
        else {
            $url = Url::fromUserInput('/' . ltrim($parent_link_path, '/'));
            if ($url->isRouted()) {
                $links = $this->menuLinkManager
                    ->loadLinksByRoute($url->getRouteName(), $url->getRouteParameters(), $menu_name);
            }
        }
        if (!empty($links)) {
            return reset($links)->getPluginId();
        }
    }
    // Parent could not be determined.
    throw new MigrateSkipRowException(sprintf("No parent link found for plid '%d' in menu '%s'.", $parent_id, $value[0]));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.