2001-03-10 11:07:52 +00:00
<?php
2000-12-23 15:20:10 +00:00
2004-08-21 06:42:38 +00:00
/**
* @file
2009-08-28 19:44:05 +00:00
* Controls the visual building blocks a page is constructed with.
2004-08-21 06:42:38 +00:00
*/
2013-10-25 17:16:02 +00:00
use Drupal\block\BlockInterface;
2015-03-05 09:37:01 +00:00
use Drupal\Component\Utility\Html;
2014-06-30 03:33:08 +00:00
use Drupal\Core\Routing\RouteMatchInterface;
2014-09-29 13:41:29 +00:00
use Drupal\Core\Url;
2014-10-23 11:32:15 +00:00
use Drupal\language\ConfigurableLanguageInterface;
2014-04-04 13:49:13 +00:00
use Drupal\system\Entity\Menu;
2014-09-08 14:15:48 +00:00
use Drupal\block\Entity\Block;
2013-01-08 21:01:41 +00:00
2004-05-11 20:10:14 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_help().
2004-05-11 20:10:14 +00:00
*/
2014-06-30 03:33:08 +00:00
function block_help($route_name, RouteMatchInterface $route_match) {
2014-05-07 02:04:53 +00:00
switch ($route_name) {
case 'help.page.block':
2009-11-19 05:54:42 +00:00
$output = '';
2009-11-22 21:24:37 +00:00
$output .= '<h3>' . t('About') . '</h3>';
2013-12-03 15:44:48 +00:00
$output .= '<p>' . t('The Block module allows you to place blocks in regions and to configure their settings. For more information, see <a href="!blocks-documentation">the online documentation for the Blocks module</a>.', array('!blocks-documentation' => 'https://drupal.org/documentation/modules/block/')) . '</p>';
2009-11-19 05:54:42 +00:00
$output .= '<h3>' . t('Uses') . '</h3>';
$output .= '<dl>';
2013-12-03 15:44:48 +00:00
$output .= '<dt>' . t('Placing and moving blocks') . '</dt>';
2014-11-28 15:14:44 +00:00
$output .= '<dd>' . t('You can place a block by clicking on its title in the <em>Place blocks</em> list on the <a href="!blocks">Block layout page</a>. You can then choose the appropriate region from the <em>Region</em> dropdown menu. Once a block has been placed, it can also be moved to a different region by choosing a region from the <em>Region</em> dropdown menu on the Block layout page, or by dragging and dropping it to the right position.', array('!blocks' => \Drupal::url('block.admin_display'))) . '</dd>';
2013-12-03 15:44:48 +00:00
$output .= '<dt>' . t('Demonstrating block regions for a theme') . '</dt>';
2014-11-28 15:14:44 +00:00
$output .= '<dd>' . t('You can see which region is where in a theme by clicking on the <em>Demonstrate block regions</em> link on the <a href="!blocks">Block layout page</a>. Regions are specific to each theme, so you need to toggle to a different theme first to demonstrate its block regions.', array('!blocks' => \Drupal::url('block.admin_display'))) . '</dd>';
2013-12-03 15:44:48 +00:00
$output .= '<dt>' . t('Toggling between different themes') . '</dt>';
2014-09-19 09:25:26 +00:00
$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>';
2013-12-03 15:44:48 +00:00
$output .= '<dt>' . t('Configuring block settings') . '</dt>';
$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.', array('!blocks' => Drupal::url('block.admin_display'))) . '</dd>';
2009-11-19 05:54:42 +00:00
$output .= '<dt>' . t('Controlling visibility') . '</dt>';
2013-12-03 15:44:48 +00:00
$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>';
$output .= '<dt>' . t('Adding custom blocks') . '</dt>';
2014-12-02 16:50:28 +00:00
$output .= '<dd>' . t('You can add custom blocks, if the <em>Custom Block</em> module is installed on the <a href="!extend">Extend page</a>. For more information, see the <a href="!blockcontent-help">Custom Block help page</a>.', array('!extend' => \Drupal::url('system.modules_list'), '!blockcontent-help' => \Drupal::url('help.page', array('name' => 'block_content')))) . '</dd>';
2009-11-19 05:54:42 +00:00
$output .= '</dl>';
2005-11-01 10:17:34 +00:00
return $output;
2003-08-05 18:33:39 +00:00
}
2014-05-07 02:04:53 +00:00
if ($route_name == 'block.admin_display' || $route_name == 'block.admin_display_theme') {
2014-06-30 03:33:08 +00:00
$demo_theme = $route_match->getParameter('theme') ?: \Drupal::config('system.theme')->get('default');
2015-01-13 10:17:01 +00:00
$themes = \Drupal::service('theme_handler')->listInfo();
2013-08-15 15:28:57 +00:00
$output = '<p>' . t('This page provides a drag-and-drop interface for adding a block to a region, and for controlling the order of blocks within regions. To add a block to a region, or to configure its specific title and visibility settings, click the block title under <em>Place blocks</em>. Since not all themes implement the same regions, or display regions in the same way, blocks are positioned on a per-theme basis. Remember that your changes will not be saved until you click the <em>Save blocks</em> button at the bottom of the page.') . '</p>';
2015-03-09 15:57:27 +00:00
$output .= '<p>' . \Drupal::l(t('Demonstrate block regions (@theme)', array('@theme' => $themes[$demo_theme]->info['name'])), new Url('block.admin_demo', array('theme' => $demo_theme))) . '</p>';
2009-10-14 02:13:15 +00:00
return $output;
}
2001-01-13 16:33:19 +00:00
}
2007-04-06 13:27:23 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_theme().
2007-04-06 13:27:23 +00:00
*/
function block_theme() {
return array(
2009-04-26 01:15:04 +00:00
'block' => array(
2009-10-23 22:24:19 +00:00
'render element' => 'elements',
2009-05-24 17:39:35 +00:00
),
2013-09-04 20:42:50 +00:00
'block_list' => array(
'render element' => 'form',
),
2007-04-06 13:27:23 +00:00
);
}
2009-01-27 00:22:27 +00:00
/**
2014-11-14 10:43:20 +00:00
* Implements hook_page_top().
2009-01-27 00:22:27 +00:00
*/
2014-11-14 10:43:20 +00:00
function block_page_top(array &$page_top) {
if (\Drupal::routeMatch()->getRouteName() === 'block.admin_demo') {
$theme = \Drupal::theme()->getActiveTheme()->getName();
$page_top['backlink'] = array(
2013-08-10 09:03:40 +00:00
'#type' => 'link',
'#title' => t('Exit block region demonstration'),
2013-11-26 22:07:54 +00:00
'#options' => array('attributes' => array('class' => array('block-demo-backlink'))),
2013-08-10 09:03:40 +00:00
'#weight' => -10,
);
2014-10-09 06:39:37 +00:00
if (\Drupal::config('system.theme')->get('default') == $theme) {
2014-11-14 10:43:20 +00:00
$page_top['backlink']['#url'] = Url::fromRoute('block.admin_display');
2014-10-09 06:39:37 +00:00
}
else {
2014-11-14 10:43:20 +00:00
$page_top['backlink']['#url'] = Url::fromRoute('block.admin_display_theme', ['theme' => $theme]);
2014-10-09 06:39:37 +00:00
}
2009-01-27 00:22:27 +00:00
}
}
2002-10-30 08:12:28 +00:00
/**
Issue #1535868 by EclipseGc, tim.plunkett, xjm, Jody Lynn, sdboyer, naxoc, tizzo, effulgentsia, dawehner, disasm, beejeebus: Convert all blocks into plugins.
2013-01-04 17:05:13 +00:00
* Returns an array of block class instances by theme.
2002-11-01 10:47:20 +00:00
*
2011-05-24 01:07:02 +00:00
* @param $theme
2009-10-14 02:13:15 +00:00
* The theme to rehash blocks for. If not provided, defaults to the currently
* used theme.
2009-11-10 17:27:54 +00:00
*
2004-05-11 20:10:14 +00:00
* @return
2006-04-04 23:32:40 +00:00
* Blocks currently exported by modules.
2002-10-30 08:12:28 +00:00
*/
2009-10-14 02:13:15 +00:00
function _block_rehash($theme = NULL) {
2013-09-16 03:58:06 +00:00
$theme = $theme ? $theme : \Drupal::config('system.theme')->get('default');
Issue #1535868 by EclipseGc, tim.plunkett, xjm, Jody Lynn, sdboyer, naxoc, tizzo, effulgentsia, dawehner, disasm, beejeebus: Convert all blocks into plugins.
2013-01-04 17:05:13 +00:00
$regions = system_region_list($theme);
2013-01-17 04:03:30 +00:00
$blocks = entity_load_multiple_by_properties('block', array('theme' => $theme));
foreach ($blocks as $block_id => $block) {
2013-09-20 10:21:32 +00:00
// Remove any invalid block from the list.
// @todo Remove this check as part of https://drupal.org/node/1776830.
if (!$block->getPlugin()) {
unset($blocks[$block_id]);
continue;
}
Issue #2030571 by calebtr, daffie, filijonka, YesCT, Thomas Brekelmans, alexpott, Mile23, tadityar, Sharique, tim.plunkett, boztek: Expand Block with methods
2015-01-12 17:47:19 +00:00
$region = $block->getRegion();
2013-02-06 12:00:39 +00:00
$status = $block->status();
Issue #1535868 by EclipseGc, tim.plunkett, xjm, Jody Lynn, sdboyer, naxoc, tizzo, effulgentsia, dawehner, disasm, beejeebus: Convert all blocks into plugins.
2013-01-04 17:05:13 +00:00
// Disable blocks in invalid regions.
2013-10-25 17:16:02 +00:00
if (!empty($region) && $region != BlockInterface::BLOCK_REGION_NONE && !isset($regions[$region]) && $status) {
2013-01-17 04:03:30 +00:00
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');
2014-12-02 16:50:28 +00:00
// Disabled blocks are moved into the BlockInterface::BLOCK_REGION_NONE
2013-10-25 17:16:02 +00:00
// later so no need to move the block to another region.
2013-02-06 12:00:39 +00:00
$block->disable()->save();
2010-06-15 16:19:28 +00:00
}
2013-02-06 12:00:39 +00:00
// Set region to none if not enabled.
2014-03-05 20:38:18 +00:00
if (!$status && $region != BlockInterface::BLOCK_REGION_NONE) {
Issue #2030571 by calebtr, daffie, filijonka, YesCT, Thomas Brekelmans, alexpott, Mile23, tadityar, Sharique, tim.plunkett, boztek: Expand Block with methods
2015-01-12 17:47:19 +00:00
$block->setRegion(BlockInterface::BLOCK_REGION_NONE);
2013-01-17 04:03:30 +00:00
$block->save();
2007-11-06 11:40:15 +00:00
}
2006-08-20 06:49:15 +00:00
}
2002-10-26 15:17:26 +00:00
return $blocks;
}
2001-01-26 13:38:46 +00:00
2009-04-21 09:31:31 +00:00
/**
2014-09-19 09:25:26 +00:00
* Initializes blocks for installed themes.
2011-11-03 10:53:06 +00:00
*
* @param $theme_list
* An array of theme names.
2009-04-21 09:31:31 +00:00
*/
2014-09-19 09:25:26 +00:00
function block_themes_installed($theme_list) {
2009-12-01 00:39:35 +00:00
foreach ($theme_list as $theme) {
block_theme_initialize($theme);
2009-04-21 09:31:31 +00:00
}
}
/**
2011-11-03 10:53:06 +00:00
* Assigns an initial, default set of blocks for a theme.
2009-04-26 16:30:28 +00:00
*
2014-09-19 09:25:26 +00:00
* This function is called the first time a new theme is installed. The new
* theme gets a copy of the default theme's blocks, with the difference that if
* a particular region isn't available in the new theme, the block is assigned
2009-04-21 09:31:31 +00:00
* to the new theme's default region.
2009-04-26 16:30:28 +00:00
*
2009-04-21 09:31:31 +00:00
* @param $theme
* The name of a theme.
*/
2009-07-14 10:22:17 +00:00
function block_theme_initialize($theme) {
2009-04-21 09:31:31 +00:00
// Initialize theme's blocks if none already registered.
2013-01-17 04:03:30 +00:00
$has_blocks = entity_load_multiple_by_properties('block', array('theme' => $theme));
2009-05-16 15:23:16 +00:00
if (!$has_blocks) {
2013-09-16 03:58:06 +00:00
$default_theme = \Drupal::config('system.theme')->get('default');
2011-07-03 17:57:21 +00:00
// Apply only to new theme's visible regions.
$regions = system_region_list($theme, REGIONS_VISIBLE);
2013-01-17 04:03:30 +00:00
$default_theme_blocks = entity_load_multiple_by_properties('block', array('theme' => $default_theme));
foreach ($default_theme_blocks as $default_theme_block_id => $default_theme_block) {
2013-10-16 00:51:01 +00:00
if (strpos($default_theme_block_id, $default_theme . '_') === 0) {
$id = str_replace($default_theme, $theme, $default_theme_block_id);
}
else {
$id = $theme . '_' . $default_theme_block_id;
}
Issue #2030571 by calebtr, daffie, filijonka, YesCT, Thomas Brekelmans, alexpott, Mile23, tadityar, Sharique, tim.plunkett, boztek: Expand Block with methods
2015-01-12 17:47:19 +00:00
$block = $default_theme_block->createDuplicateBlock($id, $theme);
Issue #1535868 by EclipseGc, tim.plunkett, xjm, Jody Lynn, sdboyer, naxoc, tizzo, effulgentsia, dawehner, disasm, beejeebus: Convert all blocks into plugins.
2013-01-04 17:05:13 +00:00
// If the region isn't supported by the theme, assign the block to the
// theme's default region.
Issue #2030571 by calebtr, daffie, filijonka, YesCT, Thomas Brekelmans, alexpott, Mile23, tadityar, Sharique, tim.plunkett, boztek: Expand Block with methods
2015-01-12 17:47:19 +00:00
if (!isset($regions[$block->getRegion()])) {
$block->setRegion(system_default_region($theme));
Issue #1535868 by EclipseGc, tim.plunkett, xjm, Jody Lynn, sdboyer, naxoc, tizzo, effulgentsia, dawehner, disasm, beejeebus: Convert all blocks into plugins.
2013-01-04 17:05:13 +00:00
}
2013-01-17 04:03:30 +00:00
$block->save();
2009-04-21 09:31:31 +00:00
}
}
}
2012-05-03 15:09:39 +00:00
/**
* Implements hook_rebuild().
*/
function block_rebuild() {
2015-01-13 10:17:01 +00:00
foreach (\Drupal::service('theme_handler')->listInfo() as $name => $data) {
2012-10-09 20:32:40 +00:00
if ($data->status) {
_block_rehash($name);
}
2010-06-15 16:19:28 +00:00
}
2009-02-03 12:30:14 +00:00
}
2009-04-26 01:15:04 +00:00
2013-09-29 07:19:59 +00:00
/**
* Implements hook_theme_suggestions_HOOK().
*/
function block_theme_suggestions_block(array $variables) {
$suggestions = array();
2014-04-01 21:14:13 +00:00
$suggestions[] = 'block__' . $variables['elements']['#configuration']['provider'];
2013-09-29 07:19:59 +00:00
// Hyphens (-) and underscores (_) play a special role in theme suggestions.
// Theme suggestions should only contain underscores, because within
// drupal_find_theme_templates(), underscores are converted to hyphens to
// match template file names, and then converted back to underscores to match
// pre-processing and other function names. So if your theme suggestion
// contains a hyphen, it will end up as an underscore after this conversion,
// and your function names won't be recognized. So, we need to convert
// hyphens to underscores in block deltas for the theme suggestions.
// We can safely explode on : because we know the Block plugin type manager
// enforces that delimiter for all derivatives.
$parts = explode(':', $variables['elements']['#plugin_id']);
$suggestion = 'block';
while ($part = array_shift($parts)) {
$suggestions[] = $suggestion .= '__' . strtr($part, '-', '_');
}
2014-06-22 05:10:13 +00:00
if (!empty($variables['elements']['#id'])) {
$suggestions[] = 'block__' . $variables['elements']['#id'];
2013-09-29 07:19:59 +00:00
}
return $suggestions;
}
2009-04-26 01:15:04 +00:00
/**
Issue #1898034 by Cottser, jenlampton, joelpittet, Shawn DeArmond, idflood, Hydra, artofeclipse, rich.yumul, chrisjlee, gnuget, c4rl, thund3rbox: block.module - Convert PHPTemplate templates to Twig.
2013-05-24 17:14:30 +00:00
* Prepares variables for block templates.
*
* Default template: block.html.twig.
2009-04-26 01:15:04 +00:00
*
2011-11-03 10:53:06 +00:00
* Prepares the values passed to the theme_block function to be passed
2009-04-26 01:15:04 +00:00
* into a pluggable template engine. Uses block properties to generate a
* series of template file suggestions. If none are found, the default
Issue #1898034 by Cottser, jenlampton, joelpittet, Shawn DeArmond, idflood, Hydra, artofeclipse, rich.yumul, chrisjlee, gnuget, c4rl, thund3rbox: block.module - Convert PHPTemplate templates to Twig.
2013-05-24 17:14:30 +00:00
* block.html.twig is used.
2009-04-26 01:15:04 +00:00
*
Issue #1898034 by Cottser, jenlampton, joelpittet, Shawn DeArmond, idflood, Hydra, artofeclipse, rich.yumul, chrisjlee, gnuget, c4rl, thund3rbox: block.module - Convert PHPTemplate templates to Twig.
2013-05-24 17:14:30 +00:00
* Most themes use their own copy of block.html.twig. The default is located
* inside "core/modules/block/templates/block.html.twig". Look in there for the
* full list of available variables.
2009-04-26 01:15:04 +00:00
*
Issue #1898034 by Cottser, jenlampton, joelpittet, Shawn DeArmond, idflood, Hydra, artofeclipse, rich.yumul, chrisjlee, gnuget, c4rl, thund3rbox: block.module - Convert PHPTemplate templates to Twig.
2013-05-24 17:14:30 +00:00
* @param array $variables
* An associative array containing:
* - elements: An associative array containing the properties of the element.
* Properties used: #block, #configuration, #children, #plugin_id.
2009-04-26 01:15:04 +00:00
*/
function template_preprocess_block(&$variables) {
2013-05-11 10:01:25 +00:00
$variables['configuration'] = $variables['elements']['#configuration'];
$variables['plugin_id'] = $variables['elements']['#plugin_id'];
2013-12-19 17:57:48 +00:00
$variables['base_plugin_id'] = $variables['elements']['#base_plugin_id'];
$variables['derivative_plugin_id'] = $variables['elements']['#derivative_plugin_id'];
2013-05-11 10:01:25 +00:00
$variables['label'] = !empty($variables['configuration']['label_display']) ? $variables['configuration']['label'] : '';
$variables['content'] = $variables['elements']['content'];
2014-03-22 22:00:38 +00:00
// A block's label is configuration: it is static. Allow dynamic labels to be
// set in the render array.
if (isset($variables['elements']['content']['#title']) && !empty($variables['configuration']['label_display'])) {
$variables['label'] = $variables['elements']['content']['#title'];
}
2009-05-21 21:12:25 +00:00
2010-04-26 14:10:40 +00:00
// Create a valid HTML ID and make sure it is unique.
2014-06-22 05:10:13 +00:00
if (!empty($variables['elements']['#id'])) {
2015-03-05 09:37:01 +00:00
$variables['attributes']['id'] = Html::getUniqueId('block-' . $variables['elements']['#id']);
Issue #1535868 by EclipseGc, tim.plunkett, xjm, Jody Lynn, sdboyer, naxoc, tizzo, effulgentsia, dawehner, disasm, beejeebus: Convert all blocks into plugins.
2013-01-04 17:05:13 +00:00
}
2014-06-13 02:46:38 +00:00
// Proactively add aria-describedby if possible to improve accessibility.
2014-06-22 05:10:13 +00:00
if ($variables['label'] && isset($variables['attributes']['role'])) {
2015-03-05 09:37:01 +00:00
$variables['title_attributes']['id'] = Html::getUniqueId($variables['label']);
2014-06-22 05:10:13 +00:00
$variables['attributes']['aria-describedby'] = $variables['title_attributes']['id'];
2014-06-13 02:46:38 +00:00
}
2009-05-28 16:44:07 +00:00
}
2009-08-26 10:29:26 +00:00
2009-08-27 20:25:29 +00:00
/**
2014-07-11 12:04:53 +00:00
* Implements hook_ENTITY_TYPE_delete() for user_role entities.
2009-08-27 20:25:29 +00:00
*
2011-11-03 10:53:06 +00:00
* Removes deleted role from blocks that use it.
2009-08-27 20:25:29 +00:00
*/
function block_user_role_delete($role) {
2014-09-08 14:15:48 +00:00
foreach (Block::loadMultiple() as $block) {
2014-06-17 17:53:40 +00:00
/** @var $block \Drupal\block\BlockInterface */
$visibility = $block->getVisibility();
if (isset($visibility['user_role']['roles'][$role->id()])) {
unset($visibility['user_role']['roles'][$role->id()]);
$block->getPlugin()->setVisibilityConfig('user_role', $visibility['user_role']);
2013-01-17 04:03:30 +00:00
$block->save();
Issue #1535868 by EclipseGc, tim.plunkett, xjm, Jody Lynn, sdboyer, naxoc, tizzo, effulgentsia, dawehner, disasm, beejeebus: Convert all blocks into plugins.
2013-01-04 17:05:13 +00:00
}
}
2009-08-27 20:25:29 +00:00
}
2009-10-09 08:02:25 +00:00
/**
2014-07-11 12:04:53 +00:00
* Implements hook_ENTITY_TYPE_delete() for menu entities.
2009-10-09 08:02:25 +00:00
*/
2014-04-04 13:49:13 +00:00
function block_menu_delete(Menu $menu) {
if (!$menu->isSyncing()) {
2014-09-08 14:15:48 +00:00
foreach (Block::loadMultiple() as $block) {
Issue #2030571 by calebtr, daffie, filijonka, YesCT, Thomas Brekelmans, alexpott, Mile23, tadityar, Sharique, tim.plunkett, boztek: Expand Block with methods
2015-01-12 17:47:19 +00:00
if ($block->getPluginId() == 'system_menu_block:' . $menu->id()) {
2014-04-04 13:49:13 +00:00
$block->delete();
}
Issue #1535868 by EclipseGc, tim.plunkett, xjm, Jody Lynn, sdboyer, naxoc, tizzo, effulgentsia, dawehner, disasm, beejeebus: Convert all blocks into plugins.
2013-01-04 17:05:13 +00:00
}
}
2009-10-09 08:02:25 +00:00
}
2012-04-19 17:45:46 +00:00
/**
2014-08-10 21:19:32 +00:00
* Implements hook_ENTITY_TYPE_delete() for 'configurable_language'.
2012-04-19 17:45:46 +00:00
*
* Delete the potential block visibility settings of the deleted language.
*/
2014-10-23 11:32:15 +00:00
function block_configurable_language_delete(ConfigurableLanguageInterface $language) {
2012-04-19 17:45:46 +00:00
// Remove the block visibility settings for the deleted language.
2014-09-08 14:15:48 +00:00
foreach (Block::loadMultiple() as $block) {
2014-06-17 17:53:40 +00:00
/** @var $block \Drupal\block\BlockInterface */
$visibility = $block->getVisibility();
2014-03-25 19:51:00 +00:00
if (isset($visibility['language']['langcodes'][$language->id()])) {
unset($visibility['language']['langcodes'][$language->id()]);
2014-11-19 21:03:06 +00:00
$block->setVisibilityConfig('language', $visibility['language']);
2013-01-17 04:03:30 +00:00
$block->save();
Issue #1535868 by EclipseGc, tim.plunkett, xjm, Jody Lynn, sdboyer, naxoc, tizzo, effulgentsia, dawehner, disasm, beejeebus: Convert all blocks into plugins.
2013-01-04 17:05:13 +00:00
}
}
2010-03-09 12:09:52 +00:00
}