Allow dynamic replacements in the menu

sun - April 1, 2009 - 17:11
Project:Administration menu
Version:6.x-3.x-dev
Component:Code
Category:task
Priority:normal
Assigned:Unassigned
Status:closed
Description

Offshoot from #345984: Client-side caching of administration menu:

Certain menu items need to be updated for the current user or the current time, such as the user counter.

- The cached version of the menu holds the value of a menu item when the menu was cached.

- We don't want to flush the cache just because one menu item's title has changed (and is constantly changing by its nature).

- We want to allow modules to expose menu items that can be updated (and created?) via JavaScript at runtime, getting the current values via Drupal.settings.

Currently, the only item that we need this for is admin_menu's user counter. However, when this functionality is in place and is implemented properly, I could also imagine other use-cases.

#1

markus_petrux - April 1, 2009 - 18:53

Here's one possible way to achieve this:

- The menu items that may need replacements should be wrapped in unique CSS classes or ID so that we can build a jQuery selector that matches.
- The one that implements a replaceable item, generates Drupal.settings with the variables, and provides an admin_menu behavior.

For example, to replace the online counts, the behavior could do something like this:

$('.admin-menu-users a span').text(Drupal.settings.mymodule.counts);

But the menu item for online counts needs a spam to wrap the counts.

#2

markus_petrux - April 2, 2009 - 14:15
Status:active» needs review

Hi sun,

Here's a patch that implements dynamic replacement of the users online counts in admin_menu.

I think it uses a technique that could be implemented by any other module that wishes to perform dynamic replacements. All you need is a unique jQuery selector.

So there would be no additional need. Just use this techique and you get dynamic replacements. :-D

AttachmentSize
admin_menu-420828-2.patch 3.05 KB

#3

sun - April 3, 2009 - 07:33
Status:needs review» needs work

Basically yes.

However, I really think this should be a hook. Each implementation of this hook returns one or more class => new_value mappings. admin_menu invokes this hook in admin_menu_init() and adds all gathered mappings to Drupal.settings. So we have 1 setting and 1 behavior that performs the replacements.

Because this implementation primarily affects the user counter, we want to turn the icon image from the menu item title into a CSS-based background image, so we can replace the item's text more easily.

On a second thought, I'm not sure whether we rather want to use IDs instead of classes. I'd guess that one replacement value always maps to one target item only.

#4

sun - April 4, 2009 - 14:46
Status:needs work» needs review

This is how I imagined it. :)

Works very nice from my own testing.

AttachmentSize
admin_menu.dynamic-replacements.patch 5.58 KB

#5

sun - April 4, 2009 - 15:24
Status:needs review» fixed

Committed that patch to all branches, so we can proceed with #293768: Allow to enable/disable menu additions.

Feel free to re-open this issue if there are any objections.

#6

markus_petrux - April 6, 2009 - 09:17

Seems to be working like a charm here as well. Thanks

#7

System Message - April 20, 2009 - 09:20
Status:fixed» closed

Automatically closed -- issue fixed for 2 weeks with no activity.

 
 

Drupal is a registered trademark of Dries Buytaert.