diff --git a/core/modules/action/action.views.inc b/core/modules/action/action.views.inc index cb62a13..e933392 100644 --- a/core/modules/action/action.views.inc +++ b/core/modules/action/action.views.inc @@ -14,7 +14,11 @@ * the alter hook doesn't load the *.views.inc automatically. */ function action_views_data() { - $data['views']['action_bulk_form'] = array( + $data['action']['table']['group'] = t('Action'); + $data['action']['table']['join'] = array( + '#global' => array(), + ); + $data['action']['action_bulk_form'] = array( 'title' => t('Bulk update'), 'help' => t('Allows users to apply an action to one or more items.'), 'field' => array( diff --git a/core/modules/action/lib/Drupal/action/Plugin/views/field/BulkForm.php b/core/modules/action/lib/Drupal/action/Plugin/views/field/BulkForm.php index 23ec58f..33556de 100644 --- a/core/modules/action/lib/Drupal/action/Plugin/views/field/BulkForm.php +++ b/core/modules/action/lib/Drupal/action/Plugin/views/field/BulkForm.php @@ -8,61 +8,22 @@ namespace Drupal\action\Plugin\views\field; use Drupal\Core\Annotation\Plugin; -use Drupal\views\Plugin\views\field\FieldPluginBase; +use Drupal\system\Plugin\views\field\BulkFormBase; /** - * Defines a simple bulk operation form element. + * Defines a actions-based bulk operation form element. * * @Plugin( * id = "action_bulk_form", * module = "action" * ) */ -class BulkForm extends FieldPluginBase { +class BulkForm extends BulkFormBase { /** - * Overrides \Drupal\views\Plugin\views\Plugin\field\FieldPluginBase::render(). + * Implements \Drupal\system\Plugin\views\field\BulkFormBase::getBulkOptions(). */ - public function render($values) { - return ''; - } - - /** - * Overrides \Drupal\views\Plugin\views\Plugin\field\FieldPluginBase::pre_render(). - */ - public function pre_render(&$values) { - parent::pre_render($values); - - // If the view is using a table style, provide a placeholder for a - // "select all" checkbox. - if (!empty($this->view->style_plugin) && $this->view->style_plugin instanceof \Drupal\views\Plugin\views\style\Table) { - // Add the tableselect css classes. - $this->options['element_label_class'] .= 'select-all'; - // Hide the actual label of the field on the table header. - $this->options['label'] = ''; - } - } - - /** - * Implements \Drupal\views\Plugin\views\Plugin\field\FieldPluginBase::views_form(). - */ - public function views_form(&$form, &$form_state) { - // Add the tableselect javascript. - $form['#attached']['library'][] = array('system', 'drupal.tableselect'); - - // Render checkboxes for all rows. - $form[$this->options['id']]['#tree'] = TRUE; - foreach ($this->view->result as $row_index => $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' => t('Update this item'), - '#title_display' => 'invisible', - '#default_value' => !empty($form_state['values'][$this->options['id']][$row_index]) ? 1 : NULL, - ); - } - + protected function getBulkOptions() { // Get all available actions. $actions = action_get_all_actions(); $entity_type = $this->getEntityType(); @@ -70,36 +31,13 @@ public function views_form(&$form, &$form_state) { $actions = array_filter($actions, function($action) use ($entity_type) { return $action['type'] == $entity_type && empty($action['configurable']); }); - $options = array_map(function($action) { + return array_map(function($action) { return $action['label']; }, $actions); - - // Replace the form submit button label. - $form['actions']['submit']['#value'] = 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' => t('With selection'), - '#options' => $options, - ); - - // Duplicate the form actions into the action container in the header. - $form['header'][$this->options['id']]['actions'] = $form['actions']; } /** - * Implements \Drupal\views\Plugin\views\Plugin\field\FieldPluginBase::views_form_submit(). + * Implements \Drupal\system\Plugin\views\field\BulkFormBase::views_form_submit(). */ public function views_form_submit(&$form, &$form_state) { if ($form_state['step'] == 'views_form_views_form') { @@ -127,10 +65,4 @@ public function views_form_submit(&$form, &$form_state) { } } - /** - * Overrides \Drupal\views\Plugin\views\Plugin\field\FieldPluginBase::query(). - */ - public function query() { - } - } diff --git a/core/modules/action/tests/action_bulk_test/config/views.view.test_bulk_form.yml b/core/modules/action/tests/action_bulk_test/config/views.view.test_bulk_form.yml index c89b270..744e735 100644 --- a/core/modules/action/tests/action_bulk_test/config/views.view.test_bulk_form.yml +++ b/core/modules/action/tests/action_bulk_test/config/views.view.test_bulk_form.yml @@ -74,7 +74,7 @@ display: link_to_node: '1' bulk_form: id: action_bulk_form - table: views + table: action field: action_bulk_form relationship: none group_type: group diff --git a/core/modules/action/lib/Drupal/action/Plugin/views/field/BulkForm.php b/core/modules/system/lib/Drupal/system/Plugin/views/field/BulkFormBase.php similarity index 58% copy from core/modules/action/lib/Drupal/action/Plugin/views/field/BulkForm.php copy to core/modules/system/lib/Drupal/system/Plugin/views/field/BulkFormBase.php index 23ec58f..53d54df 100644 --- a/core/modules/action/lib/Drupal/action/Plugin/views/field/BulkForm.php +++ b/core/modules/system/lib/Drupal/system/Plugin/views/field/BulkFormBase.php @@ -2,23 +2,19 @@ /** * @file - * Contains \Drupal\action\Plugin\views\field\BulkForm. + * Contains \Drupal\system\Plugin\views\field\BulkFormBase. */ -namespace Drupal\action\Plugin\views\field; +namespace Drupal\system\Plugin\views\field; use Drupal\Core\Annotation\Plugin; use Drupal\views\Plugin\views\field\FieldPluginBase; +use Drupal\views\Plugin\views\style\Table; /** - * Defines a simple bulk operation form element. - * - * @Plugin( - * id = "action_bulk_form", - * module = "action" - * ) + * Defines a generic bulk operation form element. */ -class BulkForm extends FieldPluginBase { +abstract class BulkFormBase extends FieldPluginBase { /** * Overrides \Drupal\views\Plugin\views\Plugin\field\FieldPluginBase::render(). @@ -35,7 +31,7 @@ public function pre_render(&$values) { // If the view is using a table style, provide a placeholder for a // "select all" checkbox. - if (!empty($this->view->style_plugin) && $this->view->style_plugin instanceof \Drupal\views\Plugin\views\style\Table) { + if (!empty($this->view->style_plugin) && $this->view->style_plugin instanceof Table) { // Add the tableselect css classes. $this->options['element_label_class'] .= 'select-all'; // Hide the actual label of the field on the table header. @@ -44,7 +40,12 @@ public function pre_render(&$values) { } /** - * Implements \Drupal\views\Plugin\views\Plugin\field\FieldPluginBase::views_form(). + * Form constructor for the bulk form. + * + * @param array $form + * An associative array containing the structure of the form. + * @param array $form_state + * An associative array containing the current state of the form. */ public function views_form(&$form, &$form_state) { // Add the tableselect javascript. @@ -63,17 +64,6 @@ public function views_form(&$form, &$form_state) { ); } - // Get all available actions. - $actions = action_get_all_actions(); - $entity_type = $this->getEntityType(); - // Filter actions by entity type and build select options. - $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); - // Replace the form submit button label. $form['actions']['submit']['#value'] = t('Apply'); @@ -91,7 +81,7 @@ public function views_form(&$form, &$form_state) { $form['header'][$this->options['id']]['action'] = array( '#type' => 'select', '#title' => t('With selection'), - '#options' => $options, + '#options' => $this->getBulkOptions(), ); // Duplicate the form actions into the action container in the header. @@ -99,33 +89,22 @@ public function views_form(&$form, &$form_state) { } /** - * Implements \Drupal\views\Plugin\views\Plugin\field\FieldPluginBase::views_form_submit(). + * Returns the available operations for this form. + * + * @return array + * An associative array of operations, suitable for a select element. */ - public function views_form_submit(&$form, &$form_state) { - if ($form_state['step'] == 'views_form_views_form') { - $action = $form_state['values']['action']; - $action = action_load($action); - $count = 0; - - // Filter only selected checkboxes. - $selected = array_filter($form_state['values'][$this->options['id']]); - - if (!empty($selected)) { - foreach (array_keys($selected) as $row_index) { - $entity = $this->get_entity($this->view->result[$row_index]); - actions_do($action->aid, $entity); - $entity->save(); - $count++; - } - } - - if ($count) { - drupal_set_message(format_plural($count, '%action was applied to @count item.', '%action was applied to @count items.', array( - '%action' => $action->label, - ))); - } - } - } + abstract protected function getBulkOptions(); + + /** + * Submit handler for the bulk form. + * + * @param array $form + * An associative array containing the structure of the form. + * @param array $form_state + * An associative array containing the current state of the form. + */ + abstract public function views_form_submit(&$form, &$form_state); /** * Overrides \Drupal\views\Plugin\views\Plugin\field\FieldPluginBase::query().