public function BulkForm::views_form

Implements \Drupal\views\Plugin\views\Plugin\field\FieldPluginBase::views_form().

File

drupal/core/modules/action/lib/Drupal/action/Plugin/views/field/BulkForm.php, line 49
Contains \Drupal\action\Plugin\views\field\BulkForm.

Class

BulkForm
Defines a simple bulk operation form element.

Namespace

Drupal\action\Plugin\views\field

Code

public function views_form(&$form, &$form_state) {

  // Add the tableselect javascript.
  $form['#attached']['library'][] = array(
    'system',
    'drupal.tableselect',
  );

  // Render checkboxes for all rows.
  foreach ($this->view->result as $row_index => $row) {
    $entity_id = $this
      ->get_value($row);
    $form[$this->options['id']][$row_index] = array(
      '#type' => 'checkbox',
      '#default_value' => FALSE,
    );
  }
  $form[$this->options['id']]['#tree'] = TRUE;

  // Get all available actions.
  $actions = action_get_all_actions();
  $entity_type = $this
    ->getEntityType();

  // Filter actions by the entity type and build options for the form.
  $actions = array_filter($actions, function ($action) use ($entity_type) {
    return $action['type'] == $entity_type && empty($action['configurable']);
  });
  $options = array_map(function ($action) {
    return $action['label'];
  }, $actions);
  $form['action'] = array(
    '#type' => 'select',
    '#title' => t('Action'),
    '#options' => $options,
    '#description' => t('Select the action you want to execute on the content entitites.'),
  );

  // Move the submit button beside the selection.
  $form['actions']['#weight'] = 1;

  // Replace the text with Update.
  $form['actions']['submit']['#value'] = t('Update');

  // Put the submit button both at the top and bottom.
  $form['actions_bottom'] = $form['actions'];
  $form['actions_bottom']['#weight'] = 100;
}