Commit 86cc10b6 authored by webchick's avatar webchick

Issue #2408165 by phenaproxima, miguelc303, quietone, eliza411, tim.plunkett,...

Issue #2408165 by phenaproxima, miguelc303, quietone, eliza411, tim.plunkett, bdone, benjy: Migration Files for Drupal 7  Blocks
parent 333977ed
......@@ -3,10 +3,10 @@ label: Blocks
migration_tags:
- Drupal 6
source:
plugin: d6_block
plugin: block
process:
# Drupal 8 does not have a status but it doesn't matter; this is here to
# skip disabled blocks.
# Block status is not a thing in Drupal 8, so this is how we skip over
# disabled blocks.
status:
plugin: skip_on_empty
method: row
......@@ -50,15 +50,18 @@ process:
2: views_block:who_s_new-block_1
3: views_block:who_s_online-who_s_online_block
-
plugin: d6_block_plugin_id
plugin: block_plugin_id
-
plugin: skip_on_empty
method: row
theme:
plugin: d6_block_theme
plugin: block_theme
source:
- theme
- default_theme
- admin_theme
region:
plugin: d6_block_region
plugin: block_region
source:
- region
- theme
......@@ -74,17 +77,20 @@ process:
label: title
weight: weight
settings:
plugin: d6_block_settings
plugin: block_settings
source:
- '@plugin'
- delta
- settings
visibility:
plugin: d6_block_visibility
plugin: block_visibility
source:
- visibility
- pages
- roles
- visibility
# If the block uses PHP visibility, don't migrate it unless the PHP module
# is enabled.
skip_php: true
destination:
plugin: entity:block
migration_dependencies:
......
id: d7_block
label: Blocks
migration_tags:
- Drupal 7
source:
plugin: block
process:
# Block status is not a thing in Drupal 8, so this is how we skip over
# disabled blocks.
status:
plugin: skip_on_empty
method: row
source: status
id:
-
plugin: concat
source:
- theme
- module
- delta
delimiter: _
plugin:
-
plugin: static_map
bypass: true
source:
- module
- delta
map:
book:
navigation: book_navigation
comment:
recent: views_block:comments_recent-block_1
forum:
active: forum_active_block
new: forum_new_block
# locale:
# 0: language_block
node:
syndicate: node_syndicate_block
search:
form: search_form_block
statistics:
popular: statistics_popular_block
system:
main: system_main_block
'powered-by': system_powered_by_block
user:
login: user_login_block
# 1: system_menu_block:tools
new: views_block:who_s_new-block_1
online: views_block:who_s_online-who_s_online_block
-
plugin: block_plugin_id
-
plugin: skip_on_empty
method: row
theme:
plugin: block_theme
source:
- theme
- default_theme
- admin_theme
region:
plugin: block_region
source:
- region
- theme
- '@theme'
region_map:
left: sidebar_first
right: sidebar_second
sidebar_first: sidebar_first
sidebar_second: sidebar_second
help: help
header: header
footer: footer
label: title
weight: weight
settings:
plugin: block_settings
source:
- '@plugin'
- delta
- settings
visibility:
plugin: block_visibility
source:
- visibility
- pages
- roles
# If the block uses PHP visibility, don't migrate it unless the PHP module
# is enabled.
skip_php: true
destination:
plugin: entity:block
migration_dependencies:
optional:
- d7_custom_block
- d7_user_role
<?php
/**
* @file
* Contains \Drupal\block\Plugin\migrate\destination\EntityBlock.
*/
namespace Drupal\block\Plugin\migrate\destination;
use Drupal\migrate\Plugin\migrate\destination\EntityConfigBase;
use Drupal\migrate\Row;
/**
* @MigrateDestination(
* id = "entity:block"
* )
*/
class EntityBlock extends EntityConfigBase {
/**
* {@inheritdoc}
*/
protected function getEntityId(Row $row) {
// Try to find the block by its plugin ID and theme.
$properties = array(
'plugin' => $row->getDestinationProperty('plugin'),
'theme' => $row->getDestinationProperty('theme'),
);
$blocks = array_keys($this->storage->loadByProperties($properties));
return reset($blocks);
}
}
......@@ -2,34 +2,38 @@
/**
* @file
* Contains \Drupal\block\Plugin\migrate\process\d6\BlockPluginId.
* Contains \Drupal\block\Plugin\migrate\process\BlockPluginId.
*/
namespace Drupal\block\Plugin\migrate\process\d6;
namespace Drupal\block\Plugin\migrate\process;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\migrate\Entity\MigrationInterface;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\MigrateSkipRowException;
use Drupal\migrate\Plugin\MigratePluginManager;
use Drupal\migrate\Plugin\MigrateProcessInterface;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* @MigrateProcessPlugin(
* id = "d6_block_plugin_id"
* id = "block_plugin_id"
* )
*/
class BlockPluginId extends ProcessPluginBase implements ContainerFactoryPluginInterface {
/**
* @var \Drupal\migrate\Plugin\MigratePluginManager
* The migration process plugin, configured for lookups in d6_custom_block
* and d7_custom_block.
*
* @var \Drupal\migrate\Plugin\MigrateProcessInterface
*/
protected $processPluginManager;
protected $migrationPlugin;
/**
* The block_content entity storage handler.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $blockContentStorage;
......@@ -37,11 +41,10 @@ class BlockPluginId extends ProcessPluginBase implements ContainerFactoryPluginI
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, array $plugin_definition, MigrationInterface $migration, EntityStorageInterface $storage, MigratePluginManager $process_plugin_manager) {
public function __construct(array $configuration, $plugin_id, array $plugin_definition, EntityStorageInterface $storage, MigrateProcessInterface $migration_plugin) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->blockContentStorage = $storage;
$this->migration = $migration;
$this->processPluginManager = $process_plugin_manager;
$this->migrationPlugin = $migration_plugin;
}
/**
......@@ -49,13 +52,18 @@ public function __construct(array $configuration, $plugin_id, array $plugin_defi
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
$entity_manager = $container->get('entity.manager');
$migration_configuration = array(
'migration' => array(
'd6_custom_block',
'd7_custom_block',
),
);
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$migration,
$entity_manager->getDefinition('block_content') ? $entity_manager->getStorage('block_content') : NULL,
$container->get('plugin.manager.migrate.process')
$container->get('plugin.manager.migrate.process')->createInstance('migration', $migration_configuration, $migration)
);
}
......@@ -70,31 +78,28 @@ public function transform($value, MigrateExecutableInterface $migrate_executable
switch ($module) {
case 'aggregator':
list($type, $id) = explode('-', $delta);
if ($type == 'category') {
// @TODO skip row.
// throw new MigrateSkipRowException();
if ($type == 'feed') {
return 'aggregator_feed_block';
}
$value = 'aggregator_feed_block';
break;
case 'menu':
$value = "system_menu_block:$delta";
break;
return "system_menu_block:$delta";
case 'block':
if ($this->blockContentStorage) {
$block_ids = $this->processPluginManager
->createInstance('migration', array('migration' => 'd6_custom_block'), $this->migration)
$block_id = $this->migrationPlugin
->transform($delta, $migrate_executable, $row, $destination_property);
$value = 'block_content:' . $this->blockContentStorage->load($block_ids[0])->uuid();
}
else {
throw new MigrateSkipRowException();
if ($block_id) {
return 'block_content:' . $this->blockContentStorage->load($block_id)->uuid();
}
}
break;
default:
throw new MigrateSkipRowException();
break;
}
}
return $value;
else {
return $value;
}
}
}
......@@ -2,10 +2,10 @@
/**
* @file
* Contains \Drupal\block\Plugin\migrate\process\d6\BlockRegion.
* Contains \Drupal\block\Plugin\migrate\process\BlockRegion.
*/
namespace Drupal\block\Plugin\migrate\process\d6;
namespace Drupal\block\Plugin\migrate\process;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\ProcessPluginBase;
......@@ -13,10 +13,11 @@
/**
* @MigrateProcessPlugin(
* id = "d6_block_region"
* id = "block_region"
* )
*/
class BlockRegion extends ProcessPluginBase {
/**
* {@inheritdoc}
*
......
......@@ -2,10 +2,10 @@
/**
* @file
* Contains \Drupal\block\Plugin\migrate\process\d6\BlockSettings.
* Contains \Drupal\block\Plugin\migrate\process\BlockSettings.
*/
namespace Drupal\block\Plugin\migrate\process\d6;
namespace Drupal\block\Plugin\migrate\process;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\ProcessPluginBase;
......@@ -13,7 +13,7 @@
/**
* @MigrateProcessPlugin(
* id = "d6_block_settings"
* id = "block_settings"
* )
*/
class BlockSettings extends ProcessPluginBase {
......
......@@ -2,10 +2,10 @@
/**
* @file
* Contains \Drupal\block\Plugin\migrate\process\d6\BlockTheme.
* Contains \Drupal\block\Plugin\migrate\process\BlockTheme.
*/
namespace Drupal\block\Plugin\migrate\process\d6;
namespace Drupal\block\Plugin\migrate\process;
use Drupal\migrate\Entity\MigrationInterface;
use Drupal\migrate\MigrateExecutableInterface;
......@@ -17,7 +17,7 @@
/**
* @MigrateProcessPlugin(
* id = "d6_block_theme"
* id = "block_theme"
* )
*/
class BlockTheme extends ProcessPluginBase implements ContainerFactoryPluginInterface {
......@@ -74,11 +74,9 @@ public static function create(ContainerInterface $container, array $configuratio
/**
* {@inheritdoc}
*
* Set the block theme, based on the current default theme.
*/
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
list($theme, $d6_default_theme, $d6_admin_theme) = $value;
list($theme, $default_theme, $admin_theme) = $value;
// If the source theme exists on the destination, we're good.
if (isset($this->themes[$theme])) {
......@@ -87,13 +85,13 @@ public function transform($value, MigrateExecutableInterface $migrate_executable
// If the source block is assigned to a region in the source default theme,
// then assign it to the destination default theme.
if (strtolower($theme) == strtolower($d6_default_theme)) {
if (strtolower($theme) == strtolower($default_theme)) {
return $this->themeConfig->get('default');
}
// If the source block is assigned to a region in the source admin theme,
// then assign it to the destination admin theme.
if (strtolower($theme) == strtolower($d6_admin_theme)) {
if (strtolower($theme) == strtolower($admin_theme)) {
return $this->themeConfig->get('admin');
}
......
......@@ -2,77 +2,140 @@
/**
* @file
* Contains \Drupal\block\Plugin\migrate\process\d6\BlockVisibility.
* Contains \Drupal\block\Plugin\migrate\process\BlockVisibility.
*/
namespace Drupal\block\Plugin\migrate\process\d6;
namespace Drupal\block\Plugin\migrate\process;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\migrate\Entity\MigrationInterface;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\MigrateSkipRowException;
use Drupal\migrate\Plugin\MigrateProcessInterface;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;
use Drupal\migrate\Entity\MigrationInterface;
use Drupal\migrate\Plugin\MigrateProcessInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* @MigrateProcessPlugin(
* id = "d6_block_visibility"
* id = "block_visibility"
* )
*/
class BlockVisibility extends ProcessPluginBase implements ContainerFactoryPluginInterface {
/**
* The migration plugin.
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* The migration process plugin, configured for lookups in the d6_user_role
* and d7_user_role migrations.
*
* @var \Drupal\migrate\Plugin\MigrateProcessInterface
*/
protected $migrationPlugin;
/**
* Whether or not to skip blocks that use PHP for visibility. Only applies
* if the PHP module is not enabled.
*
* @var bool
*/
protected $skipPHP = FALSE;
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, MigrateProcessInterface $migration_plugin) {
public function __construct(array $configuration, $plugin_id, $plugin_definition, ModuleHandlerInterface $module_handler, MigrateProcessInterface $migration_plugin) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->migration = $migration;
$this->moduleHandler = $module_handler;
$this->migrationPlugin = $migration_plugin;
if (isset($configuration['skip_php'])) {
$this->skipPHP = $configuration['skip_php'];
}
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
$migration_configuration = array(
'migration' => array(
'd6_user_role',
'd7_user_role',
),
);
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$migration,
$container->get('plugin.manager.migrate.process')->createInstance('migration', array('migration' => 'd6_user_role'), $migration)
$container->get('module_handler'),
$container->get('plugin.manager.migrate.process')->createInstance('migration', $migration_configuration, $migration)
);
}
/**
* {@inheritdoc}
*
* Set the block visibility settings.
*/
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
list($pages, $roles, $old_visibility) = $value;
list($old_visibility, $pages, $roles) = $value;
$visibility = array();
if (!empty($pages)) {
$visibility['request_path']['pages'] = $pages;
$visibility['request_path']['id'] = 'request_path';
$visibility['request_path']['negate'] = !$old_visibility;
}
// If the block is assigned to specific roles, add the user_role condition.
if ($roles) {
$visibility['user_role'] = array(
'id' => 'user_role',
'roles' => array(),
'context_mapping' => array(
'user' => '@user.current_user_context:current_user',
),
'negate' => FALSE,
);
if (!empty($roles)) {
foreach ($roles as $key => $role_id) {
$new_role = $this->migrationPlugin->transform($role_id, $migrate_executable, $row, $destination_property);
$visibility['user_role']['roles'][$new_role] = $new_role;
$roles[$key] = $this->migrationPlugin->transform($role_id, $migrate_executable, $row, $destination_property);
}
$visibility['user_role']['roles'] = array_combine($roles, $roles);
}
if ($pages) {
// 2 == BLOCK_VISIBILITY_PHP in Drupal 6 and 7.
if ($old_visibility == 2) {
// If the PHP module is present, migrate the visibility code unaltered.
if ($this->moduleHandler->moduleExists('php')) {
$visibility['php'] = array(
'id' => 'php',
// PHP code visibility could not be negated in Drupal 6 or 7.
'negate' => FALSE,
'php' => $pages,
);
}
// Skip the row if we're configured to. If not, we don't need to do
// anything else -- the block will simply have no PHP or request_path
// visibility configuration.
elseif ($this->skipPHP) {
throw new MigrateSkipRowException();
}
}
else {
$paths = preg_split("(\r\n?|\n)", $pages);
foreach ($paths as $key => $path) {
$paths[$key] = $path === '<front>' ? $path : '/' . ltrim($path, '/');
}
$visibility['request_path'] = array(
'id' => 'request_path',
'negate' => !$old_visibility,
'pages' => implode("\n", $paths),
);
}
$visibility['user_role']['id'] = 'user_role';
$visibility['user_role']['context_mapping']['user'] = 'user.current_user';
}
return $visibility;
}
......
......@@ -2,22 +2,24 @@
/**
* @file
* Contains \Drupal\block\Plugin\migrate\source\d6\Block.
* Contains \Drupal\block\Plugin\migrate\source\Block.
*/
namespace Drupal\block\Plugin\migrate\source\d6;
namespace Drupal\block\Plugin\migrate\source;
use Drupal\migrate\Row;
use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
/**
* Drupal 6 block source from database.
* Drupal block source from database.
*
* @MigrateSource(
* id = "d6_block"
* id = "block",
* source_provider = "block"
* )
*/
class Block extends DrupalSqlBase {
/**
* The default theme name.
*
......@@ -32,14 +34,33 @@ class Block extends DrupalSqlBase {
*/
protected $adminTheme;
/**
* Table containing block configuration.
*
* @var string
*/
protected $blockTable;
/**
* Table mapping blocks to user roles.
*
* @var string
*/
protected $blockRoleTable;
/**
* {@inheritdoc}
*/
public function query() {
$query = $this->select('blocks', 'b')
->fields('b', array('bid', 'module', 'delta', 'theme', 'status', 'weight', 'region', 'visibility', 'pages', 'title', 'cache'))
->orderBy('bid');
return $query;
if ($this->getModuleSchemaVersion('system') >= 7000) {
$this->blockTable = 'block';
$this->blockRoleTable = 'block_role';
}
else {
$this->blockTable = 'blocks';
$this->blockRoleTable = 'blocks_roles';
}
return $this->select($this->blockTable, 'b')->fields('b');
}
/**
......@@ -67,36 +88,54 @@ public function fields() {
'pages' => $this->t('Pages list.'),
'title' => $this->t('Block title.'),
'cache' => $this->t('Cache rule.'),
);
}
/**
* {@inheritdoc}
*/
public function getIds() {
$ids['module']['type'] = 'string';
$ids['delta']['type'] = 'string';
$ids['theme']['type'] = 'string';
return $ids;
}
/**
* {@inheritdoc}
*/
public function prepareRow(Row $row) {
$row->setSourceProperty('default_theme', $this->defaultTheme);
$row->setSourceProperty('admin_theme', $this->adminTheme);
$module = $row->getSourceProperty('module');
$delta = $row->getSourceProperty('delta');
$roles = $this->select('blocks_roles', 'br')
$roles = $this->select($this->blockRoleTable, 'br')
->fields('br', array('rid'))
->condition('module', $module)
->condition('delta', $delta)
->execute()
->fetchCol();
$row->setSourceProperty('roles', $roles);
$settings = array();
// Contrib can use hook_migration_d6_block_prepare_row() to add similar
// variables via $migration->getSource()->variableGet().
switch ($module) {
case 'aggregator':
list($type, $id) = explode('-', $delta);
if ($type == 'feed') {
$item_count = $this->database->query('SELECT block FROM {aggregator_feed} WHERE fid = :fid', array(':fid' => $id))->fetchField();
$item_count = $this->select('aggregator_feed', 'af')
->fields('af', ['block'])
->condition('fid', $id)
->execute()
->fetchField();
}
else {
$item_count = $this->database->query('SELECT block FROM {aggregator_category} WHERE cid = :cid', array(':cid' => $id))->fetchField();
$item_count = $this->select('aggregator_category', 'ac')
->fields('ac', ['block'])
->condition('cid', $id)
->execute()
->fetchField();
}
$settings['aggregator']['item_count'] = $item_count;
break;
......@@ -114,9 +153,11 @@ public function prepareRow(Row $row) {
case 'user':