Commit 206598cc authored by catch's avatar catch

Issue #1826604 by dawehner, damiankloip, tim.plunkett: Add a dropbutton field handler to Views.

parent c34cdb02
<?php
/**
* @file
* Contains \Drupal\views\Plugin\views\field\Dropbutton.
*/
namespace Drupal\views\Plugin\views\field;
use Drupal\Core\Annotation\Plugin;
/**
* Provides a handler that renders links as dropbutton.
*
* @ingroup views_field_handlers
*
* @Plugin(
* id = "dropbutton"
* )
*/
class Dropbutton extends Links {
/**
* Render the dropdown button.
*/
public function render($values) {
$links = $this->getLinks();
if (!empty($links)) {
return array(
'#type' => 'dropbutton',
'#links' => $links,
);
}
else {
return '';
}
}
}
......@@ -844,14 +844,13 @@ public function buildOptionsForm(&$form, &$form_state) {
// Get a list of the available fields and arguments for token replacement.
$options = array();
foreach ($this->view->display_handler->getHandlers('field') as $field => $handler) {
$options[t('Fields')]["[$field]"] = $handler->adminLabel();
// We only use fields up to (and including) this one.
if ($field == $this->options['id']) {
break;
}
// Setup the tokens for fields.
$previous = $this->getPreviousFieldLabels();
foreach ($previous as $id => $label) {
$options[t('Fields')]["[$id]"] = $label;
}
$count = 0; // This lets us prepare the key as we want it printed.
foreach ($this->view->display_handler->getHandlers('argument') as $arg => $handler) {
$options[t('Arguments')]['%' . ++$count] = t('@argument title', array('@argument' => $handler->adminLabel()));
......@@ -1072,6 +1071,18 @@ public function buildOptionsForm(&$form, &$form_state) {
);
}
/**
* Returns all field lables of fields before this field.
*
* @return array
* An array of field labels keyed by their field IDs.
*/
protected function getPreviousFieldLabels() {
$all_fields = $this->view->display_handler->getFieldLabels();
$field_options = array_slice($all_fields, 0, array_search($this->options['id'], array_keys($all_fields)));
return $field_options;
}
/**
* Provide extra data to the administration form
*/
......
<?php
/**
* @file
* Contains \Drupal\views\Plugin\views\field\Links.
*/
namespace Drupal\views\Plugin\views\field;
/**
* A abstract handler which provides a collection of links.
*
* @ingroup views_field_handlers
*/
abstract class Links extends FieldPluginBase {
/**
* Overrides \Drupal\views\Plugin\views\field\FieldPluginBase::defineOptions().
*/
public function defineOptions() {
$options = parent::defineOptions();
$options['fields'] = array('default' => array());
$options['destination'] = array('default' => TRUE, 'bool' => TRUE);
return $options;
}
/**
* Overrides \Drupal\views\Plugin\views\field\FieldPluginBase::defineOptions().
*/
public function buildOptionsForm(&$form, &$form_state) {
parent::buildOptionsForm($form, $form_state);
// Only show fields that precede this one.
$field_options = $this->getPreviousFieldLabels();
$form['fields'] = array(
'#type' => 'checkboxes',
'#title' => t('Fields'),
'#description' => t('Fields to be included as links.'),
'#options' => $field_options,
'#default_value' => $this->options['fields'],
);
$form['destination'] = array(
'#type' => 'checkbox',
'#title' => t('Include destination'),
'#description' => t('Include a "destination" parameter in the link to return the user to the original view upon completing the link action.'),
'#default_value' => $this->options['destination'],
);
}
/**
* Gets the list of links used by this field.
*
* @return array
* The links which are used by the render function.
*/
protected function getLinks() {
$links = array();
foreach ($this->options['fields'] as $field) {
if (empty($this->view->field[$field]->last_render_text)) {
continue;
}
$title = $this->view->field[$field]->last_render_text;
$path = '';
if (!empty($this->view->field[$field]->options['alter']['path'])) {
$path = $this->view->field[$field]->options['alter']['path'];
}
// Make sure that tokens are replaced for this paths as well.
$tokens = $this->get_render_tokens(array());
$path = strip_tags(decode_entities(strtr($path, $tokens)));
$links[$field] = array(
'href' => $path,
'title' => $title,
);
if (!empty($this->options['destination'])) {
$links[$field]['query'] = drupal_get_destination();
}
}
return $links;
}
/**
* Overrides \Drupal\views\Plugin\views\field\FieldPluginBase::query().
*/
public function query() {
}
}
<?php
/**
* @file
* Contains \Drupal\views\Tests\Handler\FieldDropButtonTest.
*/
namespace Drupal\views\Tests\Handler;
use Drupal\views\Tests\ViewTestData;
/**
* Tests the dropbutton field handler.
*
* @see \Drupal\system\Plugin\views\field\Dropbutton
*/
class FieldDropButtonTest extends HandlerTestBase {
/**
* Views used by this test.
*
* @var array
*/
public static $testViews = array('test_dropbutton');
public static function getInfo() {
return array(
'name' => 'Field: Dropbutton',
'description' => 'Tests the dropbutton field handler.',
'group' => 'Views Handlers',
);
}
/**
* Tests dropbutton field.
*/
public function testDropbutton() {
// Create some test nodes.
$nodes = array();
for ($i = 0; $i < 5; $i++) {
$nodes[] = $this->drupalCreateNode();
}
$this->drupalGet('test-dropbutton');
foreach ($nodes as $node) {
$result = $this->xpath('//ul[contains(@class, dropbutton)]/li/a[contains(@href, :path) and text()=:title]', array(':path' => '/node/' . $node->id(), ':title' => $node->label()));
$this->assertEqual(count($result), 1, 'Just one node title link was found.');
$result = $this->xpath('//ul[contains(@class, dropbutton)]/li/a[contains(@href, :path) and text()=:title]', array(':path' => '/node/' . $node->id(), ':title' => t('Custom Text')));
$this->assertEqual(count($result), 1, 'Just one custom link was found.');
}
}
}
api_version: '3.0'
base_field: nid
base_table: node
core: 8.x
description: ''
disabled: '0'
display:
default:
display_plugin: default
id: default
display_title: Master
position: ''
display_options:
access:
type: perm
cache:
type: none
query:
type: views_query
exposed_form:
type: basic
pager:
type: full
options:
items_per_page: '10'
style:
type: default
row:
type: fields
fields:
nid:
id: nid
table: node
field: nid
relationship: none
group_type: group
admin_label: ''
label: Nid
exclude: '1'
alter:
alter_text: '0'
text: ''
make_link: '0'
path: ''
absolute: '0'
external: '0'
replace_spaces: '0'
path_case: none
trim_whitespace: '0'
alt: ''
rel: ''
link_class: ''
prefix: ''
suffix: ''
target: ''
nl2br: '0'
max_length: ''
word_boundary: '1'
ellipsis: '1'
more_link: '0'
more_link_text: ''
more_link_path: ''
strip_tags: '0'
trim: '0'
preserve_tags: ''
html: '0'
element_type: ''
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: '1'
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: '1'
empty: ''
hide_empty: '0'
empty_zero: '0'
hide_alter_empty: '1'
link_to_node: '0'
title:
id: title
table: node
field: title
label: ''
alter:
alter_text: '0'
make_link: '0'
absolute: '0'
trim: '0'
word_boundary: '0'
ellipsis: '0'
strip_tags: '0'
html: '0'
hide_empty: '0'
empty_zero: '0'
link_to_node: '1'
nothing:
id: nothing
table: views
field: nothing
relationship: none
group_type: group
admin_label: ''
label: 'Custom text'
exclude: '0'
alter:
alter_text: '1'
text: 'Custom Text'
make_link: '1'
path: 'node/[nid]'
absolute: '0'
external: '0'
replace_spaces: '0'
path_case: none
trim_whitespace: '0'
alt: ''
rel: ''
link_class: ''
prefix: ''
suffix: ''
target: ''
nl2br: '0'
max_length: ''
word_boundary: '1'
ellipsis: '1'
more_link: '0'
more_link_text: ''
more_link_path: ''
strip_tags: '0'
trim: '0'
preserve_tags: ''
html: '0'
element_type: ''
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: '1'
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: '1'
empty: ''
hide_empty: '0'
empty_zero: '0'
hide_alter_empty: '0'
dropbutton:
id: dropbutton
table: views
field: dropbutton
relationship: none
group_type: group
admin_label: ''
label: Dropbutton
exclude: '0'
alter:
alter_text: '0'
text: ''
make_link: '0'
path: ''
absolute: '0'
external: '0'
replace_spaces: '0'
path_case: none
trim_whitespace: '0'
alt: ''
rel: ''
link_class: ''
prefix: ''
suffix: ''
target: ''
nl2br: '0'
max_length: ''
word_boundary: '1'
ellipsis: '1'
more_link: '0'
more_link_text: ''
more_link_path: ''
strip_tags: '0'
trim: '0'
preserve_tags: ''
html: '0'
element_type: ''
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: '1'
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: '1'
empty: ''
hide_empty: '0'
empty_zero: '0'
hide_alter_empty: '1'
fields:
title: title
nothing: nothing
nid: '0'
destination: '1'
filters:
status:
value: '1'
table: node
field: status
id: status
expose:
operator: '0'
group: '1'
sorts:
created:
id: created
table: node
field: created
order: DESC
title: test_dropbutton
page_1:
display_plugin: page
id: page_1
display_title: Page
position: ''
display_options:
path: test-dropbutton
field:
title:
link_to_node: '0'
human_name: test_dropbutton
module: views
name: test_dropbutton
tag: ''
......@@ -98,5 +98,13 @@ function views_views_data() {
),
);
$data['views']['dropbutton'] = array(
'title' => t('Dropbutton'),
'help' => t('Display fields in a dropbutton.'),
'field' => array(
'id' => 'dropbutton',
),
);
return $data;
}
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