-2

I was hoping it was possible to do something similar to this for menus:

 * Implementation of hook_theme_suggestions_HOOK_alter() for menus.
 *  * Make menus themeable:
 * e.g. => menu--main--node-type--page.html.twig
 */
function MYTHEME_theme_suggestions_menu_alter(array &$suggestions, array $variables) {
  if ($node = \Drupal::routeMatch()->getParameter('node')) {
    $content_type = $node->bundle();
    $suggestions[] = 'menu__'.$variables['menu_name'].'__node_type__%';
    $suggestions[] = 'menu__'.$variables['menu_name'].'__node_type__'.$content_type;
  }
}

But for blocks, because the main navigation block suggestions look like this for me:

 * block--system-menu-block--main.html.twig
 * block--system-menu-block.html.twig
 * block--system.html.twig
 * block.html.twig

And I was hoping that I could have something like block--system-menu-block--main--page.html.twig?

1 Answer 1

0

After trying more solutions in different answers, modifying this one finally worked for me: How to make a theme hook suggestion for blocks according to region?

use Drupal\block\Entity\Block;

function MYTHEME_theme_suggestions_block_alter(array &$suggestions, array $variables) {
  if (!empty($variables['elements']['#id'])) {
    $block = Block::load($variables['elements']['#id']);
    if ($node = \Drupal::routeMatch()->getParameter('node')) {
      $content_type = $node->bundle();
      $suggestions[] = 'block__'.$variables['elements']['#id'].'__node_type__'.$content_type;
    }
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.