Custom Menu tabs in Drupal user profile page

2019-05-16 03:20发布

I want to add a menu item along side the [view] [edit] [files] ... menu links at the top of the user profile page. When a user clicks it, it should behave like the others, in that it doesn't just launch to a new page, but the menu item they clicked on (let's call it "Funky Button") turns greyish and the user remains in the user profile area.

I've created a hook like below:

function my_module_funky() {
    // TODO: what to do?
}


function my_module_menu() {

    $items['user/%user/funky'] = array(
        'title' => t('Funky Button'),
        'page callback' => 'my_module_funky',
        'page arguments' => array(1),
        'access callback' => TRUE,
        'access arguments' => array('access funky button'),
        'type' => MENU_LOCAL_TASK,
    );

    return $items;
}

So this above code snippet adds the button -- but I can't figure out how to get it to display like the view and edit buttons display their content. Thanks!

1条回答
叼着烟拽天下
2楼-- · 2019-05-16 03:38

Your callback needs to return the string containing the HTML code of the page to display, for example:

function my_module_funky($user){
 drupal_set_title('Funky page');
 return 'This is the $user value: <pre>'.var_export($user, true).'</pre>';
}

$user comes from the line 'page arguments' => array(1) of your hook_menu implementation which transmits the value of the %user wildcard as first argument of your page callback.


If it's a complex page, you may want to create a theme function with a template file, that way you can store the page's code in a .tpl.php file, which makes it easier to maintain (especially if your module creates many such custom pages).

This would also have the benefit to allow themes to customize the page output by providing their own version of the .tpl.php file if your module becomes popular or other modules could preprocess the page to add/modify variables.

查看更多
登录 后发表回答