2
votes

I want to create a page that contains 2 different forms, that use URL arguments. Each part of this task is solved easily when done separate. I can use URL parameters on a form, using drupal_get_form as callback:

function mymodule_menu() {
  $items['user/%/blah'] = array(
    'title' => 'Blah',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('my_custom_callback', 1),
    'access arguments' => TRUE,
  );
}

function my_custom_callback($form, &$form_state, $uid) {
  // I can use candidateId here now
}

What else could be done is, create a custom page callback with two forms (not completely sure if this works)

function mymodule_menu() {
  $items['user/%/blah'] = array(
    'title' => 'Blah',
    'page callback' => 'my_page_callback',
    'page arguments' => array(1), 
    'access arguments' => TRUE,
  );
}

function my_page_callback($uid) {
  $build = array();
  global $user;

  //PROBLEM: I can't pass the URL parameters to the separate
  //form callbacks
  $build['form1'] = drupal_get_form('form1_callback');
  $build['form2'] = drupal_get_form('form2_callback');

  return $build;
}

So as described in the comment, I can't pass the URL parameters to the separate form callbacks - and they need this parameter in my module. Precisely: I need 2 Forms on one page (because I need 2 different submit buttons that work as default buttons), with an URL parameter passed each.

TL;DR:

Basically, I need a form page that has 2 text input fields, and each field should have an own, separate submit button - but both should be default buttons on it's field, so it can easily be used without the mouse, just by filling in the input field & pressing enter. The corresponding button of the field should be triggered.

I don't care if I need 1 or 2 forms, I just need the URL parameter on both forms.

Solutions that did not help:

...and others I found.

Thanks in advance.

1

1 Answers

0
votes

You were very close with the two drupal_get_form() functions. You can add a parameter after the form_id like:

function testing_forms_callback($user) {
  $build = array();
  $build['form1'] = drupal_get_form('testing_form_one', $user);
  $build['form2'] = drupal_get_form('testing_form_two', $user);

  return drupal_render($build);
}

This will allow you to pass arguments to the form in which you are loading. Here is the full code.

/**
 * Implements hook_menu()
 */
function testing_menu() {
  $items['user/%user/blah'] = array(
    'type' => MENU_CALLBACK,
    'title' => 'My Forms',
    'page callback' => 'testing_forms_callback',
    'page arguments' => array(1),
    'access callback' => TRUE,
  );

  return $items;
}

function testing_forms_callback($user) {
  $build = array();
  $build['form1'] = drupal_get_form('testing_form_one', $user);
  $build['form2'] = drupal_get_form('testing_form_two', $user);

  return drupal_render($build);
}

/**
 * Form 1.
 */
function testing_form_one($form, $form_state, $user) {
  $form['textfield_1'] = array(
    '#type' => 'textfield',
    '#title' => t('Textfield 1'),
    '#default_value' => $user->uid,
  );

  $form['submit_1'] = array(
    '#type' => 'submit',
    '#value' => t('Submit 1'),
  );

  return $form;
}

/**
 * Form 2.
 */
function testing_form_two($form, $form_state, $user) {
  $form['textfield_2'] = array(
    '#type' => 'textfield',
    '#title' => t('Textfield 2'),
    '#default_value' => $user->name,
  );

  $form['submit_2'] = array(
    '#type' => 'submit',
    '#value' => t('Submit 2'),
  );

  return $form;
}

/**
 * Submit handler to process the first textfield.
 */
function testing_form_one_submit($form, &$form_state) {
  drupal_set_message(t('called testing_form_submit_one'));
  // Do something.
  if (isset($form_state['values']['textfield_1'])) {
    $form_state['redirect'] = 'user/' . $form_state['values']['textfield_1'];
  }
}

/**
 * Submit handler to process the second textfield.
 */
function testing_form_two_submit($form, &$form_state) {
  drupal_set_message(t('called testing_form_submit_two'));
  // Do something.
  if (isset($form_state['values']['textfield_2'])) {
    $form_state['redirect'] = 'users/' . $form_state['values']['textfield_2'];
  }
}