public static function WidgetBase::addMoreAjax

public static WidgetBase::addMoreAjax(array $form, FormStateInterface $form_state)

Ajax callback for the "Add another item" button.

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

File

core/lib/Drupal/Core/Field/WidgetBase.php, line 291

Class

WidgetBase
Base class for 'Field widget' plugin implementations.

Namespace

Drupal\Core\Field

Code

public static function addMoreAjax(array $form, FormStateInterface $form_state) {
  $button = $form_state->getTriggeringElement();

  // Go one level up in the form, to the widgets container.
  $element = NestedArray::getValue($form, array_slice($button['#array_parents'], 0, -1));

  // Ensure the widget allows adding additional items.
  if ($element['#cardinality'] != FieldStorageDefinitionInterface::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;
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Field!WidgetBase.php/function/WidgetBase::addMoreAjax/8.1.x