Khanh Hoang - Kenn
Kenn is a user experience designer and front end developer who enjoys creating beautiful and usable web and mobile experiences.
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 visibilitystores 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.
Create a custom module and call it like Better menu item visibility., machine name:better_menu_item_visibility.
Let's add the first function that holds our menu link item id (mlid) and role id (rid) data:
/** * This function returns a list of mlid's with a list of roles that have access to link items. * You can change the list to add new menu items or/and roles * The list is presented in a format: * 'mlid' => array('role_id', 'role_id), */ function better_menu_item_visibility_menu_item_visibility_role_data() { return array( '15' => array('1', '2'), '321' => array('1'), '593' => array('3'), // Add as many combinations as you want. ); }
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.
And now let's do the dirty job and process the menu items:
/** * Implements hook_translated_menu_link_alter(). */ function better_menu_item_visibility_translated_menu_link_alter(&$item, $map) { if (!empty($item['access'])) { global $user; // Menu administrators can see all links. if ($user->uid == '1' || (strpos(current_path(), 'admin/structure/menu/manage/' . $item['menu_name']) === 0 && user_access('administer menu'))) { return; } $visibility_items_for_roles = better_menu_item_visibility_menu_item_visibility_role_data(); if (!empty($visibility_items_for_roles[$item['mlid']]) && !array_intersect($visibility_items_for_roles[$item['mlid']], array_keys($user->roles))) { $item['access'] = FALSE; } } }
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 inbetter_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.