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?
Comment | File | Size | Author |
---|---|---|---|
#27 | retain_menu_classes-2782387.patch | 1.25 KB | 3CWebDev |
Comments
Comment #2
larowlanHave you applied the core patch mentioned on the project page?
Comment #3
Stephen OllmanIsn't this patch now in 8.2.x.
This is the core version I'm running.
Comment #4
larowlanYep, its in 8.2
Can you confirm this happens without mobile slider menu module?
Comment #5
Stephen Ollman- 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.
Comment #6
larowlanCan 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.
Comment #7
Stephen Ollmana: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!!
Comment #8
larowlanThanks, 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.
Comment #9
Stephen OllmanDoesn'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!
Comment #10
Stephen OllmanComment #11
Stephen OllmanModules 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
Comment #12
Stephen OllmanSimple JQuery solution to add a class to a menu item
<a>
based on its title.Or adding the class to the menu item
<li>
:Comment #13
nicholas.alipaz CreditAttribution: nicholas.alipaz commentedHere 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.
Comment #14
nicholas.alipaz CreditAttribution: nicholas.alipaz commentedComment #15
Stephen OllmanLove 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'
Comment #16
nicholas.alipaz CreditAttribution: nicholas.alipaz commentedStephen, 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.
Comment #17
danjordan CreditAttribution: danjordan commentedUpdated patch for current dev version
Comment #18
mehrpadin CreditAttribution: mehrpadin commentedHey there,
Are you experiencing this with the dev?
Comment #19
ivnish CreditAttribution: ivnish commented#17 is applying to the last version (8.x-1.4)
Comment #20
kreatIL CreditAttribution: kreatIL as a volunteer commentedPatch #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.
Comment #21
renguer0 CreditAttribution: renguer0 as a volunteer commentedIt can be merged with https://www.drupal.org/project/superfish/issues/3021816 ?
Comment #22
ivnish CreditAttribution: ivnish commentedPatch #17 gives a lot of php warnings to dblog
Comment #23
3CWebDev CreditAttribution: 3CWebDev commentedNew patch attached to add stripped container classes from Menu Link Attributes module back into Superfish li elements.
Comment #24
3CWebDev CreditAttribution: 3CWebDev commentedNew patch to fix warnings in logs.
Comment #25
3CWebDev CreditAttribution: 3CWebDev commentedNew patch to fix warnings in logs.
Comment #26
ivnish CreditAttribution: ivnish commentedComment #27
3CWebDev CreditAttribution: 3CWebDev commentedSorry, found an issue with the patch and re-rolled.
New patch to fix warnings in logs.
Comment #28
ivnish CreditAttribution: ivnish commentedup!