block.module
Same filename and directory in other branches
File
-
core/
modules/ block/ block.module
View source
<?php
/**
* @file
*/
use Drupal\block\Hook\BlockHooks;
/**
* Assigns an initial, default set of blocks for a theme.
*
* 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
* to the new theme's default region.
*
* @param string $theme
* The name of a theme.
*/
function block_theme_initialize($theme) : void {
// Initialize theme's blocks if none already registered.
$has_blocks = \Drupal::entityTypeManager()->getStorage('block')
->loadByProperties([
'theme' => $theme,
]);
if (!$has_blocks) {
$default_theme = \Drupal::config('system.theme')->get('default');
// Apply only to new theme's visible regions.
$regions = system_region_list($theme, REGIONS_VISIBLE);
$default_theme_blocks = \Drupal::entityTypeManager()->getStorage('block')
->loadByProperties([
'theme' => $default_theme,
]);
foreach ($default_theme_blocks as $default_theme_block_id => $default_theme_block) {
if (str_starts_with($default_theme_block_id, $default_theme . '_')) {
$id = str_replace($default_theme . '_', '', $default_theme_block_id);
}
else {
$id = $default_theme_block_id;
}
$id = \Drupal::service('block.repository')->getUniqueMachineName($id, $theme);
$block = $default_theme_block->createDuplicateBlock($id, $theme);
// If the region isn't supported by the theme, assign the block to the
// theme's default region.
if (!isset($regions[$block->getRegion()])) {
$block->setRegion(system_default_region($theme));
}
$block->save();
}
}
}
/**
* Prepares variables for block templates.
*
* Default template: block.html.twig.
*
* Prepares the values passed to the theme_block function to be passed
* into a pluggable template engine. Uses block properties to generate a
* series of template file suggestions. If none are found, the default
* block.html.twig is used.
*
* 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.
*
* @param array $variables
* An associative array containing:
* - elements: An associative array containing the properties of the element.
* Properties used: #block, #configuration, #children, #plugin_id.
*
* @deprecated in drupal:11.2.0 and is removed from drupal:12.0.0. Initial
* template_preprocess functions are registered directly in hook_theme().
*
* @see https://www.drupal.org/node/3504125
*/
function template_preprocess_block(&$variables) : void {
@trigger_error(__FUNCTION__ . '() is deprecated in drupal:11.2.0 and is removed from drupal:12.0.0. Initial template_preprocess functions are registered directly in hook_theme(). See https://www.drupal.org/node/3504125', E_USER_DEPRECATED);
\Drupal::service(BlockHooks::class)->preprocessBlock($variables);
}
Functions
Title | Deprecated | Summary |
---|---|---|
block_theme_initialize | Assigns an initial, default set of blocks for a theme. | |
template_preprocess_block | in drupal:11.2.0 and is removed from drupal:12.0.0. Initial template_preprocess functions are registered directly in hook_theme(). |
Prepares variables for block templates. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.