ActionBulkForm.php 3.57 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\views\Plugin\views\field\BulkForm.
6 7
 */

8
namespace Drupal\views\Plugin\views\field;
9

10
use Drupal\Component\Annotation\PluginID;
11
use Drupal\system\Plugin\views\field\BulkFormBase;
12 13

/**
14
 * Defines a actions-based bulk operation form element.
15
 *
16
 * @PluginID("action_bulk_form")
17
 */
18
class ActionBulkForm extends BulkFormBase {
19

20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
  /**
   * Overrides \Drupal\views\Plugin\views\field\FieldPluginBase::defineOptions().
   */
  protected function defineOptions() {
    $options = parent::defineOptions();
    $options['include_exclude'] = array(
      'default' => 'exclude',
    );
    $options['selected_actions'] = array(
      'default' => array(),
    );
    return $options;
  }

  /**
   * Overrides \Drupal\views\Plugin\views\field\FieldPluginBase::buildOptionsForm().
   */
  public function buildOptionsForm(&$form, &$form_state) {
    parent::buildOptionsForm($form, $form_state);

    $form['include_exclude'] = array(
      '#type' => 'radios',
      '#title' => t('Available actions'),
      '#options' => array(
        'exclude' => t('All actions, except selected'),
        'include' => t('Only selected actions'),
      ),
      '#default_value' => $this->options['include_exclude'],
    );
    $form['selected_actions'] = array(
      '#type' => 'checkboxes',
      '#title' => t('Selected actions'),
      '#options' => $this->getBulkOptions(FALSE),
      '#default_value' => $this->options['selected_actions'],
    );
  }

  /**
   * Overrides \Drupal\views\Plugin\views\PluginBase::buildOptionsForm().
   */
  public function validateOptionsForm(&$form, &$form_state) {
    parent::validateOptionsForm($form, $form_state);

    $form_state['values']['options']['selected_actions'] = array_filter($form_state['values']['options']['selected_actions']);
  }

66
  /**
67
   * Implements \Drupal\system\Plugin\views\field\BulkFormBase::getBulkOptions().
68 69 70
   *
   * @param bool $filtered
   *   (optional) Whether to filter actions to selected actions.
71
   */
72
  protected function getBulkOptions($filtered = TRUE) {
73 74
    // Get all available actions.
    $entity_type = $this->getEntityType();
75 76
    $options = array();
    // Filter the action list.
77
    foreach ($this->actions as $id => $action) {
78 79 80 81 82 83 84 85 86 87 88 89 90 91
      if ($filtered) {
        $in_selected = in_array($id, $this->options['selected_actions']);
        // If the field is configured to include only the selected actions,
        // skip actions that were not selected.
        if (($this->options['include_exclude'] == 'include') && !$in_selected) {
          continue;
        }
        // Otherwise, if the field is configured to exclude the selected
        // actions, skip actions that were selected.
        elseif (($this->options['include_exclude'] == 'exclude') && $in_selected) {
          continue;
        }
      }
      // Only allow actions that are valid for this entity type.
92 93
      if (($action->getType() == $entity_type)) {
        $options[$id] = $action->label();
94 95 96 97
      }
    }

    return $options;
98 99 100
  }

  /**
101
   * Implements \Drupal\system\Plugin\views\field\BulkFormBase::views_form_submit().
102 103
   */
  public function views_form_submit(&$form, &$form_state) {
104
    parent::views_form_submit($form, $form_state);
105
    if ($form_state['step'] == 'views_form_views_form') {
106 107
      $count = count(array_filter($form_state['values'][$this->options['id']]));
      $action = $this->actions[$form_state['values']['action']];
108
      if ($count) {
109
        drupal_set_message(format_plural($count, '%action was applied to @count item.', '%action was applied to @count items.', array(
110
          '%action' => $action->label(),
111
        )));
112 113 114 115 116
      }
    }
  }

}