You are here

public function BulkForm::viewsForm in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Plugin/views/field/BulkForm.php \Drupal\system\Plugin\views\field\BulkForm::viewsForm()

Form constructor for the bulk form.

Parameters

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

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

1 call to BulkForm::viewsForm()
UserBulkForm::viewsForm in core/modules/user/src/Plugin/views/field/UserBulkForm.php
Provide a more useful title to improve the accessibility.
1 method overrides BulkForm::viewsForm()
UserBulkForm::viewsForm in core/modules/user/src/Plugin/views/field/UserBulkForm.php
Provide a more useful title to improve the accessibility.

File

core/modules/system/src/Plugin/views/field/BulkForm.php, line 251
Contains \Drupal\system\Plugin\views\field\BulkForm.

Class

BulkForm
Defines a actions-based bulk operation form element.

Namespace

Drupal\system\Plugin\views\field

Code

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

  // 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) {
      $entity = $this
        ->getEntityTranslation($this
        ->getEntity($row), $row);
      $form[$this->options['id']][$row_index] = array(
        '#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');

    // Ensure a consistent container for filters/operations in the view header.
    $form['header'] = array(
      '#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']] = array(
      '#type' => 'container',
    );
    $form['header'][$this->options['id']]['action'] = array(
      '#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']);
  }
}