diff --git a/core/modules/taxonomy/config/schema/taxonomy.views.schema.yml b/core/modules/taxonomy/config/schema/taxonomy.views.schema.yml index f4125ab7764f6c28946607b521d825b6f3509dbd..40b226a23742b55005c5dda1093f5e78eca18122 100644 --- a/core/modules/taxonomy/config/schema/taxonomy.views.schema.yml +++ b/core/modules/taxonomy/config/schema/taxonomy.views.schema.yml @@ -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' diff --git a/core/modules/taxonomy/src/Plugin/views/field/Taxonomy.php b/core/modules/taxonomy/src/Plugin/views/field/Taxonomy.php deleted file mode 100644 index 903f174f3f3b48257fb2a9180bb3c5cd6a145229..0000000000000000000000000000000000000000 --- a/core/modules/taxonomy/src/Plugin/views/field/Taxonomy.php +++ /dev/null @@ -1,102 +0,0 @@ -<?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); - } - -} diff --git a/core/modules/taxonomy/src/Plugin/views/field/TermName.php b/core/modules/taxonomy/src/Plugin/views/field/TermName.php new file mode 100644 index 0000000000000000000000000000000000000000..c2eba0f2a19f8d1121dc2f5b7b90d71b9876f35b --- /dev/null +++ b/core/modules/taxonomy/src/Plugin/views/field/TermName.php @@ -0,0 +1,60 @@ +<?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); + } + +} diff --git a/core/modules/taxonomy/src/Plugin/views/wizard/TaxonomyTerm.php b/core/modules/taxonomy/src/Plugin/views/wizard/TaxonomyTerm.php index 848ef1bb611a5d334491c7b293983cf16159c92d..c728c2c53d59ad5d228f44409519cf0949107687 100644 --- a/core/modules/taxonomy/src/Plugin/views/wizard/TaxonomyTerm.php +++ b/core/modules/taxonomy/src/Plugin/views/wizard/TaxonomyTerm.php @@ -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; } diff --git a/core/modules/taxonomy/src/TermViewsData.php b/core/modules/taxonomy/src/TermViewsData.php index d101f84ba0c13e0eec73ab3c27486ff0a3d5fbb3..1d3ba6f1af36e24a379f68b5013810a10b176c57 100644 --- a/core/modules/taxonomy/src/TermViewsData.php +++ b/core/modules/taxonomy/src/TermViewsData.php @@ -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'); diff --git a/core/modules/taxonomy/src/Tests/Views/TaxonomyViewsFieldAccessTest.php b/core/modules/taxonomy/src/Tests/Views/TaxonomyViewsFieldAccessTest.php new file mode 100644 index 0000000000000000000000000000000000000000..c16b10a789dd0e46cb17ae3f7588b942c4cb4a41 --- /dev/null +++ b/core/modules/taxonomy/src/Tests/Views/TaxonomyViewsFieldAccessTest.php @@ -0,0 +1,74 @@ +<?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()); + } + +} diff --git a/core/modules/taxonomy/src/Tests/Views/TermNameFieldTest.php b/core/modules/taxonomy/src/Tests/Views/TermNameFieldTest.php new file mode 100644 index 0000000000000000000000000000000000000000..6a2d65c509d311b46eeb1631c477ebef91cbaf24 --- /dev/null +++ b/core/modules/taxonomy/src/Tests/Views/TermNameFieldTest.php @@ -0,0 +1,54 @@ +<?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')); + + } + +} diff --git a/core/modules/taxonomy/tests/modules/taxonomy_test_views/test_views/views.view.taxonomy_default_argument_test.yml b/core/modules/taxonomy/tests/modules/taxonomy_test_views/test_views/views.view.taxonomy_default_argument_test.yml index 5842b342c5392f31305bf3e9b210d5a0a12eef7d..dcb0bc4901be0ee97de3ca74875d06d2dcaca395 100644 --- a/core/modules/taxonomy/tests/modules/taxonomy_test_views/test_views/views.view.taxonomy_default_argument_test.yml +++ b/core/modules/taxonomy/tests/modules/taxonomy_test_views/test_views/views.view.taxonomy_default_argument_test.yml @@ -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: { } diff --git a/core/modules/taxonomy/tests/modules/taxonomy_test_views/test_views/views.view.test_field_filters.yml b/core/modules/taxonomy/tests/modules/taxonomy_test_views/test_views/views.view.test_field_filters.yml index ccde243b9d6930be6be5b7593256d8f708aa3f7f..baa59ff76c9512cdb38b4ab7278f91539e638829 100644 --- a/core/modules/taxonomy/tests/modules/taxonomy_test_views/test_views/views.view.test_field_filters.yml +++ b/core/modules/taxonomy/tests/modules/taxonomy_test_views/test_views/views.view.test_field_filters.yml @@ -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: diff --git a/core/modules/taxonomy/tests/modules/taxonomy_test_views/test_views/views.view.test_groupwise_term.yml b/core/modules/taxonomy/tests/modules/taxonomy_test_views/test_views/views.view.test_groupwise_term.yml index c5ba611dfdf4fc6c2745f8dbc0125945aaacdba2..c4e18a00c79b01fd1c7365e1e0ec354a9a389c6e 100644 --- a/core/modules/taxonomy/tests/modules/taxonomy_test_views/test_views/views.view.test_groupwise_term.yml +++ b/core/modules/taxonomy/tests/modules/taxonomy_test_views/test_views/views.view.test_groupwise_term.yml @@ -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: diff --git a/core/modules/taxonomy/tests/modules/taxonomy_test_views/test_views/views.view.test_taxonomy_parent.yml b/core/modules/taxonomy/tests/modules/taxonomy_test_views/test_views/views.view.test_taxonomy_parent.yml index d2c87b380e201803b0ad77a112e4dc0c68d84c34..eb6d2987632ea15d6ba62b92c542214c358d0e47 100644 --- a/core/modules/taxonomy/tests/modules/taxonomy_test_views/test_views/views.view.test_taxonomy_parent.yml +++ b/core/modules/taxonomy/tests/modules/taxonomy_test_views/test_views/views.view.test_taxonomy_parent.yml @@ -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: { } diff --git a/core/modules/taxonomy/tests/modules/taxonomy_test_views/test_views/views.view.test_taxonomy_term_name.yml b/core/modules/taxonomy/tests/modules/taxonomy_test_views/test_views/views.view.test_taxonomy_term_name.yml new file mode 100644 index 0000000000000000000000000000000000000000..6fae1306d10bf0db81394539d8e436e26649101f --- /dev/null +++ b/core/modules/taxonomy/tests/modules/taxonomy_test_views/test_views/views.view.test_taxonomy_term_name.yml @@ -0,0 +1,158 @@ +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: { } diff --git a/core/modules/taxonomy/tests/modules/taxonomy_test_views/test_views/views.view.test_taxonomy_tid_field.yml b/core/modules/taxonomy/tests/modules/taxonomy_test_views/test_views/views.view.test_taxonomy_tid_field.yml index 1d7562a37348e49934a3ca6455b6fbd62dd79f1a..6fab8865a72a5ba4ccfc0fbaae508b10e220650c 100644 --- a/core/modules/taxonomy/tests/modules/taxonomy_test_views/test_views/views.view.test_taxonomy_tid_field.yml +++ b/core/modules/taxonomy/tests/modules/taxonomy_test_views/test_views/views.view.test_taxonomy_tid_field.yml @@ -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: { } diff --git a/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_groupwise_term_ui.yml b/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_groupwise_term_ui.yml index 4dcb99b23abb2bf9001c40957a14417bc65ed475..cfff08eb4fa3f55ee79d1d4447c868e2347af570 100644 --- a/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_groupwise_term_ui.yml +++ b/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_groupwise_term_ui.yml @@ -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: