You are here

public function WebformBulkFormBase::submitForm in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Form/WebformBulkFormBase.php \Drupal\webform\Form\WebformBulkFormBase::submitForm()

Form submission handler.

Parameters

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

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

Overrides FormInterface::submitForm

File

src/Form/WebformBulkFormBase.php, line 119

Class

WebformBulkFormBase
Provides the webform bulk form base.

Namespace

Drupal\webform\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $actions = $this
    ->getActions();

  // If the action does exist, skip it and assume that someone has altered
  // the form and added a custom action.
  if (!isset($actions[$form_state
    ->getValue('action')])) {
    return;
  }
  $action = $actions[$form_state
    ->getValue('action')];
  $entity_ids = array_filter($form_state
    ->getValue('items'));
  $entities = $this->entityTypeManager
    ->getStorage($this->entityTypeId)
    ->loadMultiple($entity_ids);
  foreach ($entities as $key => $entity) {

    // Skip execution if the user did not have access.
    if (!$action
      ->getPlugin()
      ->access($entity, $this
      ->currentUser())) {
      $this
        ->messenger()
        ->addError($this
        ->t('No access to execute %action on the @entity_type_label %entity_label.', [
        '%action' => $action
          ->label(),
        '@entity_type_label' => $entity
          ->getEntityType()
          ->getLabel(),
        '%entity_label' => $entity
          ->label(),
      ]));
      unset($entities[$key]);
      continue;
    }
  }
  $count = count($entities);

  // If there were entities selected but the action isn't allowed on any of
  // them, we don't need to do anything further.
  if (!$count) {
    return;
  }
  $action
    ->execute($entities);
  $operation_definition = $action
    ->getPluginDefinition();
  if (!empty($operation_definition['confirm_form_route_name'])) {
    $options = [
      'query' => $this
        ->getDestinationArray(),
    ];
    $form_state
      ->setRedirect($operation_definition['confirm_form_route_name'], [], $options);
  }
  else {

    // Don't display the message unless there are some elements affected and
    // there is no confirmation form.
    $this
      ->messenger()
      ->addStatus($this
      ->formatPlural($count, '%action was applied to @count item.', '%action was applied to @count items.', [
      '%action' => $action
        ->label(),
    ]));
  }
}