Commit f946527e authored by webchick's avatar webchick

Issue #1851082 by tim.plunkett: Add a base bulk form handler class, remove it from Actions module.

parent b3f18a65
......@@ -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(
......
......@@ -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 '<!--form-item-' . $this->options['id'] . '--' . $this->view->row_index . '-->';
}
/**
* 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() {
}
}
......@@ -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
......
<?php
/**
* @file
* Contains \Drupal\system\Plugin\views\field\BulkFormBase.
*/
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 generic bulk operation form element.
*/
abstract class BulkFormBase extends FieldPluginBase {
/**
* Overrides \Drupal\views\Plugin\views\Plugin\field\FieldPluginBase::render().
*/
public function render($values) {
return '<!--form-item-' . $this->options['id'] . '--' . $this->view->row_index . '-->';
}
/**
* 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 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'] = '';
}
}
/**
* 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.
$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,
);
}
// 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' => $this->getBulkOptions(),
);
// Duplicate the form actions into the action container in the header.
$form['header'][$this->options['id']]['actions'] = $form['actions'];
}
/**
* Returns the available operations for this form.
*
* @return array
* An associative array of operations, suitable for a select element.
*/
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().
*/
public function query() {
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment