May 3, 2019

Drupal Modules we like: Menu Link Attributes

Have you ever installed a menu module for one of the following reasons?

  • Adding a class to specific menu items
  • Setting a menu item's target
  • Setting a relationship between linked content
  • or pretty much any other arbitrary attribute.

With the Menu Link Attributes module for Drupal 8 you can satisfy all of those needs in one easy setup. We recently worked on a project that had strict accessibility requirements that we needed to follow and dealing with the menu was a specific pain point for us. Drupal has the basic requirements covered but specific cases required us to set certain attributes on menu items. Normally this is not easily achievable without some major custom work and hard to pass on to the client to maintain themselves for any future menu additions.

Our main problem was setting attributes to not get dinged for duplicate menu links in our menu structure. Let’s take a look at how this module enabled us to quickly setup the attributes we wanted to make available to the client. The config is pretty straight forward, we use YAML to build the form that will appear on the menu item edit page.

 

We’ll start by declaring the wrapper.

attributes:

 

We'll then add all of our options, for our needs we had to create two settings.

attributes:
  aria-hidden:
  role:

 

We will then add our options to each and we'll have a complete form.

attributes:
  aria-hidden:
    options:
      'true': 'True'
      'false': 'False'
  role:
    options:
      presentation: Presentation

 

This is how our settings will appear on the form.

Menu Link Attributes

 

That’s all there is to it, it’s a really simple module that gives you some really powerful options that take a lot of the headache out of menu config.