Commit 34453e5e authored by webchick's avatar webchick

Issue #1871696 by tim.plunkett, xjm, sun, msonnabaum, EclipseGc: Convert block...

Issue #1871696 by tim.plunkett, xjm, sun, msonnabaum, EclipseGc: Convert block instances to configuration entities to resolve architectural issues.
parent 342ace17
......@@ -371,11 +371,9 @@ function aggregator_save_category($edit) {
->condition('cid', $edit['cid'])
->execute();
// Make sure there is no active block for this category.
$block_configs = config_get_storage_names_with_prefix('plugin.core.block');
foreach ($block_configs as $config_id) {
$config = config($config_id);
if ($config->get('id') == 'aggregator_category_block:' . $edit['cid']) {
$config->delete();
if (module_exists('block')) {
foreach (entity_load_multiple_by_properties('block', array('plugin' => 'aggregator_category_block:' . $edit['cid'])) as $block) {
$block->delete();
}
}
$edit['title'] = '';
......@@ -439,11 +437,9 @@ function aggregator_save_feed($edit) {
->condition('fid', $edit['fid'])
->execute();
// Make sure there is no active block for this feed.
$block_configs = config_get_storage_names_with_prefix('plugin.core.block');
foreach ($block_configs as $config_id) {
$config = config($config_id);
if ($config->get('id') == 'aggregator_feed_block:' . $edit['fid']) {
$config->delete();
if (module_exists('block')) {
foreach (entity_load_multiple_by_properties('block', array('plugin' => 'aggregator_feed_block:' . $edit['fid'])) as $block) {
$block->delete();
}
}
}
......
......@@ -39,8 +39,7 @@ public function testBlockLinks() {
$this->updateFeedItems($feed, $this->getDefaultFeedItemCount());
// Clear the block cache to load the new block definitions.
$manager = $this->container->get('plugin.manager.block');
$manager->clearCachedDefinitions();
$this->container->get('plugin.manager.block')->clearCachedDefinitions();
// Need admin user to be able to access block admin.
$admin_user = $this->drupalCreateUser(array(
......@@ -51,15 +50,11 @@ public function testBlockLinks() {
));
$this->drupalLogin($admin_user);
$block = array(
'title' => 'feed-' . $feed->title,
'block_count' => 2,
);
$this->drupalPlaceBlock("aggregator_feed_block:{$feed->fid}", $block);
$block = $this->drupalPlaceBlock("aggregator_feed_block:{$feed->fid}", array('label' => 'feed-' . $feed->title), array('block_count' => 2));
// Confirm that the block is now being displayed on pages.
$this->drupalGet('node');
$this->assertText(t($block['title']), 'Feed block is displayed on the page.');
$this->assertText($block->label(), 'Feed block is displayed on the page.');
// Find the expected read_more link.
$href = 'aggregator/sources/' . $feed->fid;
......@@ -77,7 +72,7 @@ public function testBlockLinks() {
aggregator_save_feed((array) $feed);
// Check that the block is no longer displayed.
$this->drupalGet('node');
$this->assertNoText(t($block['title']), 'Feed block is not displayed on the page when number of items is set to 0.');
$this->assertNoText($block->label(), 'Feed block is not displayed on the page when number of items is set to 0.');
}
/**
......
......@@ -5,6 +5,8 @@
* Admin page callbacks for the block module.
*/
use Drupal\block\Plugin\Core\Entity\Block;
/**
* Page callback: Attaches CSS for the block region demo.
*
......@@ -18,310 +20,80 @@ function block_admin_demo($theme = NULL) {
/**
* Page callback: Shows the block administration page.
*
* @param $theme
* The theme to display the administration page for. If not provided, defaults
* to the currently used theme.
*
* @see block_menu()
*/
function block_admin_display($theme = NULL) {
global $theme_key;
drupal_theme_initialize();
if (!isset($theme)) {
// If theme is not specifically set, rehash for the current theme.
$theme = $theme_key;
}
// Fetch and sort blocks.
$blocks = block_admin_display_prepare_blocks($theme);
return drupal_get_form('block_admin_display_form', $blocks, $theme);
}
/**
* Prepares a list of blocks for display on the blocks administration page.
*
* @param $theme
* The machine-readable name of the theme whose blocks should be returned.
*
* @return
* An array of blocks, as returned by _block_rehash(), sorted by region in
* preparation for display on the blocks administration page.
*
* @see block_admin_display_form()
*/
function block_admin_display_prepare_blocks($theme) {
$blocks = _block_rehash($theme);
$compare_theme = &drupal_static('_block_compare:theme');
$compare_theme = $theme;
usort($blocks, '_block_compare');
return $blocks;
}
/**
* Form constructor for the main block administration form.
*
* @param $blocks
* An array of blocks, as returned by block_admin_display_prepare_blocks().
* @param $theme
* A string representing the name of the theme to edit blocks for.
* @param $block_regions
* (optional) An array of regions in which the blocks will be allowed to be
* placed. Defaults to all visible regions for the theme whose blocks are
* being configured. In all cases, a dummy region for disabled blocks will
* also be displayed.
*
* @return
* An array representing the form definition.
*
* @ingroup forms
* @see block_admin_display_form_submit()
*/
function block_admin_display_form($form, &$form_state, $blocks, $theme, $block_regions = NULL) {
$path = drupal_get_path('module', 'block');
$form['#attached']['css'][] = $path . '/block.admin.css';
$form['#attached']['library'][] = array('system', 'drupal.tableheader');
$form['#attached']['library'][] = array('block', 'drupal.block');
// Get a list of block regions if one was not provided.
if (!isset($block_regions)) {
$block_regions = system_region_list($theme, REGIONS_VISIBLE);
}
// Add a last region for disabled blocks.
$block_regions_with_disabled = $block_regions + array(BLOCK_REGION_NONE => BLOCK_REGION_NONE);
foreach ($block_regions_with_disabled as $region => $title) {
$form['#attached']['drupal_add_tabledrag'][] = array('blocks', 'match', 'sibling', 'block-region-select', 'block-region-' . $region, NULL, FALSE);
$form['#attached']['drupal_add_tabledrag'][] = array('blocks', 'order', 'sibling', 'block-weight', 'block-weight-' . $region);
}
// Weights range from -delta to +delta, so delta should be at least half
// of the amount of blocks present. This makes sure all blocks in the same
// region get an unique weight.
$weight_delta = round(count($blocks) / 2);
// Build the form tree.
$form['edited_theme'] = array(
'#type' => 'value',
'#value' => $theme,
);
$form['block_regions'] = array(
'#type' => 'value',
'#value' => $block_regions_with_disabled,
);
$form['blocks'] = array();
$form['#tree'] = TRUE;
foreach ($blocks as $key => $instance) {
$block = $instance->getConfig();
$form['blocks'][$key]['config_id'] = array(
'#type' => 'value',
'#value' => $block['config_id'],
);
$info = $instance->getDefinition();
$form['blocks'][$key]['info'] = array(
'#markup' => check_plain($info['subject']),
);
$form['blocks'][$key]['theme'] = array(
'#type' => 'hidden',
'#value' => $theme,
);
$form['blocks'][$key]['weight'] = array(
'#type' => 'weight',
'#default_value' => $block['weight'],
'#delta' => $weight_delta,
'#title_display' => 'invisible',
'#title' => t('Weight for @block block', array('@block' => $info['subject'])),
);
$form['blocks'][$key]['region'] = array(
'#type' => 'select',
'#default_value' => $block['region'] != BLOCK_REGION_NONE ? $block['region'] : NULL,
'#empty_value' => BLOCK_REGION_NONE,
'#title_display' => 'invisible',
'#title' => t('Region for @block block', array('@block' => $info['subject'])),
'#options' => $block_regions,
);
$links['configure'] = array(
'title' => t('configure'),
'href' => 'admin/structure/block/manage/' . $block['config_id'] . '/' . $theme . '/configure',
);
$links['delete'] = array(
'title' => t('delete'),
'href' => 'admin/structure/block/manage/' . $block['config_id'] . '/' . $theme . '/delete',
);
$form['blocks'][$key]['operations'] = array(
'#type' => 'operations',
'#links' => $links,
);
}
// Do not allow disabling the main system content block when it is present.
if (isset($form['blocks']['system_main']['region'])) {
$form['blocks']['system_main']['region']['#required'] = TRUE;
}
$form['actions'] = array(
'#tree' => FALSE,
'#type' => 'actions',
);
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save blocks'),
'#button_type' => 'primary',
);
return $form;
}
/**
* Form submission handler for block_admin_display_form().
* @param string $theme
* The theme to display the administration page for.
*
* @see block_admin_display_form()
*/
function block_admin_display_form_submit($form, &$form_state) {
foreach ($form_state['values']['blocks'] as $block) {
$config = config($block['config_id']);
$config->set('weight', $block['weight']);
$config->set('region', $block['region']);
$config->save();
}
drupal_set_message(t('The block settings have been updated.'));
cache_invalidate_tags(array('content' => TRUE));
}
/**
* Sorts active blocks by region, then by weight; sorts inactive blocks by name.
* @return array
* A render array for a page containing a list of blocks.
*
* Callback for usort() in block_admin_display_prepare_blocks().
* @see block_menu()
*/
function _block_compare($ainstance, $binstance) {
global $theme_key;
$a = $ainstance->getConfig();
$b = $binstance->getConfig();
// Theme should be set before calling this function, or the current theme
// is being used.
$theme = &drupal_static(__FUNCTION__ . ':theme');
if (!isset($theme)) {
$theme = $theme_key;
}
$regions = &drupal_static(__FUNCTION__ . ':regions');
// We need the region list to correctly order by region.
if (!isset($regions)) {
$regions = array_flip(array_keys(system_region_list($theme)));
$regions[BLOCK_REGION_NONE] = count($regions);
}
// Separate enabled from disabled.
$status = $b['status'] - $a['status'];
if ($status) {
return $status;
}
// Sort by region (in the order defined by theme .info file).
if ((!empty($a['region']) && !empty($b['region'])) && ($place = ($regions[$a['region']] - $regions[$b['region']]))) {
return $place;
}
// Sort by weight, unless disabled.
if ($a['region'] != BLOCK_REGION_NONE) {
$weight = $a['weight'] - $b['weight'];
if ($weight) {
return $weight;
}
}
// Sort by title.
$ainfo = $ainstance->getDefinition();
$binfo = $binstance->getDefinition();
return strcmp($ainfo['subject'], $binfo['subject']);
function block_admin_display($theme) {
return entity_list_controller('block')->render($theme);
}
/**
* Form constructor for the block instance configuration form.
* Page callback: Build the block instance add form.
*
* @param string $plugin_id
* The plugin ID for the block instance.
* @param string $theme
* (optional) The name of the theme for the block instance. If no theme is
* specified, the default theme will be used.
*
* @see block_menu()
* @see custom_block_menu()
* @see block_admin_configure_validate()
* @see block_admin_configure_submit()
* The name of the theme for the block instance.
*
* @ingroup forms
* @return array
* The block instance edit form.
*/
function block_admin_configure($form, &$form_state, $plugin_id, $theme = NULL) {
$instance = block_load($plugin_id);
$form['#instance'] = $instance;
$config = $instance->getConfig();
if (!isset($config['config_id']) && !$theme) {
$theme = variable_get('theme_default', 'stark');
}
elseif (!$theme && isset($config['config_id'])) {
list(, , , $theme) = explode('.', $config['config_id']);
}
$form['theme'] = array(
'#type' => 'value',
'#value' => $theme,
);
$form += $instance->form($form, $form_state);
return $form;
function block_admin_add($plugin_id, $theme) {
$entity = entity_create('block', array(
'plugin' => $plugin_id,
'theme' => $theme,
));
return entity_get_form($entity);
}
/**
* Form validation handler for block_admin_configure().
* Page callback: Build the block instance edit form.
*
* @see block_admin_configure()
* @see block_admin_configure_submit()
*/
function block_admin_configure_validate($form, &$form_state) {
$form['#instance']->validate($form, $form_state);
}
/**
* Form submission handler for block_admin_configure().
* @param \Drupal\block\Plugin\Core\Entity\Block $entity
* The block instance.
*
* @see block_admin_configure()
* @see block_admin_configure_validate()
* @return array
* The block instance edit form.
*/
function block_admin_configure_submit($form, &$form_state) {
$form['#instance']->submit($form, $form_state);
$config_values = $form['#instance']->getConfig();
$machine_name = 'plugin.core.block.' . $form_state['values']['theme'] . '.' . $form_state['values']['machine_name'];
$config = config($machine_name);
$config->set('id', $form['#instance']->getPluginId());
foreach ($config_values as $key => $value) {
$config->set($key, $value);
function block_admin_edit(Block $entity) {
// Get the theme for the page title.
$admin_theme = config('system.theme')->get('admin');
$themes = list_themes();
$theme_key = $entity->get('theme');
$theme = $themes[$theme_key];
// Use meaningful titles for the main site and administrative themes.
$theme_title = $theme->info['name'];
if ($theme_key == variable_get('theme_default', 'stark')) {
$theme_title = t('!theme (default theme)', array('!theme' => $theme_title));
}
elseif ($admin_theme && $theme_key == $admin_theme) {
$theme_title = t('!theme (administration theme)', array('!theme' => $theme_title));
}
$config->save();
drupal_set_message(t('The block configuration has been saved.'));
cache_invalidate_tags(array('content' => TRUE));
$form_state['redirect'] = 'admin/structure/block/list/block_plugin_ui:' . $form_state['values']['theme'];
// Get the block subject for the page title.
drupal_set_title(t("%label block in %theme", array('%label' => $entity->label(), '%theme' => $theme_title)), PASS_THROUGH);
return entity_get_form($entity);
}
/**
* Form constructor for the block instance deletion form.
*
* @param string $plugin_id
* The plugin ID for the block instance.
* @param string $theme
* The name of the theme for the block instance.
* @param \Drupal\block\Plugin\Core\Entity\Block $entity
* The block instance.
*
* @see block_menu()
* @see block_admin_block_delete_submit()
*/
function block_admin_block_delete($form, &$form_state, $plugin_id, $theme) {
$block = block_load($plugin_id);
$form['id'] = array('#type' => 'value', '#value' => $plugin_id);
$form['theme'] = array('#type' => 'value', '#value' => $theme);
$definition = $block->getDefinition();
$config = $block->getConfig();
$subject = empty($config['subject']) ? $definition['subject'] : $config['subject'];
$form['subject'] = array('#type' => 'value', '#value' => $subject);
function block_admin_block_delete(array $form, array &$form_state, Block $entity) {
$form['id'] = array('#type' => 'value', '#value' => $entity->id());
return confirm_form($form, t('Are you sure you want to delete the block %name?', array('%name' => $subject)), 'admin/structure/block', '', t('Delete'), t('Cancel'));
return confirm_form($form, t('Are you sure you want to delete the block %name?', array('%name' => $entity->label())), 'admin/structure/block', '', t('Delete'), t('Cancel'));
}
/**
......@@ -330,10 +102,10 @@ function block_admin_block_delete($form, &$form_state, $plugin_id, $theme) {
* @see block_admin_block_delete()
*/
function block_admin_block_delete_submit($form, &$form_state) {
$config = config($form_state['values']['id']);
$config->delete();
drupal_set_message(t('The block %name has been removed.', array('%name' => $form_state['values']['subject'])));
$form_state['redirect'] = 'admin/structure/block/list/block_plugin_ui:' . $form_state['values']['theme'];
$entity = entity_load('block', $form_state['values']['id']);
drupal_set_message(t('The block %name has been removed.', array('%name' => $entity->label())));
$form_state['redirect'] = 'admin/structure/block/list/block_plugin_ui:' . $entity->get('theme');
$entity->delete();
}
/**
......
......@@ -29,7 +29,7 @@
* @see hook_block_view_ID_alter()
* @see hook_block_view_NAME_alter()
*/
function hook_block_view_alter(array &$build, \Drupal\block\BlockInterface $block) {
function hook_block_view_alter(array &$build, \Drupal\block\Plugin\Core\Entity\Block $block) {
// Remove the contextual links on all blocks that provide them.
if (is_array($build) && isset($build['#contextual_links'])) {
unset($build['#contextual_links']);
......
......@@ -121,22 +121,29 @@ function block_menu() {
'access arguments' => array('administer blocks'),
'file' => 'block.admin.inc',
);
$items['admin/structure/block/manage/%/%'] = array(
$items['admin/structure/block/add/%/%'] = array(
'title' => 'Configure block',
'page callback' => 'drupal_get_form',
'page arguments' => array('block_admin_configure', 4, 5),
'page callback' => 'block_admin_add',
'page arguments' => array(4, 5),
'access arguments' => array('administer blocks'),
'file' => 'block.admin.inc',
);
$items['admin/structure/block/manage/%block'] = array(
'title' => 'Configure block',
'page callback' => 'block_admin_edit',
'page arguments' => array(4),
'access arguments' => array('administer blocks'),
'file' => 'block.admin.inc',
);
$items['admin/structure/block/manage/%/%/configure'] = array(
$items['admin/structure/block/manage/%block/configure'] = array(
'title' => 'Configure block',
'type' => MENU_DEFAULT_LOCAL_TASK,
'context' => MENU_CONTEXT_INLINE,
);
$items['admin/structure/block/manage/%/%/delete'] = array(
$items['admin/structure/block/manage/%block/delete'] = array(
'title' => 'Delete block',
'page callback' => 'drupal_get_form',
'page arguments' => array('block_admin_block_delete', 4, 5),
'page arguments' => array('block_admin_block_delete', 4),
'access arguments' => array('administer blocks'),
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_NONE,
......@@ -306,27 +313,24 @@ function _block_get_renderable_region($list = array()) {
!in_array($_SERVER['REQUEST_METHOD'], array('GET', 'HEAD'));
foreach ($list as $key => $block) {
$config = $block->getConfig();
$definition = $block->getDefinition();
$build[$key] = array(
'#block' => $block,
'#weight' => (int) $config['weight'],
'#theme_wrappers' => array('block'),
);
if ($not_cacheable || in_array($config['cache'], array(DRUPAL_NO_CACHE, DRUPAL_CACHE_CUSTOM))) {
// Non-cached blocks get built immediately. Provides more content
// that can be easily manipulated during hook_page_alter().
$build[$key] = _block_get_renderable_block($build[$key]);
$settings = $block->get('settings');
if ($not_cacheable || in_array($settings['cache'], array(DRUPAL_NO_CACHE, DRUPAL_CACHE_CUSTOM))) {
// Non-cached blocks get built immediately.
if ($block->access()) {
$build[$key] = entity_view($block, 'block');
}
}
else {
$key_components = explode('.', $key);
$id = array_pop($key_components);
$build[$key] += array(
$build[$key] = array(
'#block' => $block,
'#weight' => $block->get('weight'),
'#theme_wrappers' => array('block'),
'#pre_render' => array('_block_get_renderable_block'),
'#cache' => array(
'keys' => array($id, $config['module']),
'granularity' => $config['cache'],
'keys' => array($id, $block->get('module')),
'granularity' => $settings['cache'],
'bin' => 'block',
'tags' => array('content' => TRUE),
),
......@@ -338,9 +342,8 @@ function _block_get_renderable_region($list = array()) {
// skip the help block, since we assume that most users do not need or want
// to perform contextual actions on the help block, and the links needlessly
// draw attention on it.
if ($definition['class'] != 'Drupal\\system\\Plugin\\block\\block\\SystemHelpBlock' && $definition['class'] != 'Drupal\\system\\Plugin\\block\\block\\SystemMainBlock') {
global $theme;
$build[$key]['#contextual_links']['block'] = array('admin/structure/block/manage', array($key, $theme));
if (!in_array($block->get('plugin'), array('system_help_block', 'system_main_block'))) {
$build[$key]['#contextual_links']['block'] = array('admin/structure/block/manage', array($key));
}
}
return $build;
......@@ -357,33 +360,25 @@ function _block_get_renderable_region($list = array()) {
* Blocks currently exported by modules.
*/
function _block_rehash($theme = NULL) {
$blocks = array();
$instances = array();
$theme = $theme ? $theme : variable_get('theme_default', 'stark');
$block_configs = config_get_storage_names_with_prefix('plugin.core.block.' . $theme);
$regions = system_region_list($theme);
foreach ($block_configs as $config) {
// Only list valid block instances.
if (!$block = block_load($config)) {
continue;
}
$blocks[$config] = $block;
$config = config($config);
$region = $config->get('region');
$status = $config->get('status');
$blocks = entity_load_multiple_by_properties('block', array('theme' => $theme));
foreach ($blocks as $block_id => $block) {
$region = $block->get('region');
$status = $block->get('status');
// Disable blocks in invalid regions.
if (!empty($region) && $region != BLOCK_REGION_NONE && !isset($regions[$region]) && $status == 1) {
drupal_set_message(t('The block %info was assigned to the invalid region %region and has been disabled.', array('%info' => $config->get('id'), '%region' => $region)), 'warning');
drupal_set_message(t('The block %info was assigned to the invalid region %region and has been disabled.', array('%info' => $block_id, '%region' => $region)), 'warning');
// Disabled modules are moved into the BLOCK_REGION_NONE later so no
// need to move the block to another region.
$config->set('status', 0);
$config->save();
$block->set('status', 0);
$block->save();
}
// Set region to none if not enabled and make sure status is set.
if (empty($status)) {
$config->set('region', BLOCK_REGION_NONE);
$config->set('status', 0);
$config->save();
$block->set('region', BLOCK_REGION_NONE);
$block->set('status', 0);
$block->save();
}
}
return $blocks;
......@@ -414,28 +409,22 @@ function block_themes_enabled($theme_list) {
*/
function block_theme_initialize($theme) {
// Initialize theme's blocks if none already registered.
$has_blocks = config_get_storage_names_with_prefix('plugin.core.block.' . $theme);
$has_blocks = entity_load_multiple_by_properties('block', array('theme' => $theme));
if (!$has_blocks) {
$default_theme = variable_get('theme_default', 'stark');
// Apply only to new theme's visible regions.
<