Alarid
Alarid

Reputation: 818

Drupal form with custom arguments built twice

I'm trying to pass arguments to a form called by "durpal_get_form", in a function PHP called in AJAX from a JS script.

So I did something like this :

// Load form
$form = drupal_get_form("_bto_ajax_submenu_organize_form", array(
    'territories' => $territories, 
    'accommodations' => $accommodations, 
    'activities' => $activities,
    'language' => $language,
));

The fact is my form is built twice. First time I've got my arguments with their values, but the second time they are all equal to "NULL"... So in my submit function of the form, I can't get them back. I tried to put them in $form_storage, in hidden fields... Nothing works.

Any ideas ?

EDIT : here's my code. There are some parts you may not understand, but at least you'll get a global view of the problem.

The form and the submit :

/**
 * Organize form 
 */
function _bto_ajax_submenu_organize_form($form, &$form_state, $args)
{
  $form['place'] = array(
    '#prefix' => '<div class="left">',
    '#title' => t('Where do you want to go ?', array(), array('langcode' => $args['language'])),
    '#type' => 'select',
    '#options' => _bto_ajax_submenu_organize_form_get_territories($args['territories'], $args['language']),
  );

  $form['activity'] = array(
    '#title' => t('What do you want to do ?', array(), array('langcode' => $args['language'])),
    '#type' => 'select',
    '#options' => _bto_ajax_submenu_organize_form_get_activities($args['activities'], $args['language']),
    '#suffix' => '</div>'
  );

  $form['accommodation'] = array(
    '#prefix' => '<div class="right">',
    '#title' => t('Where do you want to lodge ?', array(), array('langcode' => $args['language'])),
    '#type' => 'select',
    '#options' => _bto_ajax_submenu_organize_form_get_accommodations($args['accommodations'], $args['language']),
    '#suffix' => '</div>'
  );

  if ($form_state['build_info']['args'][0]['language'] != NULL) {
    switch ($args['language']) {
      case 'fr':
        $form['test'] = array(
          '#type' => 'hidden',
          '#value' => 'fr',
        );
        break;

      case 'en':
        $form['test'] = array(
          '#type' => 'hidden',
          '#value' => 'en',
        );
        break;

      case 'de':
        $form['test'] = array(
          '#type' => 'hidden',
          '#value' => 'de',
        );
        break;

      default:
        $form['test'] = array(
          '#type' => 'hidden',
          '#value' => 'fr',
        );
        break;
    }
  }

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t("Let's go !", array(), array('langcode' => $args['language'])),
  );

  return $form;
}


/**
 * Organize form submit
 */
function _bto_ajax_submenu_organize_form_submit($form, &$form_state)
{
  dsm($form_state['build_info']['args']);
  $values = $form_state['values'];
  $storage = $form_state['storage'];
  $query = array();
  dsm($values);

  // Territory
  if ($values['place'] != 0)
    $query['place'] = $values['place'];

  // Activity
  if ($values['activity'] != 0)
    $query['filter'] = $values['activity'];

  // Hébergements
  if ($values['accommodation'] != 0)
    $query['filter2'] = $values['accommodation'];

  $url = get_node_path('109', $storage['lang']);
  $url = str_replace('fr/', '', $url);
  $url = str_replace('en/', '', $url);
  $url = str_replace('de/', '', $url);
  drupal_goto($url, array('query' => $query));
}

The hook menu :

/**
 * Hook menu
 * @return array $items     Contient les items du menu
 */
function bto_menu()
{
    $items = array();
    $items['ajax/%'] = array(
        'access callback' => true,
        'page arguments' => array(1),
        'page callback' => '_bto_ajax',
        'type' => MENU_CALLBACK,
    );
    //...
    return $items;
}

And the calling of the form :

/**
 * Callback ajax route
 * @param  $request 
 */
function _bto_ajax($request) 
{
    extract($_POST);
    switch ($request) {
        // ...             
        case 'menu-principal-organiser':
            $output = _bto_ajax_submenu_load_organize($vars);
            break;        
    }

    print  $output;
}


/**
 * Organize your trip
 */
function _bto_ajax_submenu_load_organize($vars) 
{
    $vars = json_decode($vars);
    $territories = $vars->territories;
    $accommodations = $vars->accommodations;
    $activities = $vars->activities;
    $language = $vars->language;

    // Load form
    $form = drupal_get_form("_bto_ajax_submenu_organize_form", array(
        'territories' => $territories, 
        'accommodations' => $accommodations, 
        'activities' => $activities,
        'language' => $language,
    ));

    // Load packaged offers
    // ...

    // Load template
    $output = theme('submenu-organize', array(
        'form' => drupal_render($form),
        'packaged_offers' => $packaged_offers,
        'language' => $language,
    ));

    return $output;
}

Upvotes: 0

Views: 470

Answers (1)

Anurag
Anurag

Reputation: 555

Ensure that Drupal.attachBehaviors() is called from your AJAX callback. See Drupal 7 JavaScript API for more details.

Upvotes: 0

Related Questions