0

Normally the #ajax property of a form item launches a ajax callback. So when you select an item of a drop-down menu it will be triggered.

In this case I use variable_get() to see if the drop-down menu value has already been saved on that admin page. If it is I use it as the default value. But in that case what does not happen is the ajax call that is associated with that value.

So this is what I would like to have:

if(isset(variable_get('dropdown_value'))) {
  // Manually call ajax callback function, just like the one in $form[item][#ajax][callback].
}

Does anybody know how to do this? I would prefer to use php instead of JavaScript.

1

If I understand correctly, anything that can query a URL would do, for example Curl.

0

I'm assuming that you want the behaviour caused by the AJAX callback to take effect on the form even if the AJAX request hasn't been made?

The best way to do this would be to move the logic out of the AJAX callback function and make all necessary form changes in the form function itself. Then in your AJAX callback you just need to return the element you're interested in. For example your code might look like this:

// Simple form presenting a dropdown which changes the value in a textfield.
function mymodule_dropdown_form($form, &$form_state) {
  // If the dropdown value is set in $form_state (which happens when the form
  // is re-built for an AJAX request), use that value.
  if (isset($form_state['values']['dropdown1'])) {
    $dropdown1_value = $form_state['values']['dropdown1'];
  }
  // Otherwise if the variable has already been set, use that value
  else if ($variable_value = variable_get('dropdown_value', FALSE)) {
    $dropdown1_value = $variable_value;
  }
  // Otherwise choose your default value
  else {
    $dropdown1_value = 1;
  }

  $form['dropdown1'] = array(
    '#type' => 'select',
    '#title' => 'Dropdown 1',
    '#options' => drupal_map_assoc(array(1, 2, 3, 4, 5)),
    '#default_value' => $dropdown1_value,
    '#ajax' => array(
      'callback' => 'mymodule_dropdown_form_callback',
      'wrapper' => 'textfield-wrapper'
    )
  );

  // Now decide on the value for the textfield.
  if ($dropdown1_value == 1) {
    $textfield_value = 'Value 1';
  }
  else if ($dropdown1_value == 2) {
    $textfield_value = 'Value 2';
  }
  // etc...

  // Now we have a definitive value for the textfield regardless of context.
  $form['textbox'] = array(
    '#type' => 'textfield',
    '#title' => 'Text Field',
    '#default_value' => $textfield_value,
    '#prefix' => '<div id="textfield-wrapper">',
    '#suffix' => '</div>'
  );

  return $form;
}


function mymodule_dropdown_form_callback(&$form, &$form_state) {
  // The element is already build correctly at this point so just return it.
  return $form['textbox'];
}

That's a very rudimentary example and will need a bit of massaging before it does anything useful but it should give you a place to start.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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