Commit 418497ee authored by alexpott's avatar alexpott

Issue #2456713 by larowlan, dawehner, jibran: Custom taxonomy field views...

Issue #2456713 by larowlan, dawehner, jibran: Custom taxonomy field views handler needs to be replaced with generic Field API handler
parent fab76164
......@@ -91,13 +91,9 @@ views.field.term_link_edit:
type: label
label: 'Text to display'
views.field.taxonomy:
type: views_field
label: 'Taxonomy language'
views.field.term_name:
type: views.field.field
mapping:
link_to_taxonomy:
type: boolean
label: 'Link this field to its taxonomy term page'
convert_spaces:
type: boolean
label: 'Convert spaces in term names to hyphens'
......
<?php
/**
* @file
* Definition of Drupal\taxonomy\Plugin\views\field\Taxonomy.
*/
namespace Drupal\taxonomy\Plugin\views\field;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\area\Result;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\ResultRow;
use Drupal\views\ViewExecutable;
/**
* Field handler to provide simple renderer that allows linking to a taxonomy
* term.
*
* @todo This handler should use entities directly.
*
* @ingroup views_field_handlers
*
* @ViewsField("taxonomy")
*/
class Taxonomy extends FieldPluginBase {
/**
* Overrides Drupal\views\Plugin\views\field\FieldPluginBase::init().
*
* This method assumes the taxonomy_term_data table. If using another table,
* we'll need to be more specific.
*/
public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
parent::init($view, $display, $options);
$this->additional_fields['vid'] = 'vid';
$this->additional_fields['tid'] = 'tid';
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['link_to_taxonomy'] = array('default' => FALSE);
$options['convert_spaces'] = array('default' => FALSE);
return $options;
}
/**
* Provide link to taxonomy option
*/
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
$form['link_to_taxonomy'] = array(
'#title' => $this->t('Link this field to its taxonomy term page'),
'#description' => $this->t("Enable to override this field's links."),
'#type' => 'checkbox',
'#default_value' => !empty($this->options['link_to_taxonomy']),
);
$form['convert_spaces'] = array(
'#title' => $this->t('Convert spaces in term names to hyphens'),
'#description' => $this->t('This allows links to work with Views taxonomy term arguments.'),
'#type' => 'checkbox',
'#default_value' => !empty($this->options['convert_spaces']),
);
parent::buildOptionsForm($form, $form_state);
}
/**
* Prepares a link to the taxonomy.
*
* @param string $data
* The XSS safe string for the link text.
* @param \Drupal\views\ResultRow $values
* The values retrieved from a single row of a view's query result.
*
* @return string
* Returns a string for the link text.
*/
protected function renderLink($data, ResultRow $values) {
$term = $this->getEntity($values);
if (!empty($this->options['link_to_taxonomy']) && $term && $data !== NULL && $data !== '') {
$this->options['alter']['make_link'] = TRUE;
$this->options['alter']['url'] = $term->urlInfo();
}
if (!empty($this->options['convert_spaces'])) {
$data = str_replace(' ', '-', $data);
}
return $data;
}
/**
* {@inheritdoc}
*/
public function render(ResultRow $values) {
$value = $this->getValue($values);
return $this->renderLink($this->sanitizeValue($value), $values);
}
}
<?php
/**
* @file
* Contains \Drupal\taxonomy\Plugin\views\field\TermName.
*/
namespace Drupal\taxonomy\Plugin\views\field;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\field\Field;
use Drupal\views\ResultRow;
/**
* Displays taxonomy term names and allows converting spaces to hyphens.
*
* @ingroup views_field_handlers
*
* @ViewsField("term_name")
*/
class TermName extends Field {
/**
* {@inheritdoc}
*/
public function getItems(ResultRow $values) {
$items = parent::getItems($values);
if ($this->options['convert_spaces']) {
foreach ($items as &$item) {
// Replace spaces with hyphens.
$name = $item['raw']->get('value')->getValue();
$item['rendered']['#markup'] = str_replace(' ', '-', $name);
}
}
return $items;
}
/**
* {@inheritdoc}
*/
protected function defineOptions() {
$options = parent::defineOptions();
$options['convert_spaces'] = array('default' => FALSE);
return $options;
}
/**
* {@inheritdoc}
*/
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
$form['convert_spaces'] = array(
'#title' => $this->t('Convert spaces in term names to hyphens'),
'#type' => 'checkbox',
'#default_value' => !empty($this->options['convert_spaces']),
);
parent::buildOptionsForm($form, $form_state);
}
}
......@@ -50,8 +50,9 @@ protected function defaultDisplayOptions() {
$display_options['fields']['name']['alter']['html'] = 0;
$display_options['fields']['name']['hide_empty'] = 0;
$display_options['fields']['name']['empty_zero'] = 0;
$display_options['fields']['name']['link_to_taxonomy'] = 1;
$display_options['fields']['name']['plugin_id'] = 'taxonomy';
$display_options['fields']['name']['type'] = 'string';
$display_options['fields']['name']['settings']['link_to_entity'] = 1;
$display_options['fields']['name']['plugin_id'] = 'term_name';
return $display_options;
}
......
......@@ -94,7 +94,7 @@ public function getViewsData() {
);
}
$data['taxonomy_term_field_data']['name']['field']['id'] = 'taxonomy';
$data['taxonomy_term_field_data']['name']['field']['id'] = 'term_name';
$data['taxonomy_term_field_data']['name']['argument']['many to one'] = TRUE;
$data['taxonomy_term_field_data']['name']['argument']['empty field name'] = t('Uncategorized');
......
<?php
/**
* @file
* Contains \Drupal\taxonomy\Tests\Views\TaxonomyViewsFieldAccessTest.
*/
namespace Drupal\taxonomy\Tests\Views;
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\user\Entity\User;
use Drupal\views\Tests\Handler\FieldFieldAccessTestBase;
/**
* Tests base field access in Views for the taxonomy entity.
*
* @group taxonomy
*/
class TaxonomyViewsFieldAccessTest extends FieldFieldAccessTestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['taxonomy', 'text', 'entity_test'];
/**
* {@inheritdoc}
*/
protected function setUp($import_test_views = TRUE) {
parent::setUp($import_test_views);
$this->installEntitySchema('taxonomy_term');
}
/**
* Check access for taxonomy fields.
*/
public function testTermFields() {
$vocab = Vocabulary::create([
'vid' => 'random',
'name' => 'Randomness',
]);
$vocab->save();
$term1 = Term::create([
'name' => 'Semi random',
'vid' => $vocab->id(),
]);
$term1->save();
$term2 = Term::create([
'name' => 'Majorly random',
'vid' => $vocab->id(),
]);
$term2->save();
$term3 = Term::create([
'name' => 'Not really random',
'vid' => $vocab->id(),
]);
$term3->save();
$this->assertFieldAccess('taxonomy_term', 'name', 'Majorly random');
$this->assertFieldAccess('taxonomy_term', 'name', 'Semi random');
$this->assertFieldAccess('taxonomy_term', 'name', 'Not really random');
$this->assertFieldAccess('taxonomy_term', 'tid', $term1->id());
$this->assertFieldAccess('taxonomy_term', 'tid', $term2->id());
$this->assertFieldAccess('taxonomy_term', 'tid', $term3->id());
$this->assertFieldAccess('taxonomy_term', 'uuid', $term1->uuid());
$this->assertFieldAccess('taxonomy_term', 'uuid', $term2->uuid());
$this->assertFieldAccess('taxonomy_term', 'uuid', $term3->uuid());
}
}
<?php
/**
* @file
* Contains \Drupal\taxonomy\Tests\Views\TermNameFieldTest.
*/
namespace Drupal\taxonomy\Tests\Views;
use Drupal\views\Views;
/**
* Tests the term_name field handler.
*
* @group taxonomy
*
* @see \Drupal\taxonomy\Plugin\views\field\TermName
*/
class TermNameFieldTest extends TaxonomyTestBase {
/**
* {@inheritdoc}
*/
public static $testViews = array('test_taxonomy_term_name');
/**
* Tests term name field plugin functionality.
*/
public function testTermNameField() {
$this->term1->name->value = $this->randomMachineName() . ' ' . $this->randomMachineName();
$this->term1->save();
$user = $this->drupalCreateUser(['access content']);
$this->drupalLogin($user);
$view = Views::getView('test_taxonomy_term_name');
$view->initDisplay();
$this->executeView($view);
$this->assertEqual($this->term1->getName(), $view->getStyle()->getField(0, 'name'));
$this->assertEqual($this->term2->getName(), $view->getStyle()->getField(1, 'name'));
$view = Views::getView('test_taxonomy_term_name');
$display =& $view->storage->getDisplay('default');
$display['display_options']['fields']['name']['convert_spaces'] = TRUE;
$this->executeView($view);
$this->assertEqual(str_replace(' ', '-', $this->term1->getName()), $view->getStyle()->getField(0, 'name'));
$this->assertEqual($this->term2->getName(), $view->getStyle()->getField(1, 'name'));
}
}
......@@ -94,7 +94,6 @@ display:
html: false
hide_empty: false
empty_zero: false
link_to_taxonomy: true
relationship: none
group_type: group
admin_label: ''
......@@ -109,8 +108,10 @@ display:
element_default_classes: true
empty: ''
hide_alter_empty: true
convert_spaces: false
plugin_id: taxonomy
plugin_id: field
type: string
settings:
link_to_entity: true
entity_type: taxonomy_term
entity_field: name
filters: { }
......
......@@ -70,7 +70,6 @@ display:
html: false
hide_empty: false
empty_zero: false
link_to_taxonomy: true
relationship: none
group_type: group
admin_label: ''
......@@ -85,8 +84,10 @@ display:
element_default_classes: true
empty: ''
hide_alter_empty: true
convert_spaces: false
plugin_id: taxonomy
plugin_id: field
type: string
settings:
link_to_entity: true
entity_type: taxonomy_term
entity_field: name
filters:
......
......@@ -27,7 +27,11 @@ display:
field: name
id: name
table: taxonomy_term_field_data
plugin_id: taxonomy
plugin_id: term_name
type: string
settings:
link_to_entity: true
convert_spaces: false
entity_type: taxonomy_term
entity_field: name
nid:
......
......@@ -96,7 +96,6 @@ display:
html: false
hide_empty: false
empty_zero: false
link_to_taxonomy: true
relationship: none
group_type: group
admin_label: ''
......@@ -111,8 +110,10 @@ display:
element_default_classes: true
empty: ''
hide_alter_empty: true
convert_spaces: false
plugin_id: taxonomy
plugin_id: field
type: string
settings:
link_to_entity: true
entity_type: taxonomy_term
entity_field: name
filters: { }
......
langcode: en
status: true
dependencies:
module:
- taxonomy
- user
id: test_taxonomy_term_name
label: test_taxonomy_term_name
module: views
description: ''
tag: ''
base_table: taxonomy_term_field_data
base_field: tid
core: 8.x
display:
default:
display_plugin: default
id: default
display_title: Master
position: 0
display_options:
access:
type: perm
options:
perm: 'access content'
cache:
type: none
options: { }
query:
type: views_query
options:
disable_sql_rewrite: false
distinct: false
replica: false
query_comment: ''
query_tags: { }
exposed_form:
type: basic
options:
submit_button: Apply
reset_button: false
reset_button_label: Reset
exposed_sorts_label: 'Sort by'
expose_sort_order: true
sort_asc_label: Asc
sort_desc_label: Desc
pager:
type: full
options:
items_per_page: 10
offset: 0
id: 0
total_pages: null
expose:
items_per_page: false
items_per_page_label: 'Items per page'
items_per_page_options: '5, 10, 20, 40, 60'
items_per_page_options_all: false
items_per_page_options_all_label: '- All -'
offset: false
offset_label: Offset
tags:
previous: ' previous'
next: 'next ›'
first: '« first'
last: 'last »'
quantity: 9
style:
type: default
options:
grouping: { }
row_class: ''
default_row_class: true
uses_fields: false
row:
type: fields
options:
inline: { }
separator: ''
hide_empty: false
default_field_elements: true
fields:
name:
id: name
table: taxonomy_term_field_data
field: name
relationship: none
group_type: group
admin_label: ''
label: Name
exclude: false
alter:
alter_text: false
text: ''
make_link: false
path: ''
absolute: false
external: false
replace_spaces: false
path_case: none
trim_whitespace: false
alt: ''
rel: ''
link_class: ''
prefix: ''
suffix: ''
target: ''
nl2br: false
max_length: 0
word_boundary: true
ellipsis: true
more_link: false
more_link_text: ''
more_link_path: ''
strip_tags: false
trim: false
preserve_tags: ''
html: false
element_type: ''
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: true
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: true
empty: ''
hide_empty: false
empty_zero: false
hide_alter_empty: true
plugin_id: term_name
type: string
settings:
link_to_entity: false
convert_spaces: false
entity_type: taxonomy_term
entity_field: name
filters: { }
sorts:
tid:
id: tid
table: taxonomy_term_field_data
field: tid
relationship: none
group_type: group
admin_label: ''
order: ASC
exposed: false
expose:
label: ''
plugin_id: standard
entity_type: taxonomy_term
entity_field: tid
header: { }
footer: { }
empty: { }
relationships: { }
arguments: { }
......@@ -128,9 +128,10 @@ display:
hide_empty: false
empty_zero: false
hide_alter_empty: true
link_to_taxonomy: true
convert_spaces: false
plugin_id: taxonomy
plugin_id: field
type: string
settings:
link_to_entity: true
entity_type: taxonomy_term
entity_field: name
filters: { }
......
......@@ -27,7 +27,11 @@ display:
field: name
id: name
table: taxonomy_term_field_data
plugin_id: taxonomy
plugin_id: term_name
type: string
settings:
link_to_entity: true
convert_spaces: false
entity_type: taxonomy_term
entity_field: name
nid:
......
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