annotate core/modules/block/block.module @ 15:e200cb7efeb3

Update Drupal core to 8.5.3 via Composer
author Chris Cannam
date Thu, 26 Apr 2018 11:26:54 +0100
parents 4c8ae668cc8c
children 129ea1e6d783
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /**
Chris@0 4 * @file
Chris@0 5 * Controls the visual building blocks a page is constructed with.
Chris@0 6 */
Chris@0 7
Chris@0 8 use Drupal\Component\Utility\Html;
Chris@0 9 use Drupal\Core\Routing\RouteMatchInterface;
Chris@0 10 use Drupal\Core\Url;
Chris@0 11 use Drupal\language\ConfigurableLanguageInterface;
Chris@0 12 use Drupal\system\Entity\Menu;
Chris@0 13 use Drupal\block\Entity\Block;
Chris@0 14
Chris@0 15 /**
Chris@0 16 * Implements hook_help().
Chris@0 17 */
Chris@0 18 function block_help($route_name, RouteMatchInterface $route_match) {
Chris@0 19 switch ($route_name) {
Chris@0 20 case 'help.page.block':
Chris@0 21 $block_content = \Drupal::moduleHandler()->moduleExists('block_content') ? \Drupal::url('help.page', ['name' => 'block_content']) : '#';
Chris@0 22 $output = '';
Chris@0 23 $output .= '<h3>' . t('About') . '</h3>';
Chris@0 24 $output .= '<p>' . t('The Block module allows you to place blocks in regions of your installed themes, and configure block settings. For more information, see the <a href=":blocks-documentation">online documentation for the Block module</a>.', [':blocks-documentation' => 'https://www.drupal.org/documentation/modules/block/']) . '</p>';
Chris@0 25 $output .= '<h3>' . t('Uses') . '</h3>';
Chris@0 26 $output .= '<dl>';
Chris@0 27 $output .= '<dt>' . t('Placing and moving blocks') . '</dt>';
Chris@0 28 $output .= '<dd>' . t('You can place a new block in a region by selecting <em>Place block</em> on the <a href=":blocks">Block layout page</a>. Once a block is placed, it can be moved to a different region by drag-and-drop or by using the <em>Region</em> drop-down list, and then clicking <em>Save blocks</em>.', [':blocks' => \Drupal::url('block.admin_display')]) . '</dd>';
Chris@0 29 $output .= '<dt>' . t('Toggling between different themes') . '</dt>';
Chris@0 30 $output .= '<dd>' . t('Blocks are placed and configured specifically for each theme. The Block layout page opens with the default theme, but you can toggle to other installed themes.') . '</dd>';
Chris@0 31 $output .= '<dt>' . t('Demonstrating block regions for a theme') . '</dt>';
Chris@0 32 $output .= '<dd>' . t('You can see where the regions are for the current theme by clicking the <em>Demonstrate block regions</em> link on the <a href=":blocks">Block layout page</a>. Regions are specific to each theme.', [':blocks' => \Drupal::url('block.admin_display')]) . '</dd>';
Chris@0 33 $output .= '<dt>' . t('Configuring block settings') . '</dt>';
Chris@0 34 $output .= '<dd>' . t('To change the settings of an individual block click on the <em>Configure</em> link on the <a href=":blocks">Block layout page</a>. The available options vary depending on the module that provides the block. For all blocks you can change the block title and toggle whether to display it.', [':blocks' => Drupal::url('block.admin_display')]) . '</dd>';
Chris@0 35 $output .= '<dt>' . t('Controlling visibility') . '</dt>';
Chris@0 36 $output .= '<dd>' . t('You can control the visibility of a block by restricting it to specific pages, content types, and/or roles by setting the appropriate options under <em>Visibility settings</em> of the block configuration.') . '</dd>';
Chris@0 37 $output .= '<dt>' . t('Adding custom blocks') . '</dt>';
Chris@0 38 $output .= '<dd>' . t('You can add custom blocks, if the <em>Custom Block</em> module is installed. For more information, see the <a href=":blockcontent-help">Custom Block help page</a>.', [':blockcontent-help' => $block_content]) . '</dd>';
Chris@0 39 $output .= '</dl>';
Chris@0 40 return $output;
Chris@0 41 }
Chris@0 42 if ($route_name == 'block.admin_display' || $route_name == 'block.admin_display_theme') {
Chris@0 43 $demo_theme = $route_match->getParameter('theme') ?: \Drupal::config('system.theme')->get('default');
Chris@0 44 $themes = \Drupal::service('theme_handler')->listInfo();
Chris@0 45 $output = '<p>' . t('Block placement is specific to each theme on your site. Changes will not be saved until you click <em>Save blocks</em> at the bottom of the page.') . '</p>';
Chris@0 46 $output .= '<p>' . \Drupal::l(t('Demonstrate block regions (@theme)', ['@theme' => $themes[$demo_theme]->info['name']]), new Url('block.admin_demo', ['theme' => $demo_theme])) . '</p>';
Chris@0 47 return $output;
Chris@0 48 }
Chris@0 49 }
Chris@0 50
Chris@0 51 /**
Chris@0 52 * Implements hook_theme().
Chris@0 53 */
Chris@0 54 function block_theme() {
Chris@0 55 return [
Chris@0 56 'block' => [
Chris@0 57 'render element' => 'elements',
Chris@0 58 ],
Chris@0 59 ];
Chris@0 60 }
Chris@0 61
Chris@0 62 /**
Chris@0 63 * Implements hook_page_top().
Chris@0 64 */
Chris@0 65 function block_page_top(array &$page_top) {
Chris@0 66 if (\Drupal::routeMatch()->getRouteName() === 'block.admin_demo') {
Chris@0 67 $theme = \Drupal::theme()->getActiveTheme()->getName();
Chris@0 68 $page_top['backlink'] = [
Chris@0 69 '#type' => 'link',
Chris@0 70 '#title' => t('Exit block region demonstration'),
Chris@0 71 '#options' => ['attributes' => ['class' => ['block-demo-backlink']]],
Chris@0 72 '#weight' => -10,
Chris@0 73 ];
Chris@0 74 if (\Drupal::config('system.theme')->get('default') == $theme) {
Chris@0 75 $page_top['backlink']['#url'] = Url::fromRoute('block.admin_display');
Chris@0 76 }
Chris@0 77 else {
Chris@0 78 $page_top['backlink']['#url'] = Url::fromRoute('block.admin_display_theme', ['theme' => $theme]);
Chris@0 79 }
Chris@0 80 }
Chris@0 81 }
Chris@0 82
Chris@0 83 /**
Chris@0 84 * Initializes blocks for installed themes.
Chris@0 85 *
Chris@0 86 * @param $theme_list
Chris@0 87 * An array of theme names.
Chris@0 88 */
Chris@0 89 function block_themes_installed($theme_list) {
Chris@0 90 foreach ($theme_list as $theme) {
Chris@0 91 // Don't initialize themes that are not displayed in the UI.
Chris@0 92 if (\Drupal::service('theme_handler')->hasUi($theme)) {
Chris@0 93 block_theme_initialize($theme);
Chris@0 94 }
Chris@0 95 }
Chris@0 96 }
Chris@0 97
Chris@0 98 /**
Chris@0 99 * Assigns an initial, default set of blocks for a theme.
Chris@0 100 *
Chris@0 101 * This function is called the first time a new theme is installed. The new
Chris@0 102 * theme gets a copy of the default theme's blocks, with the difference that if
Chris@0 103 * a particular region isn't available in the new theme, the block is assigned
Chris@0 104 * to the new theme's default region.
Chris@0 105 *
Chris@0 106 * @param $theme
Chris@0 107 * The name of a theme.
Chris@0 108 */
Chris@0 109 function block_theme_initialize($theme) {
Chris@0 110 // Initialize theme's blocks if none already registered.
Chris@0 111 $has_blocks = \Drupal::entityTypeManager()->getStorage('block')->loadByProperties(['theme' => $theme]);
Chris@0 112 if (!$has_blocks) {
Chris@0 113 $default_theme = \Drupal::config('system.theme')->get('default');
Chris@0 114 // Apply only to new theme's visible regions.
Chris@0 115 $regions = system_region_list($theme, REGIONS_VISIBLE);
Chris@0 116 $default_theme_blocks = \Drupal::entityTypeManager()->getStorage('block')->loadByProperties(['theme' => $default_theme]);
Chris@0 117 foreach ($default_theme_blocks as $default_theme_block_id => $default_theme_block) {
Chris@0 118 if (strpos($default_theme_block_id, $default_theme . '_') === 0) {
Chris@0 119 $id = str_replace($default_theme, $theme, $default_theme_block_id);
Chris@0 120 }
Chris@0 121 else {
Chris@0 122 $id = $theme . '_' . $default_theme_block_id;
Chris@0 123 }
Chris@0 124 $block = $default_theme_block->createDuplicateBlock($id, $theme);
Chris@0 125 // If the region isn't supported by the theme, assign the block to the
Chris@0 126 // theme's default region.
Chris@0 127 if (!isset($regions[$block->getRegion()])) {
Chris@0 128 $block->setRegion(system_default_region($theme));
Chris@0 129 }
Chris@0 130 $block->save();
Chris@0 131 }
Chris@0 132 }
Chris@0 133 }
Chris@0 134
Chris@0 135 /**
Chris@0 136 * Implements hook_rebuild().
Chris@0 137 */
Chris@0 138 function block_rebuild() {
Chris@0 139 foreach (\Drupal::service('theme_handler')->listInfo() as $theme => $data) {
Chris@0 140 if ($data->status) {
Chris@0 141 $regions = system_region_list($theme);
Chris@0 142 /** @var \Drupal\block\BlockInterface[] $blocks */
Chris@0 143 $blocks = \Drupal::entityTypeManager()->getStorage('block')->loadByProperties(['theme' => $theme]);
Chris@0 144 foreach ($blocks as $block_id => $block) {
Chris@0 145 // Disable blocks in invalid regions.
Chris@0 146 if (!isset($regions[$block->getRegion()])) {
Chris@0 147 if ($block->status()) {
Chris@0 148 drupal_set_message(t('The block %info was assigned to the invalid region %region and has been disabled.', ['%info' => $block_id, '%region' => $block->getRegion()]), 'warning');
Chris@0 149 }
Chris@0 150 $block
Chris@0 151 ->setRegion(system_default_region($theme))
Chris@0 152 ->disable()
Chris@0 153 ->save();
Chris@0 154 }
Chris@0 155 }
Chris@0 156 }
Chris@0 157 }
Chris@0 158 }
Chris@0 159
Chris@0 160 /**
Chris@0 161 * Implements hook_theme_suggestions_HOOK().
Chris@0 162 */
Chris@0 163 function block_theme_suggestions_block(array $variables) {
Chris@0 164 $suggestions = [];
Chris@0 165
Chris@0 166 $suggestions[] = 'block__' . $variables['elements']['#configuration']['provider'];
Chris@0 167 // Hyphens (-) and underscores (_) play a special role in theme suggestions.
Chris@0 168 // Theme suggestions should only contain underscores, because within
Chris@0 169 // drupal_find_theme_templates(), underscores are converted to hyphens to
Chris@0 170 // match template file names, and then converted back to underscores to match
Chris@0 171 // pre-processing and other function names. So if your theme suggestion
Chris@0 172 // contains a hyphen, it will end up as an underscore after this conversion,
Chris@0 173 // and your function names won't be recognized. So, we need to convert
Chris@0 174 // hyphens to underscores in block deltas for the theme suggestions.
Chris@0 175
Chris@0 176 // We can safely explode on : because we know the Block plugin type manager
Chris@0 177 // enforces that delimiter for all derivatives.
Chris@0 178 $parts = explode(':', $variables['elements']['#plugin_id']);
Chris@0 179 $suggestion = 'block';
Chris@0 180 while ($part = array_shift($parts)) {
Chris@0 181 $suggestions[] = $suggestion .= '__' . strtr($part, '-', '_');
Chris@0 182 }
Chris@0 183
Chris@0 184 if (!empty($variables['elements']['#id'])) {
Chris@0 185 $suggestions[] = 'block__' . $variables['elements']['#id'];
Chris@0 186 }
Chris@0 187
Chris@0 188 return $suggestions;
Chris@0 189 }
Chris@0 190
Chris@0 191 /**
Chris@0 192 * Prepares variables for block templates.
Chris@0 193 *
Chris@0 194 * Default template: block.html.twig.
Chris@0 195 *
Chris@0 196 * Prepares the values passed to the theme_block function to be passed
Chris@0 197 * into a pluggable template engine. Uses block properties to generate a
Chris@0 198 * series of template file suggestions. If none are found, the default
Chris@0 199 * block.html.twig is used.
Chris@0 200 *
Chris@0 201 * Most themes use their own copy of block.html.twig. The default is located
Chris@0 202 * inside "core/modules/block/templates/block.html.twig". Look in there for the
Chris@0 203 * full list of available variables.
Chris@0 204 *
Chris@0 205 * @param array $variables
Chris@0 206 * An associative array containing:
Chris@0 207 * - elements: An associative array containing the properties of the element.
Chris@0 208 * Properties used: #block, #configuration, #children, #plugin_id.
Chris@0 209 */
Chris@0 210 function template_preprocess_block(&$variables) {
Chris@0 211 $variables['configuration'] = $variables['elements']['#configuration'];
Chris@0 212 $variables['plugin_id'] = $variables['elements']['#plugin_id'];
Chris@0 213 $variables['base_plugin_id'] = $variables['elements']['#base_plugin_id'];
Chris@0 214 $variables['derivative_plugin_id'] = $variables['elements']['#derivative_plugin_id'];
Chris@0 215 $variables['label'] = !empty($variables['configuration']['label_display']) ? $variables['configuration']['label'] : '';
Chris@0 216 $variables['content'] = $variables['elements']['content'];
Chris@0 217 // A block's label is configuration: it is static. Allow dynamic labels to be
Chris@0 218 // set in the render array.
Chris@0 219 if (isset($variables['elements']['content']['#title']) && !empty($variables['configuration']['label_display'])) {
Chris@0 220 $variables['label'] = $variables['elements']['content']['#title'];
Chris@0 221 }
Chris@0 222
Chris@0 223 // Create a valid HTML ID and make sure it is unique.
Chris@0 224 if (!empty($variables['elements']['#id'])) {
Chris@0 225 $variables['attributes']['id'] = Html::getUniqueId('block-' . $variables['elements']['#id']);
Chris@0 226 }
Chris@0 227
Chris@0 228 // Proactively add aria-describedby if possible to improve accessibility.
Chris@0 229 if ($variables['label'] && isset($variables['attributes']['role'])) {
Chris@0 230 $variables['title_attributes']['id'] = Html::getUniqueId($variables['label']);
Chris@0 231 $variables['attributes']['aria-describedby'] = $variables['title_attributes']['id'];
Chris@0 232 }
Chris@0 233
Chris@0 234 }
Chris@0 235
Chris@0 236 /**
Chris@0 237 * Implements hook_ENTITY_TYPE_delete() for user_role entities.
Chris@0 238 *
Chris@0 239 * Removes deleted role from blocks that use it.
Chris@0 240 */
Chris@0 241 function block_user_role_delete($role) {
Chris@0 242 foreach (Block::loadMultiple() as $block) {
Chris@0 243 /** @var $block \Drupal\block\BlockInterface */
Chris@0 244 $visibility = $block->getVisibility();
Chris@0 245 if (isset($visibility['user_role']['roles'][$role->id()])) {
Chris@0 246 unset($visibility['user_role']['roles'][$role->id()]);
Chris@0 247 $block->setVisibilityConfig('user_role', $visibility['user_role']);
Chris@0 248 $block->save();
Chris@0 249 }
Chris@0 250 }
Chris@0 251 }
Chris@0 252
Chris@0 253 /**
Chris@0 254 * Implements hook_ENTITY_TYPE_delete() for menu entities.
Chris@0 255 */
Chris@0 256 function block_menu_delete(Menu $menu) {
Chris@0 257 if (!$menu->isSyncing()) {
Chris@0 258 foreach (Block::loadMultiple() as $block) {
Chris@0 259 if ($block->getPluginId() == 'system_menu_block:' . $menu->id()) {
Chris@0 260 $block->delete();
Chris@0 261 }
Chris@0 262 }
Chris@0 263 }
Chris@0 264 }
Chris@0 265
Chris@0 266 /**
Chris@0 267 * Implements hook_ENTITY_TYPE_delete() for 'configurable_language'.
Chris@0 268 *
Chris@0 269 * Delete the potential block visibility settings of the deleted language.
Chris@0 270 */
Chris@0 271 function block_configurable_language_delete(ConfigurableLanguageInterface $language) {
Chris@0 272 // Remove the block visibility settings for the deleted language.
Chris@0 273 foreach (Block::loadMultiple() as $block) {
Chris@0 274 /** @var $block \Drupal\block\BlockInterface */
Chris@0 275 $visibility = $block->getVisibility();
Chris@0 276 if (isset($visibility['language']['langcodes'][$language->id()])) {
Chris@0 277 unset($visibility['language']['langcodes'][$language->id()]);
Chris@0 278 $block->setVisibilityConfig('language', $visibility['language']);
Chris@0 279 $block->save();
Chris@0 280 }
Chris@0 281 }
Chris@0 282 }