1 ajax_example_autocomplete.inc ajax_example_node_by_author_autocomplete($form, &$form_state)

Search by title and author.

In this example, we'll look up nodes by title, but we want only nodes that have been authored by a particular user. That means that we'll have to make an autocomplete function which takes a username as an argument, and use #ajax to change the #autocomplete_path based on the selected user.

Although the implementation of the validate handler may look complex, it's just ambitious. The idea here is: 1. Autcomplete to get a valid username. 2. Use #ajax to update the node element with a #autocomplete_callback that gives the context for the username. 3. Do an autcomplete on the node field that is limited by the username.

Parameters

array $form: Form API form.

array $form_state: Form API form state.

Return value

array Form API array.

File

modules/examples/ajax_example/ajax_example_autocomplete.inc, line 258
ajax_example_autocomplete.inc

Code

function ajax_example_node_by_author_autocomplete($form, &$form_state) {

  $form['intro'] = array(
    '#markup' => '<div>' . t("This example uses a user autocomplete to dynamically change a node title autocomplete using #ajax.
      This is a way to get past the fact that we have no other way to provide context to the autocomplete function.
      It won't work very well unless you have a few users who have created some content that you can search for.") . '</div>',
  );

  $form['author'] = array(
    '#type' => 'textfield',
    '#title' => t('Choose the username that authored nodes you are interested in'),
    // Since we just need simple user lookup, we can use the simplest function
    // of them all, user_autocomplete().
    '#autocomplete_path' => 'user/autocomplete',
    '#ajax' => array(
      'callback' => 'ajax_example_node_by_author_ajax_callback',
      'wrapper' => 'autocomplete-by-node-ajax-replace',
    ),
  );

  // This form element with autocomplete will be replaced by #ajax whenever the
  // author changes, allowing the search to be limited by user.
  $form['node'] = array(
    '#type' => 'textfield',
    '#title' => t('Choose a node by title'),
    '#prefix' => '<div id="autocomplete-by-node-ajax-replace">',
    '#suffix' => '</div>',
    '#disabled' => TRUE,
  );

  // When the author changes in the author field, we'll change the
  // autocomplete_path to match.
  if (!empty($form_state['values']['author'])) {
    $author = user_load_by_name($form_state['values']['author']);
    if (!empty($author)) {
      $autocomplete_path = 'examples/ajax_example/node_by_author_autocomplete/' . $author->uid;
      $form['node']['#autocomplete_path'] = $autocomplete_path;
      $form['node']['#title'] = t('Choose a node title authored by %author', array('%author' => $author->name));
      $form['node']['#disabled'] = FALSE;
    }
  }

  $form['actions'] = array(
    '#type' => 'actions',
  );

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

  return $form;
}