You are here

public static function BackgroundImageFormTrait::attachSubmitHandler in Background Image 8

Same name and namespace in other branches
  1. 2.x src/BackgroundImageFormTrait.php \Drupal\background_image\BackgroundImageFormTrait::attachSubmitHandler()
  2. 2.0.x src/BackgroundImageFormTrait.php \Drupal\background_image\BackgroundImageFormTrait::attachSubmitHandler()

Attaches a submit handler to the given form.

Parameters

callable $handler: The handler to attach.

array $form: The form.

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

1 call to BackgroundImageFormTrait::attachSubmitHandler()
BackgroundImageForm::build in src/Form/BackgroundImageForm.php
Builds the Background Image form.

File

src/BackgroundImageFormTrait.php, line 188

Class

BackgroundImageFormTrait
Trait BackgroundImageFormTrait.

Namespace

Drupal\background_image

Code

public static function attachSubmitHandler(callable $handler, &$form, FormStateInterface $form_state) {
  if (!empty($form['#background_image_submit_handler'])) {
    return;
  }
  $form['#background_image_submit_handler'] = TRUE;

  // Entity form actions.
  foreach ([
    'submit',
    'publish',
    'unpublish',
  ] as $action) {
    if (!empty($form['actions'][$action])) {
      if (!isset($form['actions'][$action]['#submit'])) {
        $form['actions'][$action]['#submit'] = [];
      }
      if (!in_array($handler, $form['actions'][$action]['#submit'])) {
        array_unshift($form['actions'][$action]['#submit'], $handler);
      }
    }
  }

  // Generic submit button.
  if (!empty($form['submit'])) {
    if (!isset($form['submit']['#submit'])) {
      $form['submit']['#submit'] = [];
    }
    if (!in_array($handler, $form['submit']['#submit'])) {
      array_unshift($form['submit']['#submit'], $handler);
    }
  }
  else {
    if (!isset($form['#submit'])) {
      $form['#submit'] = [];
    }
    if (!in_array($handler, $form['#submit'])) {
      array_unshift($form['#submit'], $handler);
    }
  }
}