class MenuLinkParent

Same name in other branches
  1. 9 core/modules/migrate/src/Plugin/migrate/process/MenuLinkParent.php \Drupal\migrate\Plugin\migrate\process\MenuLinkParent
  2. 8.9.x core/modules/migrate/src/Plugin/migrate/process/MenuLinkParent.php \Drupal\migrate\Plugin\migrate\process\MenuLinkParent
  3. 10 core/modules/migrate/src/Plugin/migrate/process/MenuLinkParent.php \Drupal\migrate\Plugin\migrate\process\MenuLinkParent

Determines the parent of a menu link.

Menu link item belongs to a menu such as 'Navigation' or 'Administration'. Menu link item also has a parent item unless it is the root element of the menu.

This process plugin determines the parent item of a menu link. If the parent item can't be determined by ID, we try to determine it by a combination of menu name and parent link path.

The source is an array of three values:

  • parent_id: The numeric ID of the parent menu link, or 0 if the link is the root element of the menu.
  • menu_name: The name of the menu the menu link item belongs to.
  • parent_link_path: The Drupal path or external URL the parent of this menu link points to.

Example:


process:
  parent:
    plugin: menu_link_parent
    source:
      - plid
      - menu_name
      - parent_link_path

In this example, first look for a menu link that had an ID defined by 'plid' in the source (e.g., '20'). If that fails, try to determine the parent by a combination of a menu name (e.g., 'management') and a parent menu link path (e.g., 'admin/structure').

Hierarchy

Expanded class hierarchy of MenuLinkParent

See also

https://www.drupal.org/docs/8/api/menu-api

\Drupal\migrate\Plugin\MigrateProcessInterface

1 file declares its use of MenuLinkParent
MenuLinkParentTest.php in core/modules/migrate/tests/src/Unit/process/MenuLinkParentTest.php

File

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

Namespace

Drupal\migrate\Plugin\migrate\process
View source
class MenuLinkParent extends ProcessPluginBase implements ContainerFactoryPluginInterface {
    
    /**
     * The menu link plugin manager.
     *
     * @var \Drupal\Core\Menu\MenuLinkManagerInterface
     */
    protected $menuLinkManager;
    
    /**
     * The currently running migration.
     *
     * @var \Drupal\migrate\Plugin\MigrationInterface
     */
    protected $migration;
    
    /**
     * The migrate lookup service.
     *
     * @var \Drupal\migrate\MigrateLookupInterface
     */
    protected $migrateLookup;
    
    /**
     * The menu link entity storage handler.
     *
     * @var \Drupal\Core\Entity\EntityStorageInterface
     */
    protected $menuLinkStorage;
    
    /**
     * Constructs a MenuLinkParent object.
     *
     * @param array $configuration
     *   A configuration array containing information about the plugin instance.
     * @param string $plugin_id
     *   The plugin ID for the plugin instance.
     * @param mixed $plugin_definition
     *   The plugin implementation definition.
     * @param \Drupal\migrate\MigrateLookupInterface $migrate_lookup
     *   The migrate lookup service.
     * @param \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager
     *   The menu link manager.
     * @param \Drupal\Core\Entity\EntityStorageInterface $menu_link_storage
     *   The menu link storage object.
     * @param \Drupal\migrate\Plugin\MigrationInterface $migration
     *   The currently running migration.
     */
    public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrateLookupInterface $migrate_lookup, MenuLinkManagerInterface $menu_link_manager, EntityStorageInterface $menu_link_storage, MigrationInterface $migration) {
        parent::__construct($configuration, $plugin_id, $plugin_definition);
        $this->migration = $migration;
        $this->migrateLookup = $migrate_lookup;
        $this->menuLinkManager = $menu_link_manager;
        $this->menuLinkStorage = $menu_link_storage;
    }
    
    /**
     * {@inheritdoc}
     */
    public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, ?MigrationInterface $migration = NULL) {
        return new static($configuration, $plugin_id, $plugin_definition, $container->get('migrate.lookup'), $container->get('plugin.manager.menu.link'), $container->get('entity_type.manager')
            ->getStorage('menu_link_content'), $migration);
    }
    
    /**
     * {@inheritdoc}
     *
     * Find the parent link GUID.
     */
    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]));
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
MenuLinkParent::$menuLinkManager protected property The menu link plugin manager.
MenuLinkParent::$menuLinkStorage protected property The menu link entity storage handler.
MenuLinkParent::$migrateLookup protected property The migrate lookup service.
MenuLinkParent::$migration protected property The currently running migration.
MenuLinkParent::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
MenuLinkParent::transform public function Find the parent link GUID. Overrides ProcessPluginBase::transform
MenuLinkParent::__construct public function Constructs a MenuLinkParent object.
PluginInspectionInterface::getPluginDefinition public function Gets the definition of the plugin implementation. 6
PluginInspectionInterface::getPluginId public function Gets the plugin ID of the plugin instance. 2
ProcessPluginBase::$stopPipeline protected property Determines if processing of the pipeline is stopped.
ProcessPluginBase::isPipelineStopped public function Determines if the pipeline should stop processing. Overrides MigrateProcessInterface::isPipelineStopped
ProcessPluginBase::multiple public function Indicates whether the returned value requires multiple handling. Overrides MigrateProcessInterface::multiple 3
ProcessPluginBase::reset public function Resets the internal data of a plugin. Overrides MigrateProcessInterface::reset
ProcessPluginBase::stopPipeline protected function Stops pipeline processing after this plugin finishes.

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