When I was building a feature (as in Features), I noticed the menu_masks variable. I have read menu_get_ancestors() and _menu_router_save() but I still don't understand what it does.

On a more practical level: When I'm building a feature which defines the menu's for my site, should I export the menu_masks variable too?


menu_masks is a Drupal variable used to find the possible ancestors of a menu item. It contains an array of values that are calculated from the paths defined from modules; each values is essentially a binary number where the digit is 1 when the path part doesn't contain a placeholder, and 0 when it contains a placeholder. For example, the number for node/%node/edit is 9 (101 in base 2), while the number for admin/structure/types/manage/%comment_node_type/comment/fields is 123 (1111011 in base 2).
The reason why numbers are used is avoid comparing strings; plus those numbers are easily sorted in a way that a number matching a path without placeholders is placed before a number matching a path with one or more placeholders. (The number for %/%/% would be 0, while the number for admin/appearance/list is 13.)

Notice that menu_get_ancestors() doesn't return the ancestors of the path passed as argument, but the possible ancestors. Passing array('node', '12', 'edit') to the function, I would get the an array containing the following values, as result.

  1. node/12/edit
  2. node/12/%
  3. node/%/edit
  4. node/12
  5. node/%
  6. node

In my test site, only the third, the fifth, and the sixth are paths defined by the modules I installed.

You don't need to export the menu_masks variable: Its content is very specific for the site and it is re-calculated when menu_rebuild() is called.
Drupal is still able to work correctly even in the case menu_rebuild() is not yet called, and that Drupal variable has not been set. You should not notice any performance issue, but if that would be the case, you can call menu_rebuild() or force Drupal to call that function; if you have the Devel module installed, you can easily force Drupal to rebuild its menus.

  • Great explanation, thanks. I'd give you another upvote if I could :-) – marcvangend Apr 12 '13 at 19:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.