Drupal Menu Position Management

By Bevan Wishart

I recently was working on a drupal project I had inherited from previous devs. This introduced me for the first time to the Drupal Menu Position module - https://www.drupal.org/project/menu_position

In this case I made use of this module to place the user profiles of users with the role 'blog author' under the 'blog' menu. In effect having the 'blog' menu highlight when viewing one of these profiles.

The challenge with this solution is that the menu position module lacks the ability to export to features. A quick google search shows that a patch is under development to resolve this, however its status at time of writing (Feb 2015) is currently 'needs work' and applying the latest version of that patch elicited less than usable results.

But, with a simple hook_update_n function in a custom module install file, the menu position was easily configured via code.


/**
* Configure menu_position rule for user profiles under blogs.
*/
function tlms_config_update_7016() {
 module_load_include('inc', 'menu_position', 'menu_position.admin');
 $settings = array(
   'values' => array(
     'plid' => 'main-menu:695',
     'admin_title' => 'Blog Author Profiles',
     'rid' => '',
     'conditions' => array(
       'pages' => array(
         'pages' => "user/*\nusers/*",
       ),
     ),
   ),
 );
menu_position_rule_form_submit(NULL, $settings);
}

To break this down, what is done here is to effectively simulate the submission of the add menu position rule form. By use of xdebug and setting a breakpoint in the menu_position_rule_form_submit() function, I was able to inspect the passed in variables and replicate these in the above code snippet.

Voila, menu_position configuration in code.