You are here

public function SubmitButtonAjax::processForm in Flexiform 8

Process Form Enhancer.

File

src/Plugin/FormEnhancer/SubmitButtonAjax.php, line 104

Class

SubmitButtonAjax
FormEnhancer for altering the ajax settings of submit buttons.

Namespace

Drupal\flexiform\Plugin\FormEnhancer

Code

public function processForm($element, FormStateInterface $form_state, $form) {
  $needs_wrapping = FALSE;
  $wrapper_id = $form['#build_id'] . '-ajax-wrapper';
  foreach ($this->configuration as $key => $ajax_info) {
    if (empty($ajax_info['enabled'])) {
      continue;
    }
    $array_parents = explode('::', $key);
    $button = NestedArray::getValue($element, $array_parents, $exists);
    if ($exists) {
      $needs_wrapping = TRUE;
      $button['#ajax'] = [
        'wrapper' => $wrapper_id,
        'callback' => [
          static::class,
          'formAjaxCallback',
        ],
        'flexiform' => $ajax_info,
      ];
      NestedArray::setValue($element, $array_parents, $button);
    }
  }
  if ($needs_wrapping) {
    $element['#prefix'] = '<div id="' . $wrapper_id . '">' . (!empty($element['#prefix']) ? $element['#prefix'] : '');
    $element['#suffix'] = (!empty($element['#suffix']) ? $element['#suffix'] : '') . '</div>';
  }
  return $element;
}