Chris@0: moduleExists('block_content') ? Url::fromRoute('help.page', ['name' => 'block_content'])->toString() : '#'; Chris@0: $output = ''; Chris@0: $output .= '
' . t('The Block module allows you to place blocks in regions of your installed themes, and configure block settings. For more information, see the online documentation for the Block module.', [':blocks-documentation' => 'https://www.drupal.org/documentation/modules/block/']) . '
'; Chris@0: $output .= '' . t('Block placement is specific to each theme on your site. Changes will not be saved until you click Save blocks at the bottom of the page.') . '
'; Chris@0: $output .= '' . \Drupal::l(t('Demonstrate block regions (@theme)', ['@theme' => $themes[$demo_theme]->info['name']]), new Url('block.admin_demo', ['theme' => $demo_theme])) . '
'; Chris@0: return $output; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements hook_theme(). Chris@0: */ Chris@0: function block_theme() { Chris@0: return [ Chris@0: 'block' => [ Chris@0: 'render element' => 'elements', Chris@0: ], Chris@0: ]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements hook_page_top(). Chris@0: */ Chris@0: function block_page_top(array &$page_top) { Chris@0: if (\Drupal::routeMatch()->getRouteName() === 'block.admin_demo') { Chris@0: $theme = \Drupal::theme()->getActiveTheme()->getName(); Chris@0: $page_top['backlink'] = [ Chris@0: '#type' => 'link', Chris@0: '#title' => t('Exit block region demonstration'), Chris@0: '#options' => ['attributes' => ['class' => ['block-demo-backlink']]], Chris@0: '#weight' => -10, Chris@0: ]; Chris@0: if (\Drupal::config('system.theme')->get('default') == $theme) { Chris@0: $page_top['backlink']['#url'] = Url::fromRoute('block.admin_display'); Chris@0: } Chris@0: else { Chris@0: $page_top['backlink']['#url'] = Url::fromRoute('block.admin_display_theme', ['theme' => $theme]); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Initializes blocks for installed themes. Chris@0: * Chris@0: * @param $theme_list Chris@0: * An array of theme names. Chris@0: */ Chris@0: function block_themes_installed($theme_list) { Chris@0: foreach ($theme_list as $theme) { Chris@0: // Don't initialize themes that are not displayed in the UI. Chris@0: if (\Drupal::service('theme_handler')->hasUi($theme)) { Chris@0: block_theme_initialize($theme); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Assigns an initial, default set of blocks for a theme. Chris@0: * Chris@0: * This function is called the first time a new theme is installed. The new Chris@0: * theme gets a copy of the default theme's blocks, with the difference that if Chris@0: * a particular region isn't available in the new theme, the block is assigned Chris@0: * to the new theme's default region. Chris@0: * Chris@0: * @param $theme Chris@0: * The name of a theme. Chris@0: */ Chris@0: function block_theme_initialize($theme) { Chris@0: // Initialize theme's blocks if none already registered. Chris@0: $has_blocks = \Drupal::entityTypeManager()->getStorage('block')->loadByProperties(['theme' => $theme]); Chris@0: if (!$has_blocks) { Chris@0: $default_theme = \Drupal::config('system.theme')->get('default'); Chris@0: // Apply only to new theme's visible regions. Chris@0: $regions = system_region_list($theme, REGIONS_VISIBLE); Chris@0: $default_theme_blocks = \Drupal::entityTypeManager()->getStorage('block')->loadByProperties(['theme' => $default_theme]); Chris@0: foreach ($default_theme_blocks as $default_theme_block_id => $default_theme_block) { Chris@0: if (strpos($default_theme_block_id, $default_theme . '_') === 0) { Chris@0: $id = str_replace($default_theme, $theme, $default_theme_block_id); Chris@0: } Chris@0: else { Chris@0: $id = $theme . '_' . $default_theme_block_id; Chris@0: } Chris@0: $block = $default_theme_block->createDuplicateBlock($id, $theme); Chris@0: // If the region isn't supported by the theme, assign the block to the Chris@0: // theme's default region. Chris@0: if (!isset($regions[$block->getRegion()])) { Chris@0: $block->setRegion(system_default_region($theme)); Chris@0: } Chris@0: $block->save(); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements hook_rebuild(). Chris@0: */ Chris@0: function block_rebuild() { Chris@0: foreach (\Drupal::service('theme_handler')->listInfo() as $theme => $data) { Chris@0: if ($data->status) { Chris@0: $regions = system_region_list($theme); Chris@0: /** @var \Drupal\block\BlockInterface[] $blocks */ Chris@0: $blocks = \Drupal::entityTypeManager()->getStorage('block')->loadByProperties(['theme' => $theme]); Chris@0: foreach ($blocks as $block_id => $block) { Chris@0: // Disable blocks in invalid regions. Chris@0: if (!isset($regions[$block->getRegion()])) { Chris@0: if ($block->status()) { Chris@17: \Drupal::messenger() Chris@17: ->addWarning(t('The block %info was assigned to the invalid region %region and has been disabled.', [ Chris@17: '%info' => $block_id, Chris@17: '%region' => $block->getRegion(), Chris@17: ])); Chris@0: } Chris@0: $block Chris@0: ->setRegion(system_default_region($theme)) Chris@0: ->disable() Chris@0: ->save(); Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements hook_theme_suggestions_HOOK(). Chris@0: */ Chris@0: function block_theme_suggestions_block(array $variables) { Chris@0: $suggestions = []; Chris@0: Chris@0: $suggestions[] = 'block__' . $variables['elements']['#configuration']['provider']; Chris@0: // Hyphens (-) and underscores (_) play a special role in theme suggestions. Chris@0: // Theme suggestions should only contain underscores, because within Chris@0: // drupal_find_theme_templates(), underscores are converted to hyphens to Chris@0: // match template file names, and then converted back to underscores to match Chris@0: // pre-processing and other function names. So if your theme suggestion Chris@0: // contains a hyphen, it will end up as an underscore after this conversion, Chris@0: // and your function names won't be recognized. So, we need to convert Chris@0: // hyphens to underscores in block deltas for the theme suggestions. Chris@0: Chris@0: // We can safely explode on : because we know the Block plugin type manager Chris@0: // enforces that delimiter for all derivatives. Chris@0: $parts = explode(':', $variables['elements']['#plugin_id']); Chris@0: $suggestion = 'block'; Chris@0: while ($part = array_shift($parts)) { Chris@0: $suggestions[] = $suggestion .= '__' . strtr($part, '-', '_'); Chris@0: } Chris@0: Chris@0: if (!empty($variables['elements']['#id'])) { Chris@0: $suggestions[] = 'block__' . $variables['elements']['#id']; Chris@0: } Chris@0: Chris@0: return $suggestions; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Prepares variables for block templates. Chris@0: * Chris@0: * Default template: block.html.twig. Chris@0: * Chris@0: * Prepares the values passed to the theme_block function to be passed Chris@0: * into a pluggable template engine. Uses block properties to generate a Chris@0: * series of template file suggestions. If none are found, the default Chris@0: * block.html.twig is used. Chris@0: * Chris@0: * Most themes use their own copy of block.html.twig. The default is located Chris@0: * inside "core/modules/block/templates/block.html.twig". Look in there for the Chris@0: * full list of available variables. Chris@0: * Chris@0: * @param array $variables Chris@0: * An associative array containing: Chris@0: * - elements: An associative array containing the properties of the element. Chris@0: * Properties used: #block, #configuration, #children, #plugin_id. Chris@0: */ Chris@0: function template_preprocess_block(&$variables) { Chris@0: $variables['configuration'] = $variables['elements']['#configuration']; Chris@0: $variables['plugin_id'] = $variables['elements']['#plugin_id']; Chris@0: $variables['base_plugin_id'] = $variables['elements']['#base_plugin_id']; Chris@0: $variables['derivative_plugin_id'] = $variables['elements']['#derivative_plugin_id']; Chris@0: $variables['label'] = !empty($variables['configuration']['label_display']) ? $variables['configuration']['label'] : ''; Chris@0: $variables['content'] = $variables['elements']['content']; Chris@0: // A block's label is configuration: it is static. Allow dynamic labels to be Chris@0: // set in the render array. Chris@0: if (isset($variables['elements']['content']['#title']) && !empty($variables['configuration']['label_display'])) { Chris@0: $variables['label'] = $variables['elements']['content']['#title']; Chris@0: } Chris@0: Chris@0: // Create a valid HTML ID and make sure it is unique. Chris@0: if (!empty($variables['elements']['#id'])) { Chris@0: $variables['attributes']['id'] = Html::getUniqueId('block-' . $variables['elements']['#id']); Chris@0: } Chris@0: Chris@0: // Proactively add aria-describedby if possible to improve accessibility. Chris@0: if ($variables['label'] && isset($variables['attributes']['role'])) { Chris@0: $variables['title_attributes']['id'] = Html::getUniqueId($variables['label']); Chris@0: $variables['attributes']['aria-describedby'] = $variables['title_attributes']['id']; Chris@0: } Chris@0: Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements hook_ENTITY_TYPE_delete() for user_role entities. Chris@0: * Chris@0: * Removes deleted role from blocks that use it. Chris@0: */ Chris@0: function block_user_role_delete($role) { Chris@0: foreach (Block::loadMultiple() as $block) { Chris@0: /** @var $block \Drupal\block\BlockInterface */ Chris@0: $visibility = $block->getVisibility(); Chris@0: if (isset($visibility['user_role']['roles'][$role->id()])) { Chris@0: unset($visibility['user_role']['roles'][$role->id()]); Chris@0: $block->setVisibilityConfig('user_role', $visibility['user_role']); Chris@0: $block->save(); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements hook_ENTITY_TYPE_delete() for menu entities. Chris@0: */ Chris@0: function block_menu_delete(Menu $menu) { Chris@0: if (!$menu->isSyncing()) { Chris@0: foreach (Block::loadMultiple() as $block) { Chris@0: if ($block->getPluginId() == 'system_menu_block:' . $menu->id()) { Chris@0: $block->delete(); Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements hook_ENTITY_TYPE_delete() for 'configurable_language'. Chris@0: * Chris@0: * Delete the potential block visibility settings of the deleted language. Chris@0: */ Chris@0: function block_configurable_language_delete(ConfigurableLanguageInterface $language) { Chris@0: // Remove the block visibility settings for the deleted language. Chris@0: foreach (Block::loadMultiple() as $block) { Chris@0: /** @var $block \Drupal\block\BlockInterface */ Chris@0: $visibility = $block->getVisibility(); Chris@0: if (isset($visibility['language']['langcodes'][$language->id()])) { Chris@0: unset($visibility['language']['langcodes'][$language->id()]); Chris@0: $block->setVisibilityConfig('language', $visibility['language']); Chris@0: $block->save(); Chris@0: } Chris@0: } Chris@0: }