简体   繁体   中英

Adding a new tab to user in Drupal

How can I add a new tab to user profile. I want to add for instance New User Tab which will be visible by administrator and user itself (not others).

If you want to create your own module, the code will look like this:

<?php
// Define callback for tab.
function user_tab_menu(){
    return array(
        'user/%/new_tab' => array(
            'title' => 'New tab',
            'page callback' => 'user_tab_page',
            'page arguments' => array(1),
            'access callback' => 'user_tab_access',
            'access arguments' => array(1),
            'type' => MENU_LOCAL_TASK,
        ),
    );
}

// Show the page
function user_tab_page($uid){
    return 'New tab';
}

// Check if user has permission, or views its own page
function user_tab_access($uid){
    return ($uid == $GLOBALS['user']->uid) || user_access('view user tabs');
}

// Define permission for administrators
function user_tab_perm(){
    return array(
        'view user tabs' => array(
            'title' => t('View user tabs'), 
            'description' => t('View user tabs'),
        ),
    );
}

You can achieve this through the views module without having to do custom coding.

Set up a new view page with path as 'user/%' Set contextual filter as 'user:uid'

Set your page content as 'user' or 'fields' or whatever you want rendered. This is going to 'hijack' the standard drupal page into a views rendering.

Now you can add any amount of additional tabs by adding views pages and setting the path to 'user/%/path_of_your_tab'. Set the menu entry for each tab to 'menu tab' and 'user menu'

You can then set access permissions in views for each tab.

嗨朋友们,感谢您帮助我....我完成了代码

function downloaded_menu(){ $items['user/%user/downloaded'] = array( 'title' => 'Downloaded', 'page callback' => 'downloaded_content_page', 'access arguments' => array('access content'), 'type' => MENU_LOCAL_TASK, 'weight' => 10, ); return $items; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM