I'm attempting to use hook_block_view_alter() to set a block title to the name of the page's parent menu item.

The block is a menu block for the main menu that is showing second-level items.

I've found that this works to set the title:

 * Implements hook_block_view_alter().
function nama_components_block_view_alter(array &$build, \Drupal\Core\Block\BlockPluginInterface $block) {
  if ($build['#id'] == 'nama_d8_main_menu') {
    $parent_menu_item = 'just testing!';
    $build['#configuration']['label'] = $parent_menu_item;
    $build['#configuration']['label_display'] = 1;

From here, how would I look up the parent menu item from the block so I can actually set $parent_menu_item? I've looked through what I'm getting in the $build and $block parameters, but I'm just not seeing it.

  • Did you ever find out how to do this? Jan 25, 2017 at 22:38
  • @ScottAnderson Ugh, no, I ended up kinda faking it with JavaScript on that project. I suppose it did solve the problem in that case, but there's got to be a better way. I ended up with this JS in a module (included via a moduleName.libraries.yml file). You'd of course need to adjust the selectors shown there. Hope you find a better solution! Jan 27, 2017 at 0:28


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.