function ViewsFormAjaxHelperTrait::addAjaxWrapper

Same name and namespace in other branches
  1. main core/modules/views/src/ViewsFormAjaxHelperTrait.php \Drupal\views\ViewsFormAjaxHelperTrait::addAjaxWrapper()

Adds a wrapper to a form region (for AJAX refreshes) after the build.

This function inserts a wrapper around the region of the form that needs to be refreshed by AJAX, based on information stored in the corresponding submit-button form element.

Parameters

array $element: The form element render array.

\Drupal\Core\Form\FormStateInterface $formState: The current state of the form.

Return value

array Render array.

File

core/modules/views/src/ViewsFormAjaxHelperTrait.php, line 190

Class

ViewsFormAjaxHelperTrait
Provides reusable code to be shared by Views Ajax forms.

Namespace

Drupal\views

Code

public static function addAjaxWrapper(array $element, FormStateInterface $formState) : array {
  // Find the region of the complete form that needs to be refreshed by AJAX.
  // This was earlier stored in a property on the element.
  $completeForm =& $formState->getCompleteForm();
  $refreshParents = $element['#views_ui_ajax_data']['refresh_parents'];
  $refreshElement = NestedArray::getValue($completeForm, $refreshParents);
  // The HTML ID that AJAX expects was also stored in a property on the
  // element, so use that information to insert the wrapper <div> here.
  $id = $element['#views_ui_ajax_data']['wrapper'];
  $refreshElement += [
    '#prefix' => '',
    '#suffix' => '',
  ];
  $refreshElement['#prefix'] = '<div id="' . $id . '" class="views-ui-ajax-wrapper">' . $refreshElement['#prefix'];
  $refreshElement['#suffix'] .= '</div>';
  // Copy the element that needs to be refreshed back into the form, with our
  // modifications to it.
  NestedArray::setValue($completeForm, $refreshParents, $refreshElement);
  return $element;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.