When implementing 'Link Attributes' module and adding a class to a standard menu item, I find that previous <li> classes are all removed AND the class that I assign via the UI does not show.

Example before assigning a class to a menu item:

<li class="mmenu-mm-list-mlid-4 mmenu-mm-list-path-sample">
    <a href="/sample" class="mmenu-mm-list ">
      <i class="icon-list2"></i>
      <span class="mmenu-block-title">Support Services</span>  
    </a>
</li>

Example after assigning a class:

<li class="">
  <a href="/sample" class="mmenu-mm-list ">
    <i class="icon-list2"></i>
    <span class="mmenu-block-title">Support Services</span>
  </a>
</li>

Not sure if its my use of the 'Mobile Sliding Menu' module or not, but the issue continued even after I uninstalled that module and cleared cache. Whenever I set a menu item attribute, not only does it remove all previous classes (permanently), it never adds the class that I assign via this module.

Is this happening to anyone else?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Stephen Ollman created an issue. See original summary.

larowlan’s picture

Status: Active » Postponed (maintainer needs more info)

Have you applied the core patch mentioned on the project page?

Stephen Ollman’s picture

Isn't this patch now in 8.2.x.

This is the core version I'm running.

larowlan’s picture

Yep, its in 8.2

Can you confirm this happens without mobile slider menu module?

Stephen Ollman’s picture

- Uninstalled Mobile Sliding Menu
- Cleared Cache with 'drush cache-rebuild'
- Installed and enabled this module
- Set Target, Rel and Class to menu item
- Cleared Cache again

Now previous Superfish classes maintain on the menu item <li> but there in no new class.

The 'Target' and 'Rel' attributes are added to the menu item <a>, but still no sign of the new class I added anywhere.

Thanks for looking into this for me.

I'm more curious if its just me or a global issue.

larowlan’s picture

Can you confirm what's in the {menu_link_content_data.link__options} column for this link.

If the class is there, its something in the output/theme layer.

If you have a debugger, check what is returned from \Drupal\link\Plugin\Field\FieldType\LinkItem::getUrl for this link.

If its options contain the class, then that means this module is working as expected.

Beyond that, another module or your theme may be ignoring the options on the Url item.

Really its stupid that a Url can influence its Link render object that wraps it, but we're stuck with that because of how url() and l() worked in D7 until D9.

Stephen Ollman’s picture

a:1:{s:10:"attributes";a:3:{s:6:"target";s:0:"";s:3:"rel";s:0:"";s:5:"class";s:7:"myclass";}}

Ok so if I output a vanilla core menu block the class appears against the menu item anchor, however if I use another menu module (eg Superfish) it does not render.

Bugger!!

larowlan’s picture

Project: Link Attributes widget » Superfish
Version: 8.x-1.0-alpha2 » 8.x-1.x-dev
Status: Postponed (maintainer needs more info) » Active

Thanks, sounds like its a Superfish issue then.

Also, hi from fellow Aussie Drupal enthusiast, be sure to look me up at DrupalSouth if you're going.

Stephen Ollman’s picture

Doesn't play nice with Superfish at all.

When I remove the 'target and 'rel' attributes after previously setting them, I'm left with blanks in the code.

<li id="main-menu-link-content7f86ecec-ec65-4ab2-ab27-76bd0a33839a" class="sf-depth-1 sf-no-children"><a href="" target="" rel="">Menu Item</a></li>

Cheers!

Stephen Ollman’s picture

Issue summary: View changes
Stephen Ollman’s picture

Modules like 'Link attributes' and 'Menu Link Attributes' allow classes to be placed against menu items.

These are stored in the database accordingly:

a:1:{s:10:"attributes";a:2:{s:5:"class";s:7:"myclass";s:6:"target";s:6:"_blank";}}

However Superfish does not utilise these attributes.

Looks like a continuation of this https://www.drupal.org/node/1104616

Stephen Ollman’s picture

Simple JQuery solution to add a class to a menu item <a> based on its title.

    $('#superfish-main li a').each(function() {
        var menu_name = $(this).html();
        menu_name = menu_name.replace(/\s+/g, '-').toLowerCase();
        $(this).addClass(menu_name);
    });

Or adding the class to the menu item <li>:

    $('#superfish-main li a').each(function() {
        var menu_name = $(this).html();
        menu_name = menu_name.replace(/\s+/g, '-').toLowerCase();
        $(this).parent().addClass(menu_name);
    });
nicholas.alipaz’s picture

Here is a first patch that sets up allowing classes to be added to menu items. I have only tested this with top level menuparent items. This seems like a pretty simple fix. I would guess this could be extended to other attributes but it seems beyond the scope of this issue and this one addition covers about 50% of the use-cases where people might need this feature would be my best estimate.

By the way, this was tested with classes added via the Menu Link Attributes module.

nicholas.alipaz’s picture

Status: Active » Needs review
Stephen Ollman’s picture

Love your work!

Works nicely. Classes defined in the menu attribute field are now added as the class attribute of the menu LI.

Tested against 'Menu Link Attributes' and 'Link Attributes'

nicholas.alipaz’s picture

Status: Needs review » Reviewed & tested by the community

Stephen, yes I have it working on two sites already. I suggest that this be pulled into the next release. Other issues could be opened as needed for other attributes seeing how this issue is specific to classes and adding in other attributes at the moment might introduce unnecessary complexity in getting this needed feature in. It should be noted that other attributes are out of scope for the issue anyhow.

Again, I think this might fix about 50% of the use-cases for people needing these sorts of features. Most people just need a class on their menu items.

danjordan’s picture

Updated patch for current dev version

mehrpadin’s picture

Hey there,

Are you experiencing this with the dev?

ivnish’s picture

#17 is applying to the last version (8.x-1.4)

kreatIL’s picture

Status: Reviewed & tested by the community » Needs work

Patch #17 applies correctly but does not seem to work in my usecase (superfish 8.x-1.4 and menu_link_attributes 8.x-1.2). Attributes assigned to containing li elements are being stripped.

renguer0’s picture

ivnish’s picture

Patch #17 gives a lot of php warnings to dblog

3CWebDev’s picture

New patch attached to add stripped container classes from Menu Link Attributes module back into Superfish li elements.

3CWebDev’s picture

New patch to fix warnings in logs.

3CWebDev’s picture

FileSize
835 bytes

New patch to fix warnings in logs.

ivnish’s picture

Status: Needs work » Needs review
3CWebDev’s picture

FileSize
1.25 KB

Sorry, found an issue with the patch and re-rolled.

New patch to fix warnings in logs.

ivnish’s picture

up!