Problem/Motivation

Currently, when one wants to display multiple level menus, the only output available is dropdown. However, it may be the case that a themer may want a menu, without dropdowns, such as with a vertical menu in the sidebar or footer.

Steps to reproduce

  1. Ensure the main menu has at least one menu with a sub-link.
  2. Under Structure > Block Layout, place a Main Menu block within the Content region.

Proposed resolution

Dropdown links in nav should be optional.

There are a myriad of ways a themer may tackle this, but to provide a simple approach, we should consider making dropdown an optional config within the nav component.

CommentFileSizeAuthor
#13 radix-3260136-13.patch528 bytesandyz
#10 radix-3260136-10.patch926 bytesdkosbob

Issue fork radix-3260136

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

jcandan created an issue. See original summary.

doxigo’s picture

Hey James, thanks for the suggestion, at this point we probably won't be adding features to 4.x version but rather work on 5.x, we can backport it if applicable but please feel free to provide a MR

Thanks

jcandan’s picture

Not sure that my default boolean for is_dropdown is a good implementation. Is there a better way to set default to true and ensure only a boolean is accepted? My twig-fu is lacking a little.

jcandan’s picture

For those wishing to know how to target menus within a region, this may be helpful:

/**
 * Implements hook_theme_suggestions_HOOK_alter().
 */
function my_radix_preprocess_block(&$variables) {
  // Add the block ID as custom attribute to block content, this will be used
  // for menu template suggestions.
  $variables['content']['#attributes']['block'] = $variables['elements']['#id'];
}

/**
 * Implements hook_theme_suggestions_HOOK_alter().
 */
function my_radix_theme_suggestions_menu_alter(array &$suggestions, array $variables) {
  $menu_name = $variables['menu_name'];
  if (isset($variables['attributes']['block'])) {
    $block = \Drupal\block\Entity\Block::load($variables['attributes']['block']);
    $region = $block->getRegion();
    $suggestions[] = 'menu__' . $region . '__' . $menu_name;
  }
}

With the above, I would be able to target a the main menu within the content region, and specify that it not be dropdown links.

{#
/**
 * Template for the main menu within the content region.
 * @file menu--content--main.html.twig
 */
#}
{% include '@radix/nav/nav.twig' with {
  is_dropdown: false,
  alignment: 'vertical'
} %}

  • doxigo committed 869d2f0 on 8.x-4.x authored by jcandan
    Issue #3260136 by jcandan: Make nav default to dropdown links optional
    
doxigo’s picture

Status: Active » Fixed

Thanks for the work James, merged

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

doxigo’s picture

Status: Closed (fixed) » Needs work

James seems like there's a typo + some issues of importing the same `nav.twig` file within itself, I have to revert this or please provide a MR

dkosbob’s picture

StatusFileSize
new926 bytes

This change also has the unintended effect of applying the .dropdown class on each nav_item unless is_dropdown is explicitly set to false/null, even if there are no children menu items and the rest of the dropdown code isn't executed.

I've made a change to address that and fix the typo mentioned. The template is including itself on line 56 - I don't know whether twig allows that or not. It doesn't seem to cause any problems in my tests.

Sorry for the patch. I tried to fork or re-open the fork but couldn't quite figure it out.

doxigo’s picture

Status: Needs work » Closed (outdated)

No longer supported/outdated, closing - feel free to open a MR if applicable.

andyz made their first commit to this issue’s fork.

andyz’s picture

StatusFileSize
new528 bytes

This was closed without integrating the change that conditions the "is_dropdown" class on `item.is_expanded` and `item.is_below`. A recent change broke the patch, so I'm uploading a new one.