You are here

public function SearchApiBulkForm::viewsForm in Search API 8

Form constructor for the bulk form.

Search API supports also non-entity datasources but, as actions require an entity, we don't show the checkbox for such rows. Unfortunately it's hard to extend this method, so we are forking the parent's method.

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 BulkForm::viewsForm

File

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

Class

SearchApiBulkForm
Defines an actions-based bulk operation form element.

Namespace

Drupal\search_api\Plugin\views\field

Code

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

  // phpcs:enable
  // Make sure we do not accidentally cache this form.
  // @todo Evaluate this again in https://www.drupal.org/node/2503009.
  $form['#cache']['max-age'] = 0;

  // Add the tableselect javascript.
  $form['#attached']['library'][] = 'core/drupal.tableselect';
  $use_revision = array_key_exists('revision', $this->view
    ->getQuery()
    ->getEntityTableInfo());

  // Only add the bulk form options and buttons if there are results.
  if (!empty($this->view->result)) {

    // Render checkboxes for all rows.
    $form[$this->options['id']]['#tree'] = TRUE;
    foreach ($this->view->result as $row_index => $row) {

      // Search API supports also non-entity datasources but, as actions
      // require an entity, we don't show the checkbox for such rows.
      if (!$this
        ->getEntity($row)) {
        continue;
      }
      $entity = $this
        ->getEntityTranslation($this
        ->getEntity($row), $row);
      $form[$this->options['id']][$row_index] = [
        '#type' => 'checkbox',
        // We are not able to determine a main "title" for each row, so we can
        // only output a generic label.
        '#title' => $this
          ->t('Update this item'),
        '#title_display' => 'invisible',
        '#default_value' => !empty($form_state
          ->getValue($this->options['id'])[$row_index]) ? 1 : NULL,
        '#return_value' => $this
          ->calculateEntityBulkFormKey($entity, $use_revision),
      ];
    }

    // Replace the form submit button label.
    $form['actions']['submit']['#value'] = $this
      ->t('Apply to selected items');

    // Ensure a consistent container for filters/operations in the view header.
    $form['header'] = [
      '#type' => 'container',
      '#weight' => -100,
    ];

    // Build the bulk operations action widget for the header.
    // Allow themes to apply .container-inline on this separate container.
    $form['header'][$this->options['id']] = [
      '#type' => 'container',
    ];
    $form['header'][$this->options['id']]['action'] = [
      '#type' => 'select',
      '#title' => $this->options['action_title'],
      '#options' => $this
        ->getBulkOptions(),
    ];

    // Duplicate the form actions into the action container in the header.
    $form['header'][$this->options['id']]['actions'] = $form['actions'];
  }
  else {

    // Remove the default actions build array.
    unset($form['actions']);
  }
}