0

On the /user page i've created two tabs:

function hook_menu() {

    $items['user/%/My-Courses'] = array(
        'title' => 'My Courses',
        'type' => MENU_LOCAL_TASK,
        'page callback' => 'drupal_get_form',
        'page arguments' => array('hook_form'),
        'access callback' => 'hook_access',
        'access arguments' => array('access content'),
    );

    $items['user/%/Privacy-Statement'] = array(
        'title' => 'Privacy Statement',
        'type' => MENU_LOCAL_TASK,
        'page callback' => 'drupal_get_form',
        'page arguments' => array('hook_form'),
        'access callback' => 'hook_access',
        'access arguments' => array('access content'),
    );

    return $items;
}

What i'm trying to do is add a link to the my courses tab, and a privacy statement (just text) to the privacy statement tab.

function hook_form(){

$form['privacy'] = array(
    '#type' => 'textfield',
    '#value' => 'Privacy statement',
  );

 // returns whatever you want to appear in that tab. 
 $form['link1'] = array(
     '#type' => 'submit',
     '#value' => t('This will show the customers courses.'),
 );

 return $form;
}

The problem is that it's adding both the link and statement to both tabs.

How would I go about targeting each tab individually?

2
  • 1
    Are you sure the 'page arguments' => array('hook_form') line is the one you are actually using?
    – apaderno
    Nov 7, 2015 at 10:55
  • No, I was trying to contain a bit of anonymity for the company and code i'm working on/for :) good catch though!
    – Michael W
    Nov 10, 2015 at 20:58

1 Answer 1

2

I think you'd be better off creating 2 separate forms for each tab, but if you want one form to handle both you can use the function menu_get_item() to determine the menu path that routed to the form.

  $menu_item = menu_get_item();
  if(isset($menu_item['path']) && $menu_item['path'] == 'user/%/Privacy-Statement') {
    //@TODO: Add form elements specific to privacy tab
  }
2
  • Creating two separate forms for each tab sounds like the direction I should be going in. How would I go about doing that, if you you mind me asking? Is it done within the same hook_form?
    – Michael W
    Nov 6, 2015 at 19:07
  • 1
    For each menu item in hook_menu() just use a different page arguments item in your array (e.g. array('nspf_courses_form') & array('nspf_privacy_form')). Then make sure to create a copy of your nspf_form form building function that has the same name (e.g. function nspf_courses_form() & function nspf_privacy_form())
    – Shawn Conn
    Nov 6, 2015 at 19:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.