diff --git a/core/modules/block/block.install b/core/modules/block/block.install index ed8d76f..2d5b344 100644 --- a/core/modules/block/block.install +++ b/core/modules/block/block.install @@ -194,15 +194,85 @@ function block_update_8006() { } /** - * Make changes required to {block_custom}. + * Migrate {block_custom} to {custom_block}. * * Note this table now resides in custom_block_schema() but for 7.x to 8.x * upgrades, changes must be made from block module as custom_block module is * only enabled during upgrade process. */ function block_update_8007() { - // Add the {block_custom_revision} table. - db_create_table('block_custom_revision', array( + // Add the {custom_block} table. + db_create_table('custom_block', array( + 'description' => 'Stores contents of custom-made blocks.', + 'fields' => array( + 'bid' => array( + 'type' => 'serial', + 'unsigned' => TRUE, + 'not null' => TRUE, + 'description' => "The block's {custom_block}.bid.", + ), + 'uuid' => array( + 'description' => 'Unique Key: Universally unique identifier for this entity.', + 'type' => 'varchar', + 'length' => 128, + 'not null' => FALSE, + ), + 'info' => array( + 'type' => 'varchar', + 'length' => 128, + 'not null' => TRUE, + 'default' => '', + 'description' => 'Block description.', + ), + 'machine_name' => array( + 'type' => 'varchar', + 'length' => 128, + 'not null' => TRUE, + 'default' => '', + 'description' => 'Machine name of block.', + ), + // Defaults to NULL in order to avoid a brief period of potential + // deadlocks on the index. + 'vid' => array( + 'description' => 'The current {block_custom_revision}.vid version identifier.', + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => FALSE, + 'default' => NULL, + ), + 'type' => array( + 'description' => 'The type of this custom block.', + 'type' => 'varchar', + 'length' => 32, + 'not null' => TRUE, + 'default' => '', + ), + 'langcode' => array( + 'description' => 'The {language}.langcode of this node.', + 'type' => 'varchar', + 'length' => 12, + 'not null' => TRUE, + 'default' => '', + ), + ), + 'indexes' => array( + 'block_custom_type' => array(array('type', 4)), + ), + 'unique keys' => array( + 'vid' => array('vid'), + 'uuid' => array('uuid'), + 'info' => array('info'), + ), + 'foreign keys' => array( + 'block_custom_revision' => array( + 'table' => 'block_custom_revision', + 'columns' => array('vid' => 'vid'), + ), + ), + 'primary key' => array('bid'), + )); + // Add the {custom_block_revision} table. + db_create_table('custom_block_revision', array( 'description' => 'Stores contents of custom-made blocks.', 'fields' => array( 'bid' => array( @@ -232,85 +302,34 @@ function block_update_8007() { ), 'primary key' => array('vid'), )); - // Add the vid column to {block_custom}. - db_add_field('block_custom', 'vid', array( - 'description' => 'The current {block_custom_revision}.vid version identifier.', - 'type' => 'int', - 'unsigned' => TRUE, - 'not null' => FALSE, - 'default' => NULL, - ), array( - 'unique keys' => array( - 'vid' => array('vid'), - ), - )); - // Add the uuid column to {block_custom}. - db_add_field('block_custom', 'uuid', array( - 'description' => 'Unique Key: Universally unique identifier for this entity.', - 'type' => 'varchar', - 'length' => 128, - 'not null' => FALSE, - ), - array( - 'unique keys' => array( - 'uuid' => array('uuid'), - ), - )); - // Add the machine_name column to {block_custom}. - db_add_field('block_custom', 'machine_name', array( - 'type' => 'varchar', - 'length' => 128, - 'not null' => TRUE, - 'default' => '', - 'description' => 'Machine name of block.', - )); - // Add the type column to {block_custom}. - db_add_field('block_custom', 'type', array( - 'description' => 'The type of this custom block.', - 'type' => 'varchar', - 'length' => 32, - 'not null' => TRUE, - 'default' => '', - )); - // Add the langcode column to {block_custom}. - db_add_field('block_custom', 'langcode', array( - 'description' => 'The {language}.langcode of this node.', - 'type' => 'varchar', - 'length' => 12, - 'not null' => TRUE, - 'default' => '', - )); - // Populate the {custom_block_revision} table. + // Populate the {custom_block} and {custom_block_revision} table. $results = db_select('block_custom', 'bc')->fields('bc')->execute(); foreach ($results as $block) { + $block->vid = $revision['vid']; + $block->langcode = LANGUAGE_NOT_SPECIFIED; + $block->type = 'basic'; + $block->machine_name = preg_replace('^([a-z|0-9|\_]', '', $block->info); $revision = array( 'bid' => $block->bid, 'log' => 'Initial value from 7.x to 8.x upgrade' ); + drupal_write_record('custom_block', $block); drupal_write_record('custom_block_revision', $revision); - db_update('block_custom')->values( - array( - 'vid' => $revision['vid'], - 'langcode' => LANGUAGE_NOT_SPECIFIED, - 'type' => 'basic', - 'machine_name' => preg_replace('^([a-z|0-9|\_]', '', $block->info) - ) - )->condition('bid', $block->bid)->execute(); } } /** - * Generate a UUID for all comments. + * Generate a UUID for all blocks. */ function block_update_8008(&$sandbox) { if (!isset($sandbox['progress'])) { $sandbox['progress'] = 0; $sandbox['last'] = 0; - $sandbox['max'] = db_query('SELECT COUNT(bid) FROM {block_custom} WHERE uuid IS NULL')->fetchField(); + $sandbox['max'] = db_query('SELECT COUNT(bid) FROM {custom_block} WHERE uuid IS NULL')->fetchField(); } - $bids = db_query_range('SELECT bid FROM {block_custom} WHERE bid > :bid AND uuid IS NULL ORDER BY bid ASC', 0, 10, array(':bid' => $sandbox['last']))->fetchCol(); - update_add_uuids($sandbox, 'block_custom', 'bid', $bids); + $bids = db_query_range('SELECT bid FROM {custom_block} WHERE bid > :bid AND uuid IS NULL ORDER BY bid ASC', 0, 10, array(':bid' => $sandbox['last']))->fetchCol(); + update_add_uuids($sandbox, 'custom_block', 'bid', $bids); $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']); } @@ -401,7 +420,7 @@ function block_update_8009() { db_drop_field('block_custom', 'body'); db_drop_field('block_custom', 'format'); - // We're done. + // We're done. $sandbox['#finished'] = 1; } } diff --git a/core/modules/block/custom_block/custom_block.admin.inc b/core/modules/block/custom_block/custom_block.admin.inc index 4fb5906..e4da0fa 100644 --- a/core/modules/block/custom_block/custom_block.admin.inc +++ b/core/modules/block/custom_block/custom_block.admin.inc @@ -16,7 +16,6 @@ * @see custom_block_menu() */ function custom_block_type_list() { - drupal_set_title(t('Custom block types')); return entity_list_controller('custom_block_type')->render(); } @@ -29,7 +28,6 @@ function custom_block_type_list() { * @see custom_block_menu() */ function custom_block_type_add() { - drupal_set_title(t('Add custom block type')); $block_type = entity_create('custom_block_type', array()); return entity_get_form($block_type); } diff --git a/core/modules/block/custom_block/custom_block.install b/core/modules/block/custom_block/custom_block.install index 6345b7f..3f1a8ef 100644 --- a/core/modules/block/custom_block/custom_block.install +++ b/core/modules/block/custom_block/custom_block.install @@ -10,14 +10,14 @@ */ function custom_block_schema() { $schema = array(); - $schema['block_custom'] = array( + $schema['custom_block'] = array( 'description' => 'Stores contents of custom-made blocks.', 'fields' => array( 'bid' => array( 'type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE, - 'description' => "The block's {block}.bid.", + 'description' => "The block's {custom_block}.bid.", ), 'uuid' => array( 'description' => 'Unique Key: Universally unique identifier for this entity.', @@ -80,7 +80,7 @@ function custom_block_schema() { 'primary key' => array('bid'), ); - $schema['block_custom_revision'] = array( + $schema['custom_block_revision'] = array( 'description' => 'Stores contents of custom-made blocks.', 'fields' => array( 'bid' => array( @@ -88,7 +88,7 @@ function custom_block_schema() { 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, - 'description' => "The block's {block}.bid.", + 'description' => "The block's {custom_block}.bid.", ), // Defaults to NULL in order to avoid a brief period of potential // deadlocks on the index. diff --git a/core/modules/block/custom_block/custom_block.module b/core/modules/block/custom_block/custom_block.module index bee1d7b..17d35d8 100644 --- a/core/modules/block/custom_block/custom_block.module +++ b/core/modules/block/custom_block/custom_block.module @@ -34,7 +34,7 @@ function custom_block_menu() { } $items['admin/structure/custom-blocks'] = array( - 'title' => 'Block types', + 'title' => 'Custom block types', 'description' => 'Manage custom block types.', 'page callback' => 'custom_block_type_list', 'access arguments' => array('administer blocks'), @@ -212,23 +212,46 @@ function custom_block_machine_name_load($machine_name) { * An array with 'path' as the key and the path to the custom block type as * the value. */ -function custom_block_type_uri(CustomBlockType $block_type) { +function custom_block_custom_block_type_uri(CustomBlockType $block_type) { return array( 'path' => 'admin/structure/custom-blocks/manage/' . $block_type->id(), ); } /** - * Implements hook_entity_info(). + * Entity URI callback. + * + * @param Drupal\custom_block\Plugin\Core\Entity\CustomBlock $block + * A custom block entity. + * + * @return array + * An array with 'path' as the key and the path to the custom block as the + * value. + */ +function custom_block_custom_block_uri(CustomBlock $block) { + return array( + 'path' => 'block/' . $block->machine_name->value, + ); +} + +/** + * Implements hook_entity_info_alter(). */ -function custom_block_entity_info(&$types) { +function custom_block_entity_info_alter(&$types) { // Add a translation handler for fields if the language module is enabled. if (module_exists('language')) { $types['custom_block']['translation']['custom_block'] = TRUE; } +} + +/** + * Implements hook_entity_bundle_info(). + */ +function custom_block_entity_bundle_info() { + $bundles = array(); foreach (config_get_storage_names_with_prefix('custom_block.type.') as $config_name) { $config = config($config_name); - $types['custom_block']['bundles'][$config->get('id')] = array( + $bundles['custom_block'][$config->get('id')] = array( 'label' => $config->get('label'), 'admin' => array( 'path' => 'admin/structure/custom-blocks/manage/%', @@ -238,6 +261,17 @@ function custom_block_entity_info(&$types) { ), ); } + return $bundles; +} + +/** + * Implements hook_entity_view_mode_info(). + */ +function custom_block_entity_view_mode_info() { + $view_modes['custom_block']['full'] = array( + 'label' => t('Full'), + ); + return $view_modes; } /** @@ -252,7 +286,7 @@ function custom_block_entity_info(&$types) { * Body field instance. */ function custom_block_add_body_field($block_type_id, $label = 'Block body') { - // Add or remove the body field, as needed. + // Add or remove the body field, as needed. $field = field_info_field('block_body'); $instance = field_info_instance('custom_block', 'block_body', $block_type_id); if (empty($field)) { diff --git a/core/modules/block/custom_block/custom_block.pages.inc b/core/modules/block/custom_block/custom_block.pages.inc index 45651ec..978b1c6 100644 --- a/core/modules/block/custom_block/custom_block.pages.inc +++ b/core/modules/block/custom_block/custom_block.pages.inc @@ -19,10 +19,6 @@ * @see custom_block_menu() */ function custom_block_page_view(CustomBlock $block) { - // If there is a menu link to this node, the link becomes the last part - // of the active trail, and the link name becomes the page title. - // Thus, we must explicitly set the page title to be the block title. - drupal_set_title($block->label()); $uri = $block->uri(); // Set the block path as the canonical URL to prevent duplicate content. drupal_add_html_head_link(array('rel' => 'canonical', 'href' => url($uri['path'])), TRUE); @@ -46,7 +42,6 @@ function custom_block_page_view(CustomBlock $block) { * @see custom_block_menu() */ function custom_block_add_page() { - drupal_set_title(t('Add custom block')); $options = array(); if (isset($_GET['theme']) && in_array($_GET['theme'], array_keys(list_themes()))) { // We have navigated to this page from the block library and will keep track diff --git a/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockTypeListController.php b/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockTypeListController.php index a59799e..584755a 100644 --- a/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockTypeListController.php +++ b/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockTypeListController.php @@ -50,7 +50,9 @@ public function buildHeader() { * Overrides Drupal\Core\Entity\EntityListController::buildRow(). */ public function buildRow(EntityInterface $entity) { - $row['type'] = check_plain($entity->label()); + parent::buildRow($entity); + $uri = $entity->uri(); + $row['type'] = l($entity->label(), $uri['path'], $uri['options']); $row['operations']['data'] = $this->buildOperations($entity); return $row; } diff --git a/core/modules/block/custom_block/lib/Drupal/custom_block/Plugin/Core/Entity/CustomBlock.php b/core/modules/block/custom_block/lib/Drupal/custom_block/Plugin/Core/Entity/CustomBlock.php index 610c3dd..63cbaa2 100644 --- a/core/modules/block/custom_block/lib/Drupal/custom_block/Plugin/Core/Entity/CustomBlock.php +++ b/core/modules/block/custom_block/lib/Drupal/custom_block/Plugin/Core/Entity/CustomBlock.php @@ -26,8 +26,8 @@ * "default" = "Drupal\custom_block\CustomBlockFormController" * }, * translation_controller_class = "Drupal\custom_block\CustomBlockTranslationController", - * base_table = "block_custom", - * revision_table = "block_custom_revision", + * base_table = "custom_block", + * revision_table = "custom_block_revision", * uri_callback = "custom_block_uri", * menu_base_path = "block/%custom_block_machine_name", * fieldable = TRUE, @@ -40,12 +40,6 @@ * }, * bundle_keys = { * "bundle" = "type" - * }, - * view_modes = { - * "full" = { - * "label" = "Full content", - * "custom_settings" = FALSE - * }, * } * ) */ @@ -155,9 +149,9 @@ public function label($langcode = NULL) { */ public function createDuplicate() { $duplicate = parent::createDuplicate(); - $duplicate->set('vid', NULL); - $duplicate->set('bid', NULL); - $duplicate->set('machine_name', NULL); + $duplicate->vid->value = NULL; + $duplicate->bid->value = NULL; + $duplicate->machine_name->value = NULL; return $duplicate; } diff --git a/core/modules/block/custom_block/lib/Drupal/custom_block/Plugin/Core/Entity/CustomBlockType.php b/core/modules/block/custom_block/lib/Drupal/custom_block/Plugin/Core/Entity/CustomBlockType.php index fb6f2b5..49a0f9c 100644 --- a/core/modules/block/custom_block/lib/Drupal/custom_block/Plugin/Core/Entity/CustomBlockType.php +++ b/core/modules/block/custom_block/lib/Drupal/custom_block/Plugin/Core/Entity/CustomBlockType.php @@ -23,7 +23,7 @@ * form_controller_class = { * "default" = "Drupal\custom_block\CustomBlockTypeFormController" * }, - * uri_callback = "custom_block_type_uri", + * uri_callback = "custom_block_custom_block_type_uri", * config_prefix = "custom_block.type", * entity_keys = { * "id" = "id", diff --git a/core/modules/block/custom_block/lib/Drupal/custom_block/Plugin/block/block/CustomBlock.php b/core/modules/block/custom_block/lib/Drupal/custom_block/Plugin/block/block/CustomBlock.php index 0ed4776..bfa7f9b 100644 --- a/core/modules/block/custom_block/lib/Drupal/custom_block/Plugin/block/block/CustomBlock.php +++ b/core/modules/block/custom_block/lib/Drupal/custom_block/Plugin/block/block/CustomBlock.php @@ -45,14 +45,14 @@ public function getConfig() { * Adds body and description fields to the block configuration form. */ public function blockForm($form, &$form_state) { - $view_modes = array(); - $info = entity_get_info('custom_block'); - foreach ($info['view_modes'] as $view_mode => $detail) { - $view_modes[$view_mode] = $detail['label']; + $options = array(); + $view_modes = entity_get_view_modes('custom_block'); + foreach ($view_modes as $view_mode => $detail) { + $options[$view_mode] = $detail['label']; } $form['custom_block']['view_mode'] = array( '#type' => 'select', - '#options' => $view_modes, + '#options' => $options, '#title' => t('View mode'), '#description' => t('Output the block in this view mode.'), '#default_value' => $this->configuration['view_mode'] diff --git a/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockCreationTest.php b/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockCreationTest.php index e7581a8..eb205e3 100644 --- a/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockCreationTest.php +++ b/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockCreationTest.php @@ -82,7 +82,7 @@ public function testFailedBlockCreation() { if (Database::getConnection()->supportsTransactions()) { // Check that the block does not exist in the database. - $bid = db_select('block_custom', 'b') + $bid = db_select('custom_block', 'b') ->fields('b', array('bid')) ->condition('info', 'fail_creation') ->execute() @@ -91,7 +91,7 @@ public function testFailedBlockCreation() { } else { // Check that the block exists in the database. - $bid = db_select('block_custom', 'b') + $bid = db_select('custom_block', 'b') ->fields('b', array('bid')) ->condition('info', 'fail_creation') ->execute() diff --git a/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockSaveTest.php b/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockSaveTest.php index 30fc1ba..e2bbb24 100644 --- a/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockSaveTest.php +++ b/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockSaveTest.php @@ -43,7 +43,7 @@ public function setUp() { */ public function testImport() { // Custom block ID must be a number that is not in the database. - $max_bid = db_query('SELECT MAX(bid) FROM {block_custom}')->fetchField(); + $max_bid = db_query('SELECT MAX(bid) FROM {custom_block}')->fetchField(); $test_bid = $max_bid + mt_rand(1000, 1000000); $info = $this->randomName(8); $block = array(