1

Consider a Drupal 7 page with a list of same-type entity forms: formA, formB, formC. The entity form uses ajax to replace part of itself.

Is there an established way to manage the wrapper id's so that an ajax call for formA knows to replace the wrapper div in formA? If I don't change the wrapper id's, I would have three divs on the page with the same ID.

2

You can use drupal_html_id() which:

Prepares a string for use as a valid HTML ID and guarantees uniqueness.

e.g.

function my_form($form, &$form_state) {
  $id_string = 'my-element-id';
  $unique_id = drupal_html_id($id_string);

  $form['element'] = array(
    '#type' => 'select',
    '#options' => $options,
    '#ajax' => array(
      'wrapper' => $unique_id,
      // ...
    )
  );

  $form['output'] = array(
    '#prefix' => '<div id="' . $unique_id . '">',
    '#suffix' => '</div>',
    '#markup' => 'Some markup'
  );
}

That should guarantee that the AJAX will work no matter how many different instances of the same form you create.

|improve this answer|||||

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.