diff --git a/core/includes/update.inc b/core/includes/update.inc index 3ee24ab..25c7790 100644 --- a/core/includes/update.inc +++ b/core/includes/update.inc @@ -1680,3 +1680,99 @@ function update_language_list($flags = Language::STATE_CONFIGURABLE) { return $filtered_languages; } + +/** + * Create block config entity from properties passed in by module. + * + * @param $block_name + * The full name of the block including block prefix (block.block). + * @param $module + * The module name that is stored in the {block} table. The block entity will + * use the module name by default if not provided in $properties. + * @param $delta + * The block delta that is stored in the {block} table. + * @param $properties + * A key-value array of block entity properties. The id, region, weight, + * status, and plugin keys are required. A module should provide additional + * properties as necessary, but must provide all values for complex + * properties such as settings and visibility. + * + * @see comment_update_8005(). + * + * @return Drupal\Core\Config\Config + * The configuration object. + */ +function update_block_to_config($block_name, $module, $delta, $properties) { + $config = config($block_name); + $node_types = _update_7000_node_get_types(); + + // Set default properties. + $defaults = array( + 'id' => '', + 'uuid' => '', + 'region' => '', + 'weight' => 0, + 'status' => 0, + 'visibility' => array( + 'path' => array( + 'visibility' => 0, + 'pages' => '', + ), + 'role' => array( + 'roles' => array(), + ), + 'node_type' => array( + 'types' => ($node_types) ? array_reverse(array_keys($node_types)) : array(), + ), + 'visibility__active_tab' => 'edit-visibility-path', + ), + 'plugin' => '', + 'settings' => array( + 'label' => '', + 'label_display' => 'visible', + 'module' => $module, + 'cache' => -1, + ), + ); + + $properties += $defaults; + + // Get visible node types. + $types = db_select('block_node_type') + ->fields('block_node_type') + ->condition('module', $module) + ->condition('delta', $delta) + ->execute() + ->fetchAllKeyed(0, 0); + + if (!empty($types)) { + foreach ($types as $type_name) { + $properties['visibility']['node_type']['types'][$type_name] = $type_name; + } + } + + // Get visible roles. + $roles = db_select('block_role') + ->fields('block_role') + ->condition('module', $module) + ->condition('delta', $delta) + ->execute() + ->fetchAllKeyed(0, 0); + + if (!empty($roles)) { + foreach ($roles as $rid) { + $properties['visibility']['roles']['role'][] = $rid; + } + } + + // Config entity is new, need id. + if ($config->isNew()) { + $uuid = new UUID(); + $properties['uuid'] = $uuid->generate(); + } + + // Set entity properties. + $config->setData($properties); + + return $config->save(); +} diff --git a/core/modules/aggregator/aggregator.install b/core/modules/aggregator/aggregator.install index 018d9d1..18baf66 100644 --- a/core/modules/aggregator/aggregator.install +++ b/core/modules/aggregator/aggregator.install @@ -330,3 +330,77 @@ function aggregator_update_8001() { 'initial' => Language::LANGCODE_DEFAULT, )); } + +/** + * Migrate aggregator blocks to configuration. + * + * @ingroup config_upgrade + */ +function aggregator_update_8002() { + // Provide a map with plugin name and query lookup routine for aggregator + // feeds and categories. + $map = array( + 'feed' => array( + 'plugin' => 'aggregator_feed_block', + 'query' => db_select('aggregator_feed')->fields('aggregator_feed', array('title')), + 'query field' => 'fid', + 'title translation' => '@title feed latest items', + ), + 'category' => array( + 'plugin' => 'aggregator_category_block', + 'query' => db_select('aggregator_category')->fields('aggregator_category', array('title')), + 'query field' => 'cid', + 'title translation' => '@title category latest items', + ), + ); + + $results = db_select('block')->condition('module', 'aggregator')->fields('block')->execute()->fetchAll(); + + foreach ($results as $block) { + // Assemble properties to pass into block config update function. + + // Get the block type and the derivative based on the delta. + list($type, $derivative) = explode('-', $block->delta); + + // Get the category or feed title from the respective table in the map. + $query = $map[$type]['query']; + $title = $query + ->condition($map[$type]['query field'], $derivative) + ->execute() + ->fetchField(); + + // Add the proper derivative title from translation in the map, and remove + // unnecessary characters. + $label = empty($block->title) ? t($map[$type]['title translation'], array('@title' => $title)) : $block->title; + $id = $block->theme . '.' . strtolower(preg_replace('/[^A-Za-z0-9\_]+/', '_', $label)); + $block_name = 'block.block.' . $id; + + $properties = array( + 'id' => $id, + 'plugin' => $map[$type]['plugin'] . ':' . $derivative, + 'weight' => $block->weight, + 'status' => $block->status, + 'region' => $block->region, + 'visibility' => array( + 'path' => array( + 'visibility' => $block->visibility, + 'pages' => $block->pages, + ), + 'role' => array( + 'roles' => array(), + ), + 'node_type' => array( + 'types' => array(), + ), + ), + 'settings' => array( + 'cache' => $block->cache, + 'label' => $label, + 'module' => 'aggregator', + 'block_count' => '10', + ), + ); + + update_block_to_config($block_name, 'aggregator', $block->delta, $properties); + } +} diff --git a/core/modules/book/book.install b/core/modules/book/book.install index 610808b..d8fc35b 100644 --- a/core/modules/book/book.install +++ b/core/modules/book/book.install @@ -79,3 +79,43 @@ function book_update_8000() { } } + +/** + * Migrate book navigation block to config including book navigation + * configuration. + */ +function book_update_8001() { + $results = db_select('block')->condition('module', 'book')->fields('block')->execute()->fetchAll(); + + foreach ($results as $block) { + // Assemble properties to pass into block config update function. + $block_name = 'block.block.' . $block->theme . '.' . $block->delta; + $properties = array( + 'id' => $block->theme . '.' . $block->delta, + 'plugin' => 'book_' . $block->delta, + 'weight' => $block->weight, + 'status' => $block->status, + 'region' => $block->region, + 'visibility' => array( + 'path' => array( + 'visibility' => $block->visibility, + 'pages' => $block->pages, + ), + 'role' => array( + 'roles' => array(), + ), + 'node_type' => array( + 'types' => array(), + ), + ), + 'settings' => array( + 'cache' => $block->cache, + 'label' => $block->title, + 'module' => 'book', + 'block_mode' => config('book.settings')->get('block.navigation.mode', 'all pages'), + ), + ); + + update_block_to_config($block_name, 'book', $block->delta, $properties); + } +} diff --git a/core/modules/comment/comment.install b/core/modules/comment/comment.install index efaf9f3..61feba8 100644 --- a/core/modules/comment/comment.install +++ b/core/modules/comment/comment.install @@ -388,6 +388,52 @@ function comment_update_8004() { } /** + * Migrate comment block to config. + */ +function comment_update_8005() { + $comment_block_count = variable_get('comment_block_count', 10); + + $results = db_select('block') + ->condition('module', 'comment') + ->condition('delta', 'recent') + ->fields('block') + ->execute() + ->fetchAll(); + + foreach ($results as $block) { + // Assemble properties to pass into block config update function. + $block_name = 'block.block.' . $block->theme . '.recent_comments'; + $properties = array( + 'id' => $block->theme . '.recent_comments', + 'plugin' => 'recent_comments', + 'weight' => $block->weight, + 'status' => $block->status, + 'region' => $block->region, + 'visibility' => array( + 'path' => array( + 'visibility' => $block->visibility, + 'pages' => $block->pages, + ), + 'role' => array( + 'roles' => array(), + ), + 'node_type' => array( + 'types' => array(), + ), + ), + 'settings' => array( + 'cache' => $block->cache, + 'label' => $block->title, + 'module' => 'ccoment', + 'block_count' => $comment_block_count, + ), + ); + + update_block_to_config($block_name, 'comment', $block->delta, $properties); + } +} + +/** * @} End of "addtogroup updates-7.x-to-8.x". * The next series of updates should start at 9000. */ diff --git a/core/modules/forum/forum.install b/core/modules/forum/forum.install index cd7ebdb..79acbce 100644 --- a/core/modules/forum/forum.install +++ b/core/modules/forum/forum.install @@ -270,3 +270,51 @@ function forum_update_8000() { 'forum_block_num_new' => 'block.new.limit', )); } + +/** + * Migrate forum new and active blocks to configuration. + */ +function forum_update_8001() { + // Block name map from Drupal 7 delta to Drupal 8. + $map = array( + 'new' => 'new_forum_topics', + 'active' => 'active_forum_topics', + ); + + $results = db_select('block')->condition('module', 'forum')->fields('block')->execute()->fetchAll(); + + foreach ($results as $block) { + // Assemble properties to pass into block config update function. + $block_name = 'block.block.' . $block->theme . '.' . $map[$block->delta]; + $properties = array( + 'id' => $block->theme . '.' . $block->delta, + 'plugin' => 'forum_' . $block->delta . '_block', + 'weight' => $block->weight, + 'status' => $block->status, + 'region' => $block->region, + 'visibility' => array( + 'path' => array( + 'visibility' => $block->visibility, + 'pages' => $block->pages, + ), + 'role' => array( + 'roles' => array(), + ), + 'node_type' => array( + 'types' => array(), + ), + ), + 'settings' => array( + 'cache' => $block->cache, + 'label' => $block->title, + 'module' => 'forum', + 'properties' => array( + 'administrative' => TRUE, + ), + 'block_count' => config('book.settings')->get('block.' . $block->delta . '.limit', 5), + ), + ); + + update_block_to_config($block_name, 'forum', $block->delta, $properties); + } +} diff --git a/core/modules/menu/menu.install b/core/modules/menu/menu.install index ae94f90..4e6bb44 100644 --- a/core/modules/menu/menu.install +++ b/core/modules/menu/menu.install @@ -15,6 +15,15 @@ function menu_uninstall() { } /** + * Implements hook_update_dependencies(). + */ +function menu_update_dependencies() { + $dependencies['menu'][8005] = array( + 'block' => 8004, + ); +} + +/** * Moves menu settings from variables to config. * * @ingroup config_upgrade @@ -89,3 +98,41 @@ function menu_update_8004() { } } +/** + * Migrate menu blocks into configuration. + * + * @ingroup config_upgrade + */ +function menu_update_8005() { + $results = db_select('block')->condition('module', 'menu')->fields('block')->execute()->fetchAll(); + + foreach ($results as $block) { + $block_name = 'block.block.' . $block->theme . '.' . $block->delta; + $properties = array( + 'id' => $block->theme . '.' . $block->delta, + 'plugin' => 'menu_menu_block:' . $block->delta, + 'weight' => $block->weight, + 'status' => $block->status, + 'region' => $block->region, + 'visibility' => array( + 'path' => array( + 'visibility' => $block->visibility, + 'pages' => $block->pages, + ), + 'role' => array( + 'roles' => array(), + ), + 'node_type' => array( + 'types' => array(), + ), + ), + 'settings' => array( + 'cache' => $block->cache, + 'label' => $block->title, + 'module' => 'menu', + ), + ); + + update_block_to_config($block_name, 'menu', $block->delta, $properties); + } +} diff --git a/core/modules/node/node.install b/core/modules/node/node.install index 84117d5..179e6ee 100644 --- a/core/modules/node/node.install +++ b/core/modules/node/node.install @@ -666,7 +666,7 @@ function node_update_8008() { } /** - * Coverts default_nodes_main variable to config. + * Converts default_nodes_main and node_recent_block_count variables to config. * * @ingroup config_upgrade */ @@ -1115,6 +1115,60 @@ function node_update_8020() { } /** + * Migrate recent content and syndicate blocks to config. + */ +function node_update_8021() { + // Block name and id mapping. + $map = array( + 'recent' => 'recent_content', + 'syndicate' => 'syndicate', + ); + $node_recent_block_count = variable_get('node_recent_block_count', 10); + + $results = db_select('block') + ->condition('module', 'node') + ->condition('delta', array('recent', 'syndicate'), 'IN') + ->fields('block') + ->execute() + ->fetchAll(); + + foreach ($results as $block) { + // Assemble properties to pass into block config update function. + $block_name = 'block.block.' . $block->theme . '.' . $map[$block->delta]; + $properties = array( + 'id' => $block->theme . '.' . $map[$block->delta], + 'plugin' => 'node_' . $block->delta . '_block', + 'weight' => $block->weight, + 'status' => $block->status, + 'region' => $block->region, + 'visibility' => array( + 'path' => array( + 'visibility' => $block->visibility, + 'pages' => $block->pages, + ), + 'role' => array( + 'roles' => array(), + ), + 'node_type' => array( + 'types' => array(), + ), + ), + 'settings' => array( + 'cache' => $block->cache, + 'label' => $block->title, + 'module' => 'node', + ), + ); + + if ($block->delta == 'recent') { + $properties['block_count'] = $node_recent_block_count; + } + + update_block_to_config($block_name, 'node', $block->delta, $properties); + } +} + +/** * @} End of "addtogroup updates-7.x-to-8.x" * The next series of updates should start at 9000. */ diff --git a/core/modules/search/search.install b/core/modules/search/search.install index 40a569c..75df391 100644 --- a/core/modules/search/search.install +++ b/core/modules/search/search.install @@ -214,3 +214,46 @@ function search_update_8001() { )); db_add_primary_key('search_index', array('word', 'sid', 'langcode', 'type')); } + +/** + * Migrate search block to config. + */ +function search_update_8002() { + $results = db_select('block') + ->condition('module', 'search') + ->condition('delta', 'form') + ->fields('block') + ->execute() + ->fetchAll(); + + foreach ($results as $block) { + // Assemble properties to pass into block config update function. + $block_name = 'block.block.' . $block->theme . '.search_form'; + $properties = array( + 'id' => $block->theme . '.search_form', + 'plugin' => 'search_form_block', + 'weight' => $block->weight, + 'status' => $block->status, + 'region' => $block->region, + 'visibility' => array( + 'path' => array( + 'visibility' => $block->visibility, + 'pages' => $block->pages, + ), + 'role' => array( + 'roles' => array(), + ), + 'node_type' => array( + 'types' => array(), + ), + ), + 'settings' => array( + 'cache' => $block->cache, + 'label' => $block->title, + 'module' => 'search', + ), + ); + + update_block_to_config($block_name, 'search', $block->delta, $properties); + } +} diff --git a/core/modules/shortcut/shortcut.install b/core/modules/shortcut/shortcut.install index d78fa3b..f83d692 100644 --- a/core/modules/shortcut/shortcut.install +++ b/core/modules/shortcut/shortcut.install @@ -101,6 +101,48 @@ function shortcut_update_8001() { } /** + * Migrate shortcuts block to config. + */ +function shortcut_update_8002() { + $results = db_select('block') + ->condition('module', 'shortcut') + ->condition('delta', 'shortcuts') + ->fields('block') + ->execute() + ->fetchAll(); + + foreach ($results as $block) { + // Assemble properties to pass into block config update function. + $block_name = 'block.block.' . $block->theme . '.shortcuts'; + $properties = array( + 'id' => $block->theme . '.shortcuts', + 'plugin' => 'shortcuts', + 'weight' => $block->weight, + 'status' => $block->status, + 'region' => $block->region, + 'visibility' => array( + 'path' => array( + 'visibility' => $block->visibility, + 'pages' => $block->pages, + ), + 'role' => array( + 'roles' => array(), + ), + 'node_type' => array( + 'types' => array(), + ), + ), + 'settings' => array( + 'cache' => $block->cache, + 'label' => $block->title, + 'module' => 'shortcut', + ), + ); + $ret = update_block_to_config($block_name, 'shortcut', $block->delta, $properties); + } +} + +/** * @} End of "addtogroup updates-7.x-to-8.x". * The next series of updates should start at 9000. */ diff --git a/core/modules/system/lib/Drupal/system/Tests/Upgrade/BlockUpgradePathTest.php b/core/modules/system/lib/Drupal/system/Tests/Upgrade/BlockUpgradePathTest.php index 45f2efb..39bacf4 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Upgrade/BlockUpgradePathTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Upgrade/BlockUpgradePathTest.php @@ -30,6 +30,23 @@ public function setUp() { } /** + * Tests block save after successful upgrade. + */ + public function testBlockUpgradeSave() { + $this->assertTrue($this->performUpgrade(), 'The upgrade was completed successfully.'); + + // Change the title of an upgraded block. + $edit = array( + 'settings[label]' => $this->randomName(50), + ); + $this->drupalPost('admin/structure/block/manage/bartik.login', $edit, t('Save block')); + + $this->drupalLogout(); + $this->drupalGet(''); + $this->assertText($edit['settings[label]'], 'Bartik user login block title successfully changed.'); + } + + /** * Tests block title length after successful upgrade. */ public function testBlockUpgradeTitleLength() { diff --git a/core/modules/system/system.install b/core/modules/system/system.install index 6c170da..6703192 100644 --- a/core/modules/system/system.install +++ b/core/modules/system/system.install @@ -2250,6 +2250,78 @@ function system_update_8059() { } /** + * Migrate system blocks to config. + * + * @ingroup config_upgrade + */ +function system_update_8060() { + $results = db_select('block')->condition('module', 'system')->fields('block')->execute()->fetchAll(); + + // There are a variety of block names and plugins that are not consistent. + $plugin_map = array( + 'main' => array( + 'plugin' => 'system_main_block', + 'name' => 'content', + ), + 'help' => array( + 'plugin' => 'system_help_block', + 'name' => 'help', + ), + 'powered-by' => array( + 'plugin' => 'system_powered_by_block', + 'name' => 'powered', + ), + 'menu-tools' => array( + 'plugin' => 'system_menu_block:menu-tools', + 'name' => 'tools', + ), + 'menu-admin' => array( + 'plugin' => 'system_menu_block:menu-admin', + 'name' => 'administration', + ), + 'menu-account' => array( + 'plugin' => 'system_menu_block:menu-account', + 'name' => 'user_account_menu', + ), + 'menu-main' => array( + 'plugin' => 'system_menu_block:menu-main', + 'name' => 'main_navigation', + ), + ); + + foreach ($results as $block) { + // Assemble properties to pass into block config update function. + $block_name = 'block.block.' . $block->theme . '.' . $plugin_map[$block->delta]['name']; + $properties = array( + 'id' => $block->theme . '.' . $plugin_map[$block->delta]['name'], + 'plugin' => $plugin_map[$block->delta]['plugin'], + 'weight' => $block->weight, + 'status' => $block->status, + 'region' => $block->region, + 'visibility' => array( + 'path' => array( + 'visibility' => $block->visibility, + 'pages' => $block->pages, + ), + 'role' => array( + 'roles' => array(), + ), + 'node_type' => array( + 'types' => array(), + ), + ), + 'settings' => array( + 'cache' => $block->cache, + 'label' => $block->title, + 'module' => 'system', + ), + ); + + update_block_to_config($block_name, 'system', $block->delta, $properties); + } +} + +/** * @} End of "defgroup updates-7.x-to-8.x". * The next series of updates should start at 9000. */ diff --git a/core/modules/user/user.install b/core/modules/user/user.install index 871c18d..38e33f7 100644 --- a/core/modules/user/user.install +++ b/core/modules/user/user.install @@ -1090,5 +1090,43 @@ function user_update_8020() { } /** + * Migrate user blocks into configuration. + */ +function user_update_8021() { + $results = db_select('block')->condition('module', 'user')->fields('block')->execute()->fetchAll(); + + foreach ($results as $block) { + // Assemble properties to pass into block config update function. + $block_name = 'block.block.' . $block->theme . '.' . $block->delta; + $properties = array( + 'id' => $block->theme . '.' . $block->delta, + 'plugin' => 'user_' . $block->delta . '_block', + 'weight' => $block->weight, + 'status' => $block->status, + 'region' => $block->region, + 'visibility' => array( + 'path' => array( + 'visibility' => $block->visibility, + 'pages' => $block->pages, + ), + 'role' => array( + 'roles' => array(), + ), + 'node_type' => array( + 'types' => array(), + ), + ), + 'settings' => array( + 'cache' => $block->cache, + 'label' => $block->title, + 'module' => 'user', + ), + ); + + update_block_to_config($block_name, 'user', $block->delta, $properties); + } +} + +/** * @} End of "addtogroup updates-7.x-to-8.x". */