block_example.module
2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
/**
* @file
* Module file for block_example.
*/
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Block\BlockPluginInterface;
/**
* @defgroup block_example Example: Block
* @ingroup examples
* @{
* Demonstrates code creation of blocks.
*
* This is an example outlining how a module can define blocks that can be
* displayed on various pages of a site, or 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.
*
* In addition, instead of hook_block_view_alter(), which is called for all
* blocks, you can also use hook_block_view_MODULE_DELTA_alter() to alter a
* specific block. To change only our block using
* hook_block_view_MODULE_DELTA_alter, we would use the function:
* block_example_block_view_block_example_example_configurable_text_alter()
*
* We are going to uppercase the subject (the title of the block as shown to the
* user) of any block if the string "uppercase" appears in the block title or
* subject. Default block titles are set programmatically in the subject key;
* titles created through the UI are saved in the title key. This module creates
* an example block to demonstrate this effect (default title set
* programmatically as subject). You can also demonstrate the effect of this
* hook by creating a new block whose title has the string 'uppercase' in it
* (set as title through the UI).
*/
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']) && stristr($build['#configuration']['label'], 'uppercase')) || (!empty($definition['subject']) && stristr($definition['subject'], 'uppercase'))) {
// This will uppercase the block title.
$build['#configuration']['label'] = Unicode::strtoupper($build['#configuration']['label']);
}
}
/**
* @} End of "defgroup block_example".
*/