You are here

public function SearchApiBulkForm::viewsFormValidate in Search API 8

Overrides BulkForm::viewsFormValidate

File

src/Plugin/views/field/SearchApiBulkForm.php, line 188

Class

SearchApiBulkForm
Defines an actions-based bulk operation form element.

Namespace

Drupal\search_api\Plugin\views\field

Code

public function viewsFormValidate(&$form, FormStateInterface $form_state) {

  // As the view might contain rows from diverse entity types and an action
  // is designed to act only on a specific entity type, we remove the
  // incompatible selected rows from the selection and popup a warning.
  // @todo Use Javascript to already reflect this in the UI.
  $user_input = $form_state
    ->getUserInput();
  $input_key = $this->options['id'];
  $selected = $form_state
    ->getValue($input_key);
  $action = $this->actions[$form_state
    ->getValue('action')];
  $removed_entities = [];
  foreach ($selected as $delta => $bulk_form_key) {
    if ($bulk_form_key) {
      try {
        $entity = $this
          ->loadEntityFromBulkFormKey($bulk_form_key);
      } catch (\Exception $e) {
        $entity = NULL;
      }
      if (!$entity || $entity
        ->getEntityTypeId() !== $action
        ->getType()) {
        $removed_entities[] = $entity
          ->label();
        unset($selected[$delta]);
      }
    }
  }
  if ($removed_entities) {
    $form_state
      ->setValue($input_key, $selected);
    $user_input[$input_key] = $selected;
    $form_state
      ->setUserInput($user_input);
    $this
      ->messenger()
      ->addWarning($this
      ->formatPlural(count($removed_entities), "Row %items removed from selection as it's not compatible with %action action.", 'Rows %items removed from selection as they are not compatible with %action action.', [
      '%action' => $action
        ->label(),
      '%items' => implode(', ', $removed_entities),
    ]));
  }
  parent::viewsFormValidate($form, $form_state);
}