Problem/Motivation

On #2393339: [META] Make sure Views base fields are using Field API for formatting, and do not lose functionality, we are updating all base entity fields in entity views data so that they are using Field API for formatting rather than using generic Views handlers.

This issue is about the Taxonomy module handler 'taxonomy', which is used in taxonomy_term_field_data.name

Proposed resolution

Change this field to use the Field API formatter 'field' instead of the custom formatter. Should also be able to remove the custom formatter from the code base completely.

Remaining tasks

Make a patch.

User interface changes

None.

API changes

Not really.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

larowlan’s picture

Assigned: Unassigned » larowlan

Giddyup

larowlan’s picture

Assigned: larowlan » Unassigned
Issue tags: +Needs change record
FileSize
6.03 KB

changed definition and updated test/default views
This alters the default taxonomy view, so will need a change record for those running d8 sites.

larowlan’s picture

Status: Active » Needs review
FileSize
9.38 KB

and deletes the plugin

Status: Needs review » Needs work

The last submitted patch, 3: taxonomy-term-name-views.2.patch, failed testing.

larowlan’s picture

Status: Needs work » Needs review
FileSize
4.33 KB
10.98 KB

Schema issues
We have existing tests which use this field

Status: Needs review » Needs work

The last submitted patch, 5: taxonomy-term-name-views.3.patch, failed testing.

jhodgdon’s picture

As in #2455131: Field comment_field_data.field_name should be using Field API formatter, the right fix for TaxonomyViewsData:

-    $data['taxonomy_term_field_data']['name']['field']['id'] = 'taxonomy';
+    $data['taxonomy_term_field_data']['name']['field']['id'] = 'field';
+    $data['taxonomy_term_field_data']['name']['field']['field_name'] = 'name';

should actually be just to remove that first line entirely, so that the Taxonomy module doesn't override the Views default EntityViewsData (which uses the 'field' plugin for any string fields).

Then in the views configs, where they have:

          link_to_taxonomy: true

you'll need to replace this with the equivalent from the generic field handler. Here's an example of how this was done in the patch for #2342045: Standard views base fields need to use same rendering as Field UI fields, for formatting, access checking, and translation consistency when it took care of the node title fields:

+++ b/core/modules/node/config/install/views.view.content.yml
@@ -168,10 +168,12 @@ display:
           hide_empty: false
           empty_zero: false
           hide_alter_empty: true
-          link_to_node: true
-          plugin_id: node
           entity_type: node
           entity_field: title
+          type: string
+          settings:
+            link_to_entity: true
+          plugin_id: field
dawehner’s picture

  1. +++ b/core/modules/taxonomy/src/TermViewsData.php
    @@ -94,7 +94,8 @@ public function getViewsData() {
    +    $data['taxonomy_term_field_data']['name']['field']['field_name'] = 'name';
    

    We don't need this line.

  2. +++ b/core/modules/taxonomy/tests/modules/taxonomy_test_views/test_views/views.view.taxonomy_default_argument_test.yml
    @@ -94,7 +94,6 @@ display:
               hide_empty: false
               empty_zero: false
    -          link_to_taxonomy: true
               relationship: none
               group_type: group
               admin_label: ''
    

    ... Do we link the name by default? I don't think so.

  3. +++ b/core/modules/taxonomy/config/schema/taxonomy.views.schema.yml
    @@ -91,17 +91,6 @@ views.field.term_link_edit:
    -    convert_spaces:
    -      type: boolean
    -      label: 'Convert spaces in term names to hyphens'
    -
    

    What do we do about this?

larowlan’s picture

Status: Needs work » Needs review
FileSize
6.08 KB
13.33 KB

Fixes for #7 and #8

Status: Needs review » Needs work

The last submitted patch, 9: taxonomy-term-name-views.4.patch, failed testing.

larowlan’s picture

Status: Needs work » Needs review
FileSize
851 bytes
14.17 KB

wizard changes

Status: Needs review » Needs work

The last submitted patch, 11: taxonomy-term-name-views.5.patch, failed testing.

larowlan’s picture

Status: Needs work » Needs review
FileSize
1.23 KB
14.24 KB

schema/wizard changes

jhodgdon’s picture

Status: Needs review » Needs work

Tests passed!

A few questions on the latest patch:

a) This bit of schema looks wrong (I realize it came from somewhere else, but...):

