Commit ec6fa93c authored by webchick's avatar webchick
Browse files

Issue #1845836 by dawehner, sun, damiankloip, xjm: Make the actions exposed in...

Issue #1845836 by dawehner, sun, damiankloip, xjm: Make the actions exposed in the views bulk operation handler configurable.
parent d569e20a
......@@ -20,20 +20,85 @@
*/
class BulkForm extends BulkFormBase {
/**
* 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']);
}
/**
* Implements \Drupal\system\Plugin\views\field\BulkFormBase::getBulkOptions().
*
* @param bool $filtered
* (optional) Whether to filter actions to selected actions.
*/
protected function getBulkOptions() {
protected function getBulkOptions($filtered = TRUE) {
// 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']);
});
return array_map(function($action) {
return $action['label'];
}, $actions);
$options = array();
// Filter the action list.
foreach ($actions as $id => $action) {
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.
if (($action['type'] == $entity_type) && empty($action['configurable'])) {
$options[$id] = $action['label'];
}
}
return $options;
}
/**
......
......@@ -47,8 +47,8 @@ public function testBulkForm() {
// Make sure a checkbox appears on all rows.
$edit = array();
for ($i = 0; $i < 10; $i++) {
$this->assertFieldById('edit-bulk-form-' . $i, NULL, format_string('The checkbox on row @row appears.', array('@row' => $i)));
$edit["bulk_form[$i]"] = TRUE;
$this->assertFieldById('edit-action-bulk-form-' . $i, NULL, format_string('The checkbox on row @row appears.', array('@row' => $i)));
$edit["action_bulk_form[$i]"] = TRUE;
}
// Set all nodes to sticky and check that.
......@@ -66,7 +66,7 @@ public function testBulkForm() {
$node = node_load($nodes[0]->id());
$this->assertTrue($node->status, 'The node is published.');
$edit = array('bulk_form[0]' => TRUE, 'action' => 'node_unpublish_action');
$edit = array('action_bulk_form[0]' => TRUE, 'action' => 'node_unpublish_action');
$this->drupalPost(NULL, $edit, t('Apply'));
$this->assertText('Unpublish content was applied to 1 item.');
......@@ -78,6 +78,30 @@ public function testBulkForm() {
// The second node should still be published.
$node = node_load($nodes[1]->id(), TRUE);
$this->assertTrue($node->status, 'An unchecked node is still published.');
// Set up to include just the sticky actions.
$view = views_get_view('test_bulk_form');
$display = &$view->storage->getDisplay('default');
$display['display_options']['fields']['action_bulk_form']['include_exclude'] = 'include';
$display['display_options']['fields']['action_bulk_form']['selected_actions']['node_make_sticky_action'] = 'node_make_sticky_action';
$display['display_options']['fields']['action_bulk_form']['selected_actions']['node_make_unsticky_action'] = 'node_make_unsticky_action';
$view->save();
$this->drupalGet('test_bulk_form');
$options = $this->xpath('//select[@id=:id]/option', array(':id' => 'edit-action'));
$this->assertEqual(count($options), 2);
$this->assertOption('edit-action', 'node_make_sticky_action');
$this->assertOption('edit-action', 'node_make_unsticky_action');
// Set up to exclude the sticky actions.
$view = views_get_view('test_bulk_form');
$display = &$view->storage->getDisplay('default');
$display['display_options']['fields']['action_bulk_form']['include_exclude'] = 'exclude';
$view->save();
$this->drupalGet('test_bulk_form');
$this->assertNoOption('edit-action', 'node_make_sticky_action');
$this->assertNoOption('edit-action', 'node_make_unsticky_action');
}
}
......@@ -72,7 +72,7 @@ display:
empty_zero: '0'
link_to_node: '1'
plugin_id: node
bulk_form:
action_bulk_form:
id: action_bulk_form
table: action
field: action_bulk_form
......
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