Sometime you need to give access to administrators roles to use admin menu
, which show items that you don’t want to show for them but it appears for hierarchical permissions.

So I add the following that allows you to remove items from main menu, this code contains two hooks the first define the links of items that you want to remove, the second hook remove them.

/*
 * hook_admin_menu_hide_deprecated_items()
 */  
function hook_admin_menu_hide_deprecated_items(){
//Example
    $items_by_role = array();
    $items_by_role = array(
        ROLE_NAME => array(
            'admin/filled_forms', //Items that you want to remove
            'admin/help', 
            'admin/config/media'
        )
    );
    return $items_by_role;
}
      
/*
 * hook_admin_menu_output_alter() 
 * @Note: each user has own cache menu id as mentioned below:
 * $cid = 'admin_menu:' . $user->uid . ':' . session_id() . ':' . $language->language;
 *  @see admin_menu_page_build()
 * So when the user open his page for the first time the menu loaded and 
 * all items built and all hooks related to menu item invoked
 */
function  hook_admin_menu_output_alter(&$content) {
    global $user; 
    $items_by_role = module_invoke_all('admin_menu_hide_deprecated_items');
    foreach($user->roles as $rid => $role_name){
        if(isset($items_by_role[$role_name])){
           $hide_links = $items_by_role[$role_name];
           $menu = &$content['menu'];
           
           $remove_links = function($hide_links, &$menu) use ( &$remove_links ) {
                foreach ($menu as $item_link => $item_links) {
                    if (in_array($item_link, $hide_links)) {
                        unset($menu[$item_link]);
                    } else {
                        if (is_array($item_links)) {
                            $remove_links($hide_links, $menu[$item_link]);
                        }
                    }
                }
            };

            $remove_links($hide_links, $menu);
      
        }
    } 
}

Just define the items/role that you want to remove inside the hook_admin_menu_hide_deprecated_items().

Thanks

Comments

attar_eweev created an issue.