+++ b/core/modules/taxonomy/config/schema/taxonomy.views.schema.yml

+views.field.term_name:
+  type: views.field.field
+  label: 'Taxonomy language'

Why is a field for the term name labeled "Taxonomy Language"?!?

b) This is a bit wonky in the patch:

++ b/core/modules/taxonomy/src/TermViewsData.php
@@ -32,6 +32,7 @@ public function getViewsData() {
       ),
     );
 
+    $data['taxonomy_term_field_data']['name']['field']['id'] = 'term_name';
     $data['taxonomy_term_field_data']['tid']['help'] = t('The tid of a taxonomy term.');
 
     $data['taxonomy_term_field_data']['tid']['argument']['id'] = 'taxonomy';
@@ -94,7 +95,6 @@ public function getViewsData() {
       );
     }
 
-    $data['taxonomy_term_field_data']['name']['field']['id'] = 'taxonomy';
     $data['taxonomy_term_field_data']['name']['argument']['many to one'] = TRUE;
     $data['taxonomy_term_field_data']['name']['argument']['empty field name'] = t('Uncategorized');

I think the added line should be put where the removed line was, not up with the 'tid' field?

c)

+++ b/core/modules/taxonomy/tests/modules/taxonomy_test_views/test_views/views.view.test_groupwise_term.yml
@@ -27,7 +27,7 @@ display:
           field: name
           id: name
           table: taxonomy_term_field_data
-          plugin_id: taxonomy
+          plugin_id: field

In the other views configs, there were more lines added than just changing the plugin ID, such as:

-          convert_spaces: false
-          plugin_id: taxonomy
+          plugin_id: field
+          type: string
+          settings:
+            link_to_entity: true

The link_to_entity setting is probably not needed, but the type: string seems like it might be?

This is also possibly a problem in:
+++ b/core/modules/taxonomy/tests/modules/taxonomy_test_views/test_views/views.view.test_taxonomy_node_term_data.yml
(two places)
+++ b/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_groupwise_term_ui.yml

Other than that, looks good to me!

larowlan’s picture

Status: Needs work » Needs review
FileSize
3.94 KB
13.15 KB

Fixes #14 note that the changes in views.view.test_taxonomy_node_term_data.yml were wrong, that was an argument plugin, so reverted.

dawehner’s picture

Status: Needs review » Needs work
  1. +++ b/core/modules/taxonomy/src/Plugin/views/field/TermName.php
    @@ -0,0 +1,56 @@
    +        if (!empty($result->{$this->aliases['name']})) {
    

    I'm pretty convinced that this line doesn't have any test coverage. Nothing is adding this alias if I see that correctly in this file. If you want to the field alias of the current file, use $this->field_alias

  2. +++ b/core/modules/taxonomy/src/Plugin/views/field/TermName.php
    @@ -0,0 +1,56 @@
    +          $result->{$this->aliases['name']} = str_replace(' ', '-', $result->{$this->aliases['nid']});
    

    huch, we replace the values of the name with the nid?

larowlan’s picture

Status: Needs work » Needs review
Issue tags: +Needs tests
FileSize
802 bytes
13.13 KB

I'm pretty convinced that this line doesn't have any test coverage.

Given point 2, I think we can say 110% there are not tests ;)

can't see myself having time for tests today, but will come back for them

dawehner’s picture

Given point 2, I think we can say 110% there are not tests ;)

We need meta tests!

jhodgdon’s picture

Status: Needs review » Needs work

Patch looks clean now, thanks! I guess all that's remaining is tests? Setting back to needs work for that.

And... side note... on the other related/similar issues, should we be removing some schema when we remove plugins? Do we need to go back to the recently-fixed issues and do that?

