diff -u b/src/Plugin/Block/MenuBlock.php b/src/Plugin/Block/MenuBlock.php --- b/src/Plugin/Block/MenuBlock.php +++ b/src/Plugin/Block/MenuBlock.php @@ -80,11 +80,2 @@ */ - public function getConfiguration() { - $label = $this->getBlockLabel() ?: $this->label(); - $this->setConfigurationValue('label', $label); - return $this->configuration; - } - - /** - * {@inheritdoc} - */ public function blockForm($form, FormStateInterface $form_state) { @@ -325,9 +316,17 @@ $tree = $this->menuTree->transform($tree, $manipulators); $build = $this->menuTree->build($tree); + $label = $this->getBlockLabel() ?: $this->label(); + // Set the block's #title (label) to the dynamic value. + $build['#title'] = [ + '#markup' => $label, + ]; if (!empty($build['#theme'])) { // Add the configuration for use in menu_block_theme_suggestions_menu(). $build['#menu_block_configuration'] = $this->configuration; + // Set the generated label into the configuration array so it is + // propagated to the theme preprocessor and template(s) as needed. + $build['#menu_block_configuration']['label'] = $label; // Remove the menu name-based suggestion so we can control its precedence // better in menu_block_theme_suggestions_menu(). $build['#theme'] = 'menu'; only in patch2: unchanged: --- a/menu_block.module +++ b/menu_block.module @@ -100,3 +100,19 @@ function menu_block_theme_suggestions_menu(array $variables) { return $suggestions; } + +/** + * Implements hook_preprocess_hook() for "block". + * + * Set the block label with the #menu_block_configuration label if it exists. + */ +function menu_block_preprocess_block(&$variables) { + if (isset($variables['content']['#menu_block_configuration']['label'])) { + $config_label = $variables['content']['#menu_block_configuration']['label']; + // Some block twig templates (especially classy + bartik from core) use + // `{{ configuration.label }}` to print the label. Others just use + // `{{ label }}`. Therefore, we have to set both template variables for + // this to work consistently. + $variables['configuration']['label'] = $variables['label'] = $config_label; + } +}