Replacing Menu Item Visibility module with custom "in code" solution

I'm a big fan of fighting with Drupal's inefficiencies and bottlenecks. Most of these come from contrib modules. Everytime we install a contrib module we should be ready for surprises which come on board with the module.

One of the latest examples is Menu item visibility (https://drupal.org/project/menu_item_visibility) that turned out to be a big trouble maker on one of my client's sites. Menu item visibility is a simple module that let's you define link visibility based on a user's role. Simple and innocent... until you look under the hood.

The thing is Menu item visibility stores it's data in database and does a query per every menu item on the page. In my case it produced around 30 queries per page and 600 queries on menu/cache rebuild (which normally equals to the number of menu items you have in your system).

The functionality that this module gives to an end user is good and useful (according to drupal.org: 6,181 sites currently report using this module) but as you see, storing these settings in db can become a huge bottleneck for your site. I looked at the Menu item visibility source and came to this "in code" solutions that fully replicates the module functionality but stores data in code.

Step 1.

Create a custom module and call it like Better menu item visibility., machine name: better_menu_item_visibility.

Step 2.

Let's add the first function that holds our menu link item id (mlid) and role id (rid) data:

  1. /**
  2. * This function returns a list of mlid's with a list of roles that have access to link items.
  3. * You can change the list to add new menu items or/and roles
  4. * The list is presented in a format:
  5. * 'mlid' => array('role_id', 'role_id),
  6. */
  7. function better_menu_item_visibility_menu_item_visibility_role_data() {
  8. return array(
  9. '15' => array('1', '2'),
  10. '321' => array('1'),
  11. '593' => array('3'),
  12. // Add as many combinations as you want.
  13. );
  14. }

This function returns an array with menu link item ids and roles that can access the item. If you already have Menu item visibility installed, you can easily port the data from the db table {menu_links_visibility_role} into this function.

Step 3.

And now let's do the dirty job and process the menu items:

  1. /**
  2. * Implements hook_translated_menu_link_alter().
  3. */
  4. function better_menu_item_visibility_translated_menu_link_alter(&$item, $map) {
  5. if (!empty($item['access'])) {
  6. global $user;
  7. // Menu administrators can see all links.
  8. if ($user->uid == '1' || (strpos(current_path(), 'admin/structure/menu/manage/' . $item['menu_name']) === 0 && user_access('administer menu'))) {
  9. return;
  10. }
  11. $visibility_items_for_roles = better_menu_item_visibility_menu_item_visibility_role_data();
  12. if (!empty($visibility_items_for_roles[$item['mlid']]) && !array_intersect($visibility_items_for_roles[$item['mlid']], array_keys($user->roles))) {
  13. $item['access'] = FALSE;
  14. }
  15. }
  16. }

In short this function skips access check for user 1 and for user that has 'administer menu' permission and does the access check for link menu items listed in better_menu_item_visibility_menu_item_visibility_role_data. As you see, instead of calling database it gets data from the code which is really fast. Let me know what you think and share your ways of fighting with Drupal's inefficiencies.