You are here

public static function WebformComputedBase::ajaxWebformComputedCallback in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Element/WebformComputedBase.php \Drupal\webform\Element\WebformComputedBase::ajaxWebformComputedCallback()

Webform computed element Ajax callback.

Parameters

array $form: An associative array containing the structure of the form.

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

Return value

array The computed element element.

File

src/Element/WebformComputedBase.php, line 266

Class

WebformComputedBase
Provides a base class for 'webform_computed' elements.

Namespace

Drupal\webform\Element

Code

public static function ajaxWebformComputedCallback(array $form, FormStateInterface $form_state) {
  $button = $form_state
    ->getTriggeringElement();
  $element = NestedArray::getValue($form, array_slice($button['#array_parents'], 0, -1));

  // Set element value and #markup  after the form has been validated.
  $webform_submission = static::getWebformSubmission($element, $form_state, $form);
  $value = static::computeValue($element, $webform_submission);
  static::setWebformComputedElementValue($element, $value);

  // Only return the wrapper id, this prevents the computed element from
  // being reinitialized via JS after each update.
  // @see js/webform.element.computed.js
  //
  // The announce attribute allows FAPI Ajax callbacks to easily
  // trigger announcements.
  // @see js/webform.announce.js
  $t_args = [
    '@title' => $element['#title'],
    '@value' => strip_tags($value),
  ];
  $attributes = [
    'class' => [
      'js-webform-computed-wrapper',
    ],
    'id' => $element['#wrapper_id'],
    'data-webform-announce' => t('@title is @value', $t_args),
  ];
  $element['#prefix'] = '<div' . new Attribute($attributes) . '>';
  $element['#suffix'] = '</div>';

  // Disable states and flexbox wrapper.
  // @see \Drupal\webform\Plugin\WebformElementBase::preRenderFixFlexboxWrapper
  $element['#webform_wrapper'] = FALSE;
  return $element;
}