Custom Menu tabs in Drupal user profile page

僤鯓⒐⒋嵵緔 提交于 2019-12-07 18:16:31

问题


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:


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.



来源:https://stackoverflow.com/questions/4192846/custom-menu-tabs-in-drupal-user-profile-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!