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.
By calling theme inside the theming functions, the preprocess functions is called twice, resulting in things like duplicate classes.
Attached patch borrows menu_item_containers way of doing it.
Comment | File | Size | Author |
---|---|---|---|
#6 | special_menu_items-call_theme_once-1447864-6.patch | 600 bytes | marcvangend |
#2 | 1447864-2-preprocess_called_two_time.patch | 524 bytes | gagarine |
special_menu_items-theme.patch | 1.47 KB | Xen |
Comments
Comment #1
gagarine CreditAttribution: gagarine commentedI don't have duplicate classes. Can you please explain how to reproduce?
Comment #2
gagarine CreditAttribution: gagarine commentedReroll. But before commit this, I want to be sure it's necessary.
Comment #3
Xen CreditAttribution: Xen commentedAs far as I remember (it's two finished projects in the past):
If you have a theme or module that imlements hook_preprocess_menu_link that adds classes to links like so:
$vars['element']['#attributes']['class'][] = 'another-class';
You'll end up with duplicate classes, but that's only one possible side effect, other modules might do other things that results in misbehavior when the preprocess functions is called multiple times. Apart from the fact it's just messy and unperforming to run the same code multiple times, even if it is idempotent.
Comment #4
gagarine CreditAttribution: gagarine commentedComment #5
mvcuh, shouldn't the patch in #2 remove the line
return theme('special_menu_items_link_default', $variables);
before trying to return something else?Comment #6
marcvangendmvc, you're right.
gagarine, steps to reproduce:
1) Add a preprocess function to your theme:
2) Clear cache and refresh the page.
3) Check your output and see the 'foo' class was added twice.
Here is a reroll which includes the fix for #5. Works great for me.
Comment #7
rossb89 CreditAttribution: rossb89 at ComputerMinds commentedJust found this issue... a 5 year old patch still works, brilliant!
This should definitely be committed. I ended up with some classes added 6 times due to multiple contrib modules and a theme function preprocessing the link.
Comment #8
rossb89 CreditAttribution: rossb89 at ComputerMinds commentedComment #10
gagarine CreditAttribution: gagarine as a volunteer commented