Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
I can't see anyone else commenting about this (unless they are calling them something else and I am just missing it) but it would be a cool idea to be able to add custom attributes to a link.
Of course, there is still much debate about this and it may be just wise to have a field where you can "inject" data attribute data.
Anything, just some quick thoughts on this... as usual, this module rules.
Comment | File | Size | Author |
---|---|---|---|
#43 | cmog_menu_attributes.info | 261 bytes | katannshaw |
#43 | custom-menu-attr-issue-03012018.png | 86.4 KB | katannshaw |
#27 | Main_menu___Site-Install.png | 90.28 KB | joelpittet |
#23 | Screen Shot 2017-01-27 at 9.34.39 AM.png | 88.44 KB | katannshaw |
Comments
Comment #1
nocean CreditAttribution: nocean commentedI'd also like to see this feature. Seems like this module would be a logical place to include such functionality.
Comment #2
Todd Zebert CreditAttribution: Todd Zebert commentedThis module provides a hook. I've used it. Do something like:
Probably can put that in your template,php too.
*Be sure to edit the above 'data-my-attribute', label and description to fit your needs.*
I've been meaning to contribute an api file for this project.
Comment #3
joelpittet@Todd Zebert there is an API file in the dev branch. You are welcome to submit improvements, I'd be happy to review. And thanks for helping with this issue!
Comment #4
knalstaaf CreditAttribution: knalstaaf commented@Todd Zebert: I installed this module, enabled it, but forgot to alter
data-my-attribute
.After applying this to a menu link I can't get rid of it anymore, not even after uninstalling the module, flushing cache etc.
Where can I find this in the database so I can manually remove this attribute again please?
Comment #5
joelpittet@knalstaaf unfortunately we are saving all attributes in the serialized options column in the database. And on uninstall we have no way to deal with attributes added by other modules or by you and the ones added by menu_attributes.
My suggestion is look at that column and *VERY carefully*, decided if you are using another module that is storing necessary options. BACKUP your menu_links table, and rewrite that column. Likely good to target a specific menu_name too, to limit any damage.
Comment #6
Todd Zebert CreditAttribution: Todd Zebert commentedJust to add some specifics to Joel's comment. It's in the "options" field of the "menu_links" table.
Please take heed of his warnings and precautions.
Depending on your technical skill, a site like http://serialize.onlinephpfunctions.com/ might be helpful in un/serializing the data.
Comment #7
knalstaaf CreditAttribution: knalstaaf commentedWill dive into this, thanks for the push in the right direction!
Comment #8
Anonymous (not verified) CreditAttribution: Anonymous commentedMay be we can create some ui to add new data-* attribute from config form?
Comment #9
joelpittet@bobrov1989 try #2 it creates the UI widgets for whatever data attribute you can imagine.
A UI for adding/removing attribute types would be interesting but I'm wonder if there is enough of a use case?
Comment #10
Anonymous (not verified) CreditAttribution: Anonymous commented@joelpittet the use cases can be different, for example some js libraries and frameworks work with data attributes, it can be used in styling menus etc.
Comment #11
joelpittet@bobrov1989 to be clear you are asking for a UI to manage adding and removing these data attributes? As of right now you can add and remove extra attributes including data- attributes with the API as mentioned in #2. So the use-case I'm curious is when people need to do this through the UI?
Comment #12
Anonymous (not verified) CreditAttribution: Anonymous commentedok, it was only proposition)))
Comment #13
joelpittetAnd a good one:) Still wondering how good? :D
Comment #14
Anonymous (not verified) CreditAttribution: Anonymous commentedYes, so do we need to implement this feature?
Comment #15
MickL CreditAttribution: MickL commentedThumbs up for custom attributes! :)
For example Bootstrap's Modal needs data-toggle and data-target. Or (the tooltip js-library) tipsy needs original-title.
Also it would be very nice to activate/deactivate the attributes in the module-settings. (For example i would like do deactivate releationship, target, access key, but add my custom(bootstrap) attributes.
Comment #16
Anonymous (not verified) CreditAttribution: Anonymous commentedOk, I'll try to take care about it
Comment #17
VVS CreditAttribution: VVS commentedTry this theming.
Comment #18
joelpittetCovering everybody's use-case will likely cause feature bloat. The API allows you to extend this module. Maybe worth creating a sandbox for bootstrap_menu_attributes or something to hook in all the needed ones.
Comment #19
Anonymous (not verified) CreditAttribution: Anonymous commentedYes, you're right, I'll be better.
Comment #20
TheJoker CreditAttribution: TheJoker commented@joelpittet I do not understand why you do not want to include these functions in the module. Now the module provides only the basic attributes, but HTML5 is dictating new conditions and a large number of frameworks for a couple of years of use this opportunity.
Thank you for your hard work, but I think it is unnecessary conservatism. Please start to move towards professional users, since this module meets the needs of beginners only. For more professional users constantly have to create hooks and climb in the code, rather than save time. Although the modules are designed for our convenience, the rejection of this attribute is at odds logic.
Comment #21
TheJoker CreditAttribution: TheJoker commentedComment #22
joelpittet@TheJoker you could make another contrib or sandbox module that extends menu_attributes that does the logic for custom data attributes. If it gets the usage you expect as being needed by a wide variety of people maybe we can merge it into menu_attributes.
I'll add any hooks you need to make it work well with menu_attributes and if it works I'll even link to it from the project page.
There is nothing to review here as there is no patch, if you want to re-open set it to 'active'.
Comment #23
katannshaw CreditAttribution: katannshaw at Promet Source commented@Joelpittet (and any others): Related to the original post and replies, I'm attempting to use the API to add custom menu attributes with the following code in my theme's template.php file:
Even after clearing my cache, these fields are not showing up under my site's main menu as seen in the attached screenshot. Is there a step that I'm missing to get these fields added? Thanks for any assistance.
Comment #24
joelpittetThat must be in a module because those hooks are using
module_invoke_all()
Comment #25
katannshaw CreditAttribution: katannshaw at Promet Source commented@Joelpittet: Thank you for the prompt answer. I will make that change.
UPDATE: @Joelpittet: I created a separate module with the following two files and these new custom menu attributes are still not showing. This module is showing in the list of modules and I did enable it and clear the cache. Do you see a step that I'm missing?
MYMODULE_menu_attributes.info
MYMODULE_menu_attributes.module
Comment #26
katannshaw CreditAttribution: katannshaw at Promet Source commented@joelpittet: I was hoping for an update. Do you have any suggestions?
Comment #27
joelpittetYou don't need
files[] = MYMODULE_menu_attributes.module
and probably should havedependencies[] = menu_attributes
in the info file.Those shouldn't change how it works, I copied your code in and it worked as expected. Here's a screen shot. Everything is named Tabindex but that's just a copy change.
Comment #28
katannshaw CreditAttribution: katannshaw at Promet Source commented@joelpittet: That was it. Thanks for your help on this! I very much appreciate it.
Comment #29
oomphinc CreditAttribution: oomphinc as a volunteer commented@jayhawkfan75 @joelpittet
Any reason NOT to contribute these options back to the core module?
Im assuming that @jayhawkfan75 was adding these options for accessibility standards.
Comment #30
joelpittetTheir usage is rare, you could consider creating a menu_attributes_a11y contrib module if you use them enough to warrant it. If that gets the uptake then maybe we can consider merging them later. I'd even be willing to promote it on the project page.
Comment #31
katannshaw CreditAttribution: katannshaw at Promet Source commented@joelpittet: I would love to contribute back a menu_attributes_a11y contrib module to the project once I get my own issue resolved.
I'm able to get back to work on this custom module and for some reason these custom attributes are not getting added to the admin/structure/menu/item/#/edit form. Here's the code I'm using, and I've checked that this custom module is enabled. It looks the same as what was in a previous post that *was* working so I'm stumped. Hopefully this is something obvious. Please help!
Kat
MYTHEME_menu_attributes.info
MYTHEME_menu_attributes.module
Comment #32
joelpittetMy first guess would be that it says
THEMENAME
but it should be a module not a theme, but it's in a .module file that should be just accidental?Otherwise it looks correct.
Comment #33
katannshaw CreditAttribution: katannshaw at Promet Source commentedI just replaced the beginning of the name with THEMENAME. It's a habit since I mostly work with themes. It should be MODULENAME. That's the only difference. The module is placed at sites/all/modules/contrib/.
Should I add this function to the end of my custom code from the API?
Any other suggestions?
Kat
Comment #34
joelpittetOh definitely not, that code is in
menu_attributes
and would cause a fatal error of duplicate function name, also that is the code that should be calling your code.I'd suggest creating a sandbox for this, put the code you have so far up in there and put a link here and I'll try it out.
Comment #35
katannshaw CreditAttribution: katannshaw at Promet Source commented@joelpittet: I've got the sandbox initially set up at https://www.drupal.org/sandbox/katannshaw/2923855
Do I need to just upload the 2 module files to the sandbox or do I need to first pull in D7 and then add the module to it before pushing that up to the sandbox? This is my first time creating a sandbox :-)
Kat
Comment #36
joelpittetJust the 2 files. Cool, first sandbox!
Comment #37
katannshaw CreditAttribution: katannshaw at Promet Source commentedThanks! There's a first time for everything. Here you go: https://www.drupal.org/node/2923855/commits
Comment #38
katannshaw CreditAttribution: katannshaw at Promet Source commented@joelpittet: Have you had a chance to look at my sandbox project yet? I really need to get this issue resolved, and it would also be great to have this contribution committed if it's good.
Comment #39
bmango CreditAttribution: bmango commented@jayhawkfan75 - I used your code to create a data-target attribute (used for scrollspy in Bootstrap) in a custom module and it worked great. Thank you.
Comment #40
joelpittetThe sandbox looks great and sounds like @bmango has tested and works for him.
I'm not sure what CMoG is, but maybe you can make it a real project and change it to menu_attributes_a11y or something generic?
Comment #41
katannshaw CreditAttribution: katannshaw at Promet Source commented@bmango: That's great to hear! Thanks so much for letting me know. I'm glad that it was helpful!
@joelpittet: Absolutely! I hadn't realized that I had committed the cmog files instead of the more generically-named files. I'll make that change and add an update to this ticket when that's done. I'll need to create a project page as well correct? Can you forward me to where I would do that? I'm a newbie to all of this :-)
Kat
Comment #42
joelpittetI've never done the sandbox to full project promotion myself but here's the docs I found: https://www.drupal.org/node/1068952
Comment #43
katannshaw CreditAttribution: katannshaw at Promet Source commented@joelpittet: Thanks for those docs. I haven't had time to get to that yet but I have something that needs to be addressed beforehand.
Even after my custom module is enabled, my local version of this code is still not getting rendered on the /admin/structure/menu/item/#/edit page under the "Menu link attributes" and "Menu item attributes" accordion sections. This is the code that I'm presently using based off of the original API:
cmog_menu_attributes.info
cmog_menu_attributes.module
Here is how it gets rendered on my local site:
I've attached the 2 module files for you or others to test if you can. Do you have any suggestions on what I'm missing?
Kat
Comment #44
katannshaw CreditAttribution: katannshaw at Promet Source commented@joelpittet: Have you had a chance to look into this for me?
Kat