Có thể thay đổi Menu Item Visibility module với custom "in code"

Có thể thay đổi Menu Item Visibility module với custom "in code"

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.

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:

/**
 * 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.

Step 3.

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.

Bạn thấy bài viết này như thế nào?: 
No votes yet
Ảnh của Khanh Hoang

Khanh Hoang - Kenn

Kenn is a user experience designer and front end developer who enjoys creating beautiful and usable web and mobile experiences.

Advertisement

 

jobsora

Dich vu khu trung tphcm

Dich vu diet chuot tphcm

Dich vu diet con trung

Quảng Cáo Bài Viết

 
www.OSTraining.com miễn phí 1 tháng học Drupal

www.OSTraining.com miễn phí 1 tháng học Drupal

Drupal Global Training Days is an initiative by the Drupal Association (D.A.) to introduce new and beginning users to Drupal.

DDI Contribution Team cung cấp và thảo luận về Drupal community

DDI Contribution Team cung cấp và thảo luận các giải pháp cho Drupal community

In 2017, the reported data showed that only 6 percent of recorded code contributions were made by contributors that identify as female. After a conversation in the

Chiến lược tiếp cận khách hàng quanh cửa hàng của bạn chất nhất 2019

Chiến lược tiếp cận khách hàng quanh cửa hàng của bạn chất nhất 2019

Seach từ khóa Fanpage: Các địa điểm search trên Foody hoặc Google; Search từ khóa có địa điểm bạn cần

Công ty diệt chuột T&C

 

Diet con trung