Commit 92c25141 authored by alexpott's avatar alexpott

Issue #2178703 by ultimike, chx, penyaskito: Migrate D6 menu links.

parent f1158cf2
......@@ -82,6 +82,20 @@ public function isValid($path) {
* {@inheritdoc}
*/
public function getUrlIfValid($path) {
return $this->getUrl($path, TRUE);
}
/**
* {@inheritdoc}
*/
public function getUrlIfValidWithoutAccessCheck($path) {
return $this->getUrl($path, FALSE);
}
/**
* Helper for getUrlIfValid() and getUrlIfValidWithoutAccessCheck().
*/
protected function getUrl($path, $access_check) {
$parsed_url = UrlHelper::parse($path);
$options = [];
......@@ -104,7 +118,7 @@ public function getUrlIfValid($path) {
$path = ltrim($path, '/');
$request = Request::create('/' . $path);
$attributes = $this->getPathAttributes($path, $request);
$attributes = $this->getPathAttributes($path, $request, $access_check);
if (!$attributes) {
return FALSE;
......@@ -123,12 +137,15 @@ public function getUrlIfValid($path) {
* The path to check.
* @param \Symfony\Component\HttpFoundation\Request $request
* A request object with the given path.
* @param bool $access_check
* If FALSE then skip access check and check only whether the path is
* valid.
*
* @return array|bool
* An array of request attributes of FALSE if an exception was thrown.
*/
protected function getPathAttributes($path, Request $request) {
if ($this->account->hasPermission('link to any page')) {
protected function getPathAttributes($path, Request $request, $access_check) {
if (!$access_check || $this->account->hasPermission('link to any page')) {
$router = $this->accessUnawareRouter;
}
else {
......
......@@ -23,6 +23,20 @@ interface PathValidatorInterface {
*/
public function getUrlIfValid($path);
/**
* Returns an URL object, if the path is valid.
*
* Unlike getUrlIfValid(), access check is not performed. Do not use this
* method if the $path is about to be presented to a user.
*
* @param string $path
* The path to check.
*
* @return \Drupal\Core\Url|false
* The url object, or FALSE if the path is not valid.
*/
public function getUrlIfValidWithoutAccessCheck($path);
/**
* Checks if the URL path is valid and accessible by the current user.
*
......
<?php
/**
* @file
* Contains \Drupal\migrate_drupal\Plugin\migrate\process\d6\Route.
*/
namespace Drupal\migrate\Plugin\migrate\process;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\migrate\Entity\MigrationInterface;
use Drupal\Core\Path\PathValidatorInterface;
use Drupal\migrate\MigrateExecutable;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;
/**
* @MigrateProcessPlugin(
* id = "route"
* )
*/
class Route extends ProcessPluginBase implements ContainerFactoryPluginInterface {
/**
* @var \Drupal\Core\Path\PathValidatorInterface
*/
protected $pathValidator;
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, PathValidatorInterface $pathValidator) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->migration = $migration;
$this->pathValidator = $pathValidator;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$migration,
$container->get('path.validator')
);
}
/**
* {@inheritdoc}
*
* Set the destination route information based on the source link_path.
*/
public function transform($value, MigrateExecutable $migrate_executable, Row $row, $destination_property) {
list($link_path, $options) = $value;
$extracted = $this->pathValidator->getUrlIfValidWithoutAccessCheck($link_path);
$route = array();
if ($extracted) {
if ($extracted->isExternal()) {
$route['route_name'] = null;
$route['route_parameters'] = array();
$route['options'] = $options;
$route['url'] = $extracted->getUri();
}
else {
$route['route_name'] = $extracted->getRouteName();
$route['route_parameters'] = $extracted->getRouteParameters();
$route['options'] = $extracted->getOptions();
if (isset($options['query'])) {
// If the querystring is stored as a string (as in D6), convert it
// into an array.
if (is_string($options['query'])) {
parse_str($options['query'], $old_query);
}
else {
$old_query = $options['query'];
}
$options['query'] = $route['options']['query'] + $old_query;
unset($route['options']['query']);
}
$route['options'] = $route['options'] + $options;
$route['url'] = null;
}
}
return $route;
}
}
id: d6_menu_links
label: Drupal 6 menu links
migration_groups:
- Drupal 6
source:
plugin: d6_menu_link
constants:
bundle: menu_link_content
process:
id: mlid
bundle: 'constants/bundle'
title: link_title
description:
plugin: extract
source:
- options
index:
- 0
- attributes
- title
menu_name:
plugin: migration
migration: d6_menu
source: menu_name
route:
plugin: route
source:
- link_path
- options
route_name: @route/route_name
route_parameters: @route/route_parameters
url: @route/url
options: @route/options
external: external
weight: weight
expanded: expanded
enabled: enabled
parent:
-
plugin: skip_process_on_empty
source: plid
-
plugin: migration
migration: d6_menu_links
changed: updated
destination:
plugin: entity:menu_link_content
migration_dependencies:
required:
- d6_menu
......@@ -64,6 +64,14 @@ migrate.source.d6_comment_entity_form_display_subject:
type: migrate_entity_constant
label: 'Constants'
migrate.source.d6_menu_link:
type: migrate_source_sql
label: 'Drupal 6 menu link'
mapping:
constants:
type: migrate_entity_constant
label: 'Constants'
migrate.source.d6_box:
type: migrate_source_sql
label: 'Drupal 6 box'
......
<?php
/**
* @file
* Contains \Drupal\migrate_drupal\Plugin\migrate\source\d6\MenuLink.
*/
namespace Drupal\migrate_drupal\Plugin\migrate\source\d6;
use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
use Drupal\migrate\Row;
/**
* Drupal 6 menu link source from database.
*
* @MigrateSource(
* id = "d6_menu_link",
* )
*/
class MenuLink extends DrupalSqlBase {
/**
* {@inheritdoc}
*/
public function query() {
$query = $this->select('menu_links', 'ml')
->fields('ml', array(
'menu_name',
'mlid',
'plid',
'link_path',
'router_path',
'link_title',
'options',
'module',
'hidden',
'external',
'has_children',
'expanded',
'weight',
'depth',
'customized',
'p1',
'p2',
'p3',
'p4',
'p5',
'p6',
'p7',
'p8',
'p9',
'updated'
))
->condition('module', 'menu')
->condition('customized', 1);
return $query;
}
/**
* {@inheritdoc}
*/
public function fields() {
return array(
'menu_name' => t("The menu name. All links with the same menu name (such as 'navigation') are part of the same menu."),
'mlid' => t('The menu link ID (mlid) is the integer primary key.'),
'plid' => t('The parent link ID (plid) is the mlid of the link above in the hierarchy, or zero if the link is at the top level in its menu.'),
'link_path' => t('The Drupal path or external path this link points to.'),
'router_path' => t('For links corresponding to a Drupal path (external = 0), this connects the link to a {menu_router}.path for joins.'),
'link_title' => t('The text displayed for the link, which may be modified by a title callback stored in {menu_router}.'),
'options' => t('A serialized array of options to be passed to the url() or l() function, such as a query string or HTML attributes.'),
'module' => t('The name of the module that generated this link.'),
'hidden' => t('A flag for whether the link should be rendered in menus. (1 = a disabled menu item that may be shown on admin screens, -1 = a menu callback, 0 = a normal, visible link)'),
'external' => t('A flag to indicate if the link points to a full URL starting with a protocol, like http:// (1 = external, 0 = internal).'),
'has_children' => t('Flag indicating whether any links have this link as a parent (1 = children exist, 0 = no children).'),
'expanded' => t('Flag for whether this link should be rendered as expanded in menus - expanded links always have their child links displayed, instead of only when the link is in the active trail (1 = expanded, 0 = not expanded)'),
'weight' => t('Link weight among links in the same menu at the same depth.'),
'depth' => t('The depth relative to the top level. A link with plid == 0 will have depth == 1.'),
'customized' => t('A flag to indicate that the user has manually created or edited the link (1 = customized, 0 = not customized).'),
'p1' => t('The first mlid in the materialized path. If N = depth, then pN must equal the mlid. If depth > 1 then p(N-1) must equal the plid. All pX where X > depth must equal zero. The columns p1 .. p9 are also called the parents.'),
'p2' => t('The second mlid in the materialized path. See p1.'),
'p3' => t('The third mlid in the materialized path. See p1.'),
'p4' => t('The fourth mlid in the materialized path. See p1.'),
'p5' => t('The fifth mlid in the materialized path. See p1.'),
'p6' => t('The sixth mlid in the materialized path. See p1.'),
'p7' => t('The seventh mlid in the materialized path. See p1.'),
'p8' => t('The eighth mlid in the materialized path. See p1.'),
'p9' => t('The ninth mlid in the materialized path. See p1.'),
'updated' => t('Flag that indicates that this link was generated during the update from Drupal 5.'),
);
}
/**
* {@inheritdoc}
*/
public function prepareRow(Row $row) {
$row->setSourceProperty('options', unserialize($row->getSourceProperty('options')));
$row->setSourceProperty('enabled', !$row->getSourceProperty('hidden'));
return parent::prepareRow($row);
}
/**
* {@inheritdoc}
*/
public function getIds() {
$ids['mlid']['type'] = 'integer';
return $ids;
}
}
<?php
/**
* @file
* Contains \Drupal\migrate_drupal\Tests\Dump\Drupal6MenuLink.
*/
namespace Drupal\migrate_drupal\Tests\Dump;
/**
* Database dump for testing the menu link migration.
*/
class Drupal6MenuLink extends Drupal6DumpBase {
/**
* {@inheritdoc}
*/
public function load() {
$this->createTable('menu_links', array(
'description' => 'Contains the individual links within a menu.',
'fields' => array(
'menu_name' => array(
'description' => "The menu name. All links with the same menu name (such as 'navigation') are part of the same menu.",
'type' => 'varchar',
'length' => 32,
'not null' => TRUE,
'default' => ''),
'mlid' => array(
'description' => 'The menu link ID (mlid) is the integer primary key.',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE),
'plid' => array(
'description' => 'The parent link ID (plid) is the mlid of the link above in the hierarchy, or zero if the link is at the top level in its menu.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0),
'link_path' => array(
'description' => 'The Drupal path or external path this link points to.',
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => ''),
'router_path' => array(
'description' => 'For links corresponding to a Drupal path (external = 0), this connects the link to a {menu_router}.path for joins.',
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => ''),
'link_title' => array(
'description' => 'The text displayed for the link, which may be modified by a title callback stored in {menu_router}.',
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => ''),
'options' => array(
'description' => 'A serialized array of options to be passed to the url() or l() function, such as a query string or HTML attributes.',
'type' => 'text',
'not null' => FALSE),
'module' => array(
'description' => 'The name of the module that generated this link.',
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => 'system'),
'hidden' => array(
'description' => 'A flag for whether the link should be rendered in menus. (1 = a disabled menu item that may be shown on admin screens, -1 = a menu callback, 0 = a normal, visible link)',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'size' => 'small'),
'external' => array(
'description' => 'A flag to indicate if the link points to a full URL starting with a protocol, like http:// (1 = external, 0 = internal).',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'size' => 'small'),
'has_children' => array(
'description' => 'Flag indicating whether any links have this link as a parent (1 = children exist, 0 = no children).',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'size' => 'small'),
'expanded' => array(
'description' => 'Flag for whether this link should be rendered as expanded in menus - expanded links always have their child links displayed, instead of only when the link is in the active trail (1 = expanded, 0 = not expanded)',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'size' => 'small'),
'weight' => array(
'description' => 'Link weight among links in the same menu at the same depth.',
'type' => 'int',
'not null' => TRUE,
'default' => 0),
'depth' => array(
'description' => 'The depth relative to the top level. A link with plid == 0 will have depth == 1.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'size' => 'small'),
'customized' => array(
'description' => 'A flag to indicate that the user has manually created or edited the link (1 = customized, 0 = not customized).',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'size' => 'small'),
'p1' => array(
'description' => 'The first mlid in the materialized path. If N = depth, then pN must equal the mlid. If depth > 1 then p(N-1) must equal the plid. All pX where X > depth must equal zero. The columns p1 .. p9 are also called the parents.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0),
'p2' => array(
'description' => 'The second mlid in the materialized path. See p1.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0),
'p3' => array(
'description' => 'The third mlid in the materialized path. See p1.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0),
'p4' => array(
'description' => 'The fourth mlid in the materialized path. See p1.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0),
'p5' => array(
'description' => 'The fifth mlid in the materialized path. See p1.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0),
'p6' => array(
'description' => 'The sixth mlid in the materialized path. See p1.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0),
'p7' => array(
'description' => 'The seventh mlid in the materialized path. See p1.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0),
'p8' => array(
'description' => 'The eighth mlid in the materialized path. See p1.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0),
'p9' => array(
'description' => 'The ninth mlid in the materialized path. See p1.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0),
'updated' => array(
'description' => 'Flag that indicates that this link was generated during the update from Drupal 5.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'size' => 'small'),
),
'indexes' => array(
'path_menu' => array(array('link_path', 128), 'menu_name'),
'menu_plid_expand_child' => array(
'menu_name', 'plid', 'expanded', 'has_children'),
'menu_parents' => array(
'menu_name', 'p1', 'p2', 'p3', 'p4', 'p5', 'p6', 'p7', 'p8', 'p9'),
'router_path' => array(array('router_path', 128)),
),
'primary key' => array('mlid'),
));
$this->database->insert('menu_links')->fields(array(
'menu_name',
'mlid',
'plid',
'link_path',
'router_path',
'link_title',
'options',
'module',
'hidden',
'external',
'has_children',
'expanded',
'weight',
'depth',
'customized',
'p1',
'p2',
'p3',
'p4',
'p5',
'p6',
'p7',
'p8',
'p9',
'updated',
))
->values(array(
'menu_name' => 'secondary-links',
'mlid' => 138,
'plid' => 0,
'link_path' => 'user/login',
'router_path' => 'user/login',
'link_title' => 'Test 1',
'options' => 'a:1:{s:10:"attributes";a:1:{s:5:"title";s:16:"Test menu link 1";}}',
'module' => 'menu',
'hidden' => 0,
'external' => 0,
'has_children' => 1,
'expanded' => 0,
'weight' => 15,
'depth' => 1,
'customized' => 1,
'p1' => '138',
'p2' => '0',
'p3' => '0',
'p4' => '0',
'p5' => '0',
'p6' => '0',
'p7' => '0',
'p8' => '0',
'p9' => '0',
'updated' => '0',
))
->values(array(
'menu_name' => 'secondary-links',
'mlid' => 139,
'plid' => 138,
'link_path' => 'admin',
'router_path' => 'admin',
'link_title' => 'Test 2',
'options' => 'a:2:{s:5:"query";s:7:"foo=bar";s:10:"attributes";a:1:{s:5:"title";s:16:"Test menu link 2";}}',
'module' => 'menu',
'hidden' => 0,
'external' => 0,
'has_children' => 0,
'expanded' => 1,
'weight' => 12,
'depth' => 2,
'customized' => 1,
'p1' => '138',
'p2' => '139',
'p3' => '0',
'p4' => '0',