block_example.module
Same filename in other branches
Module file for block_example.
File
-
modules/
block_example/ block_example.module
View source
<?php
/**
* @file
* Module file for block_example.
*/
use Drupal\Core\Block\BlockPluginInterface;
/**
* @defgroup block_example Example: Block
* @ingroup examples
* @{
* Demonstrates code creation of blocks.
*
* This example demonstrates how a module can define blocks that can be
* displayed on various pages of a site, and how to alter blocks provided by
* other modules.
*/
/**
* Implements hook_block_view_alter().
*
* This hook allows you to modify the output of any block in the system.
*
* We are going to change the block label to uppercase if it contains the string
* "uppercase" or if the default block label contains this string. The default
* block label is set programmatically in the subject key of the block's plugin
* definition. The configurable block label, which can be overridden through the
* UI, is found in the "#configuration" key of the block's build definition.
* This module creates a block that demonstrates the uppercase effect in the
* "Example: uppercase this please" block. You can also demonstrate the effect
* of this hook by editing the title of an existing block or by creating a new
* block which where the default label has the string "uppercase" in it.
*
* Instead of hook_block_view_alter(), which is called for all blocks, you can
* also use hook_block_view_BASE_BLOCK_ID_alter() to alter a specific block. To
* only change the "example_uppercase" block we would use the function:
* hook_block_view_example_uppercase_alter().
*/
function block_example_block_view_alter(array &$build, BlockPluginInterface $block) {
// We'll search for the string 'uppercase'.
$definition = $block->getPluginDefinition();
if (!empty($build['#configuration']['label']) && mb_strpos($build['#configuration']['label'], 'uppercase') || !empty($definition['subject']) && mb_strpos($definition['subject'], 'uppercase')) {
// This will uppercase the block title.
$build['#configuration']['label'] = mb_strtoupper($build['#configuration']['label']);
}
}
/**
* @} End of "defgroup block_example".
*/
Functions
Title | Deprecated | Summary |
---|---|---|
block_example_block_view_alter | Implements hook_block_view_alter(). |