dawehner’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests
FileSize
16.16 KB
6.78 KB

Well, as this test clearly shows, it doens't work.

Note: $result->... is not used anymore, so the entire implementation doesn't work :)

Status: Needs review » Needs work

The last submitted patch, 20: 2456713-20.patch, failed testing.

larowlan’s picture

Does it work in HEAD? if not should we be splitting the convert spaces functionality into a non-critical followup?

dawehner’s picture

It works in HEAD, because there the field is loaded as part of the query. Once you use the entity API field, you no longer do.

effulgentsia’s picture

Issue tags: +Critical Office Hours

Per #2393339-57: [META] Make sure Views base fields are using Field API for formatting, and do not lose functionality, tagging for critical office hours, but if there's a reason to not have this particular child issue in that list, please untag it.

dawehner’s picture

+++ b/core/modules/taxonomy/src/Plugin/views/field/TermName.php
@@ -0,0 +1,56 @@
+   * {@inheritdoc}
+   */
+  public function preRender(&$values) {
+    if ($this->options['convert_spaces']) {
+      foreach ($values as $result) {
+        if (!empty($result->{$this->field_alias})) {
+          $result->{$this->field_alias} = str_replace(' ', '-', $result->{$this->field_alias});
+        }
+      }
+    }
+  }

So what you can do is to override ::getItems, see https://www.drupal.org/files/issues/interdiff_11573.txt

larowlan’s picture

Assigned: Unassigned » larowlan

will poke at this later today

larowlan’s picture

Status: Needs work » Needs review
FileSize
2.24 KB
16.34 KB

So this works, but the test still fails, I think the way the display is changed doesn't stick
Any ideas?

$this->options['convert_spaces'] is always false in the test.

dawehner’s picture

FileSize
833 bytes
16.3 KB

This should be it, feel free to upload a patch.

The last submitted patch, 27: taxonomy-term-name-views.25.patch, failed testing.

larowlan’s picture

looks ready to me, thanks @dawehner

dawehner’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

We have to add a TaxonomyTermViewsFieldAccessTest but it should be pretty easy to do, given that the parent already does all the hard logic.

jibran’s picture

Overall +1 for the patch.

+++ b/core/modules/taxonomy/src/Plugin/views/field/TermName.php
@@ -0,0 +1,60 @@
+ * A field that displays the taxonomy term name.

It would be great if we could improve this in next reroll.

xjm’s picture

Issue tags: +Triaged D8 critical

Discussed with @webchick, @effulgentsia, and @alexpott. We agreed that this issue is critical because Views should respect access restrictions for taxonomy term field content and there are common usecases for this.

Also +1 for #31; thanks @dawehner!

larowlan’s picture

working on test

larowlan’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests
FileSize
1.86 KB
18.15 KB

And a test

jibran’s picture

Status: Needs review » Needs work

Awesome this is ready. Just few minor things.

  1. +++ b/core/modules/taxonomy/config/schema/taxonomy.views.schema.yml
    @@ -91,13 +91,9 @@ views.field.term_link_edit:
    +views.field.term_name:
    +  type: views.field.field
       mapping:
    ...
         convert_spaces:
           type: boolean
           label: 'Convert spaces in term names to hyphens'
    
    @@ -169,3 +165,11 @@ views.relationship.node_term_data:
    +
    +views.field.term_name:
    +  type: views.field.field
    +  label: 'Term name'
    +  mapping:
    +    convert_spaces:
    +      type: boolean
    +      label: 'Convert spaces in term names to hyphens'
    

    Why we have this twice?

  2. +++ b/core/modules/taxonomy/src/Plugin/views/field/TermName.php
    @@ -0,0 +1,60 @@
    + * A field that displays the taxonomy term name.
    

    Can we please add some description explaining the use case and all that?

  3. +++ b/core/modules/taxonomy/tests/modules/taxonomy_test_views/test_views/views.view.test_groupwise_term.yml
    @@ -27,7 +27,10 @@ display:
    +            link_to_entity: true
    
    +++ b/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_groupwise_term_ui.yml
    @@ -27,7 +27,10 @@ display:
    +            link_to_entity: true
    

    There is no convert_spaces setting some views export gone wrong maybe or manual edit? I think we have to re-export these views.

  4. +++ b/core/modules/taxonomy/tests/modules/taxonomy_test_views/test_views/views.view.test_taxonomy_term_name.yml
    @@ -128,9 +128,11 @@ display:
               convert_spaces: false
    

    Shouldn't this be under settings now after this change?

