1

I am having a need to dynamically change the title of a block. Here is what I am trying but no avail.

function MYMODULE_preprocess_block(&$variables) { if ($variables['elements']['#id'] == "views_block__weekly_schedule_all_blocks_block_2") { //$variables['elements']['content']['#title']['#markup'] = "Foobar"; //$variables['elements']['content']['#title'] = 'foibar'; } } 

Any help is appreciated.

2 Answers 2

3

See the block_example.module. It's hook_block_view_alter you can use to do this programmatically. You can probably use it in a *.theme as well.

Source block_example_block_view_alter.

use Drupal\Core\Block\BlockPluginInterface; function MYMODULE_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']); } } 

Most often you find answers to questions like that really fast when you google for Drupal X how to X programmatically or similar.

0

$variables['label'] = 'My label';

Source: https://api.drupal.org/api/drupal/core%21modules%21block%21block.module/function/template_preprocess_block/8.2.x

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.