You are here

public function ViewsBulkOperationsBulkForm::viewsFormValidate in Views Bulk Operations (VBO) 4.0.x

Same name and namespace in other branches
  1. 8.3 src/Plugin/views/field/ViewsBulkOperationsBulkForm.php \Drupal\views_bulk_operations\Plugin\views\field\ViewsBulkOperationsBulkForm::viewsFormValidate()
  2. 8 src/Plugin/views/field/ViewsBulkOperationsBulkForm.php \Drupal\views_bulk_operations\Plugin\views\field\ViewsBulkOperationsBulkForm::viewsFormValidate()
  3. 8.2 src/Plugin/views/field/ViewsBulkOperationsBulkForm.php \Drupal\views_bulk_operations\Plugin\views\field\ViewsBulkOperationsBulkForm::viewsFormValidate()

File

src/Plugin/views/field/ViewsBulkOperationsBulkForm.php, line 987

Class

ViewsBulkOperationsBulkForm
Defines the Views Bulk Operations field plugin.

Namespace

Drupal\views_bulk_operations\Plugin\views\field

Code

public function viewsFormValidate(&$form, FormStateInterface $form_state) {
  if ($this->options['buttons']) {
    $trigger = $form_state
      ->getTriggeringElement();
    $action_delta = end($trigger['#parents']);
    $form_state
      ->setValue('action', $action_delta);
  }
  else {
    $action_delta = $form_state
      ->getValue('action');
  }
  if ($action_delta === '') {
    $form_state
      ->setErrorByName('action', $this
      ->t('Please select an action to perform.'));
  }
  else {
    if (!isset($this->options['selected_actions'][$action_delta])) {
      $form_state
        ->setErrorByName('action', $this
        ->t('Form error occurred, please try again.'));
    }
    elseif (!isset($this->actions[$this->options['selected_actions'][$action_delta]['action_id']])) {
      $form_state
        ->setErrorByName('action', $this
        ->t('Form error occurred, Unavailable action selected.'));
    }
  }
  if (!$form_state
    ->getValue('select_all')) {

    // Update tempstore data to make sure we have also
    // results selected in other requests and validate if
    // anything is selected.
    $this->tempStoreData = $this
      ->getTempstoreData();
    $selected = array_filter($form_state
      ->getValue($this->options['id']));
    if (empty($this->tempStoreData['list']) && empty($selected)) {
      $form_state
        ->setErrorByName('', $this
        ->t('No items selected.'));
    }
  }

  // Action config validation (if implemented).
  if (empty($this->options['form_step']) && !empty($form['header'][$this->options['id']]['configuration']['#config_included'])) {
    $action_id = $form_state
      ->getValue('action');
    $action = $this->actions[$action_id];
    if (method_exists($action['class'], 'validateConfigurationForm')) {
      $actionObject = $this->actionManager
        ->createInstance($action_id);
      $actionObject
        ->validateConfigurationForm($form['header'][$this->options['id']]['configuration'], $form_state);
    }
  }

  // Update bulk form key list if the form has errors, as data might have
  // changed before validation took place.
  if ($form_state
    ->getErrors()) {
    $bulk_form_keys = [];
    foreach ($form[$this->options['id']] as $row_index => $element) {
      if (is_numeric($row_index) && isset($element['#return_value'])) {
        $bulk_form_keys[$row_index] = $element['#return_value'];
      }
    }
    $this
      ->updateTempstoreData($bulk_form_keys);
  }
}