dawehner’s picture

+++ b/core/modules/taxonomy/src/Tests/Views/TaxonomyViewsFieldAccessTest.php
@@ -0,0 +1,68 @@
+
+    $this->assertFieldAccess('taxonomy_term', 'name', 'Majorly random');
+    $this->assertFieldAccess('taxonomy_term', 'name', 'Semi random');
+    $this->assertFieldAccess('taxonomy_term', 'name', 'Not really random');

Can you expand the test coverage with ID, UUID and what not, please?

larowlan’s picture

Assigned: larowlan » Unassigned
Status: Needs work » Needs review
Issue tags: -Needs change record
FileSize
3.46 KB
18.1 KB

No need for a change record here, functionality is equivalent.
Fixes #36 and #37

Re #36.4 - no its part of the plugin options, not the formatter settings.

jibran’s picture

Status: Needs review » Reviewed & tested by the community

Thanks for the fixes.

jibran’s picture

Thanks for the fixes.

larowlan’s picture

Thanks

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 38: comment-fields-2456705.24.patch, failed testing.

The last submitted patch, 2: taxonomy-term-name-views.1.patch, failed testing.

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 38: comment-fields-2456705.24.patch, failed testing.

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 38: comment-fields-2456705.24.patch, failed testing.

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 38: comment-fields-2456705.24.patch, failed testing.

larowlan’s picture

dawehner’s picture

+++ b/core/modules/comment/config/schema/comment.views.schema.yml
@@ -4,17 +4,6 @@ views.argument.argument_comment_user_uid:
-views.field.comment:
-  type: views_field
-  label: 'Comment'
-  mapping:
-    link_to_comment:
-      type: boolean
-      label: 'Link this field to its comment'
-    link_to_entity:
-      type: boolean
-      label: 'Link field to the entity if there is no comment'
-

+++ b/core/modules/comment/src/CommentViewsData.php
@@ -27,9 +27,6 @@ public function getViewsData() {
     $data['comment_field_data']['subject']['help'] = t('The title of the comment.');
-    $data['comment_field_data']['subject']['field']['id'] = 'comment';
-
-    $data['comment_field_data']['cid']['field']['id'] = 'comment';
 
     $data['comment_field_data']['name']['title'] = t('Author');

I have no idea why those changes landed as part of that patch ... you've uploaded the wrong patch :)

jibran’s picture

Status: Needs work » Reviewed & tested by the community
FileSize
18.36 KB

Now with correct patch.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 53: custom_taxonomy_field-2456713-53.patch, failed testing.

larowlan’s picture

Status: Needs work » Needs review
FileSize
18.36 KB

larowlan--

Status: Needs review » Needs work

The last submitted patch, 55: taxonomy-term-name-views-stuff-2456713.38.patch, failed testing.

larowlan’s picture

Status: Needs work » Needs review
FileSize
1.4 KB
18.35 KB

whoops

larowlan’s picture

well that was the long way round, but we're back :)
can't believe I opened a 'random test fails' issue and it turned out I'd picked the wrong file in the 'browse' window.
pebkac

jibran’s picture

Status: Needs review » Reviewed & tested by the community

And 20 comments later we are back to RTBC.

Moral: Never RTBC an issue based only on interdiff. :P

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

This issue addresses a critical bug and is allowed per https://www.drupal.org/core/beta-changes. Committed 418497e and pushed to 8.0.x. Thanks!

  • alexpott committed 418497e on 8.0.x
    Issue #2456713 by larowlan, dawehner, jibran: Custom taxonomy field...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.