function field_add_more_js

Ajax callback: Responds to a new empty widget being added to the form.

This returns the new page content to replace the page content made obsolete by the form submission.

See also

field_add_more_submit()

1 string reference to 'field_add_more_js'
WidgetBase::formMultipleElements in drupal/core/modules/field/lib/Drupal/field/Plugin/Type/Widget/WidgetBase.php
Special handling to create form elements for multiple values.

File

drupal/core/modules/field/field.form.inc, line 150
Field forms management.

Code

function field_add_more_js($form, $form_state) {
  $button = $form_state['triggering_element'];

  // Go one level up in the form, to the widgets container.
  $element = NestedArray::getValue($form, array_slice($button['#array_parents'], 0, -1));
  $field_name = $element['#field_name'];
  $langcode = $element['#language'];
  $parents = $element['#field_parents'];
  $field_state = field_form_get_state($parents, $field_name, $langcode, $form_state);
  $field = $field_state['field'];
  if ($field['cardinality'] != FIELD_CARDINALITY_UNLIMITED) {
    return;
  }

  // Add a DIV around the delta receiving the Ajax effect.
  $delta = $element['#max_delta'];
  $element[$delta]['#prefix'] = '<div class="ajax-new-content">' . (isset($element[$delta]['#prefix']) ? $element[$delta]['#prefix'] : '');
  $element[$delta]['#suffix'] = (isset($element[$delta]['#suffix']) ? $element[$delta]['#suffix'] : '') . '</div>';
  return $element;
}