Support Menu Block's custom theme setting

Created on 20 January 2023, over 1 year ago
Updated 3 July 2023, 12 months ago

Problem/Motivation

Menu Block module allows a user to key in a specific hook for theming or template suggestion. When Menu Item Extras activates, a higher level of template specificity is introduced. The suggestions only contain the menu id, but not the custom setting that was entered. This can be problematic because if you add and remove fields from a menu, it affects which twig template is used.

Steps to reproduce

Create a Menu Block with the Menu Block contributed module and change the theme hook on the block form. It will not be respected even if a twig template exists once Menu Item Extras' theme code activates.

Proposed resolution

Add an additional check that ensures this hook setting makes it into the suggestion list and is not superceded by MIE twig suggestions.

In my custom theme, I had to do this to ensure I could influence what hook/twig template was used on the Menu Block form:

/**
 * Implements hook_theme_suggestions_HOOK_alter().
 */
function sfp_theme_suggestions_menu_alter(array &$suggestions, array $variables) {
  if (isset($variables['menu_name'])) {
    /** @var \Drupal\menu_item_extras\Utility\Utility $utility */
    $utility = \Drupal::service('menu_item_extras.utility');
    if (Utility::checkBundleHasExtraFieldsThanEntity('menu_link_content', $variables['menu_name'])) {
      // menu_item_extras suggestions are taking precedent over the theme hook in menu_block
      // we need to re-take control here to support multiple menus.
      if (!empty($variables["menu_block_configuration"]["suggestion"])) {
        $suggestion_prefix = 'menu__extras';
        $suggestions[] = $utility::suggestion($suggestion_prefix, $variables["menu_block_configuration"]["suggestion"]);
      }
    }
  }
}

That way I can add several menu blocks and point them at the same twig template without affecting other menu blocks that already exist.

✨ Feature request
Status

Active

Version

2.19

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States kevinquillen

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States studgate

    this works for me, great job @kevinquillen

  • πŸ‡ΊπŸ‡ΈUnited States kevinquillen

    Encountered this again and made a slight change on the project I am on.

    /**
     * Implements hook_theme_suggestions_HOOK_alter().
     */
    function mytheme_theme_suggestions_menu_alter(array &$suggestions, array $variables) {
      if (isset($variables['menu_name'])) {
        /** @var \Drupal\menu_item_extras\Utility\Utility $utility */
        $utility = \Drupal::service('menu_item_extras.utility');
        if (Utility::checkBundleHasExtraFieldsThanEntity('menu_link_content', $variables['menu_name'])) {
          // menu_item_extras suggestions are taking precedent.
          // we need to re-take control here to support multiple menus.
          if (!empty($variables["menu_block_configuration"]["suggestion"])) {
            $suggestion_prefix = 'menu__extras';
            $suggestions[] = $utility::suggestion($suggestion_prefix, $variables["menu_block_configuration"]["suggestion"]);
          }
    
          $suggestions = array_reverse(array_unique($suggestions));
        }
      }
    }
    

    For some reason I was seeing two menu--main.html.twig, and all the 'extras' suggestions appeared first. I reversed the array so I get the preferred behavior - use my standard templates that are in place if they exist in suggestions, before loading up menu--extras template(s).

Production build 0.69.0 2024