\Drupal\taxonomy\Plugin\views\field\TermName::getItems should work with links

Created on 13 September 2015, almost 9 years ago
Updated 29 March 2023, over 1 year ago

Follow-up to #2554755: BasicStringFormatter's nl2br() forces escaping *and* filtering β€” improve this β†’

Problem/Motivation

\Drupal\Core\Field\Plugin\Field\FieldFormatter\StringFormatter::viewElements() in case when link option checked builds different markup

      $view_value = $this->viewValue($item);
      if ($url) {
        $elements[$delta] = [
          '#type' => 'link',
          '#title' => $view_value,
          '#url' => $url,
        ];
      }

so views implementation should care about that case too

Steps to reproduce:
create a term entities basic view and try change following settings

render with convert_spaces option on

enable "Link to..." option

here should be hyphens displayed

Proposed resolution

check that \Drupal\taxonomy\Plugin\views\field\TermName::getItems checks link option
extend \Drupal\taxonomy\Tests\Views\TermNameFieldTest::testTermNameField() test

Remaining tasks

TBD

User interface changes

None.

API changes

None.

Data model changes

None.

πŸ› Bug report
Status

Fixed

Version

9.5

Component
TaxonomyΒ  β†’

Last updated 1 day ago

  • Maintained by
  • πŸ‡ΊπŸ‡ΈUnited States @xjm
  • πŸ‡¬πŸ‡§United Kingdom @catch
Created by

πŸ‡«πŸ‡·France andypost

Live updates comments and jobs are added and updated live.
  • VDC

    Related to the Views in Drupal Core initiative.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States dww

    Thanks for working on this!

    1. +++ b/core/modules/taxonomy/src/Plugin/views/field/TermName.php
      @@ -23,9 +23,10 @@ public function getItems(ResultRow $values) {
      -        // @todo Add link support https://www.drupal.org/node/2567745
      

      I grep'ed 10.1.x branch and confirmed this is the only reference to "2567745". πŸ‘

    2. +++ b/core/modules/taxonomy/src/Plugin/views/field/TermName.php
      @@ -23,9 +23,10 @@ public function getItems(ResultRow $values) {
      +        empty($this->options['settings']['link_to_entity']) ?
      +          $item['rendered']['#context']['value'] = $name :
      +          $item['rendered']['#title']['#context']['value'] = $name;
      

      I know @alexpott suggested this formulation. Personally, I don't love using ternary operators for conditional execution like this. My preference would be to use if (...) for this, since we're setting different variables based on the conditional. But that's just me. πŸ˜‚

    3. +++ b/core/modules/taxonomy/tests/src/Functional/Views/TermNameFieldTest.php
      @@ -48,6 +49,19 @@ public function testTermNameField() {
      +    $this->assertEquals($expected_link1->toString(), $view->getStyle()->getField(0, 'name'));
      

      #13 test-only failed right here, exactly as expected:

      1) Drupal\Tests\taxonomy\Functional\Views\TermNameFieldTest::testTermNameField
      Failed asserting that two strings are equal.
      --- Expected
      +++ Actual
      @@ @@
      -'oZZv7opl-u1O4twSP'
      +'oZZv7opl u1O4twSP'
      
      /var/www/html/vendor/phpunit/phpunit/src/Framework/Constraint/Equality/IsEqual.php:96
      /var/www/html/core/modules/taxonomy/tests/src/Functional/Views/TermNameFieldTest.php:63
      /var/www/html/vendor/phpunit/phpunit/src/Framework/TestResult.php:722
      
    • The fix looks good (personal preference in point 2 notwithstanding). πŸ˜‰
    • Test coverage is solid and the bot confirmed it's covering the bug.
    • The summary and title are accurate.
    • I can't see any reason not to commit this, so RTBC.

    Thanks again!
    -Derek

  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Random failure

  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Random failure

  • πŸ‡¦πŸ‡ΊAustralia larowlan πŸ‡¦πŸ‡ΊπŸ.au GMT+10

    Issue credits

    • larowlan β†’ committed 9f35abc0 on 10.0.x
      Issue #2567745 by mohit_aghera, smustgrave, quietone, andypost, dww,...
    • larowlan β†’ committed e3f208ee on 10.1.x
      Issue #2567745 by mohit_aghera, smustgrave, quietone, andypost, dww,...
    • larowlan β†’ committed e46f1e26 on 9.5.x
      Issue #2567745 by mohit_aghera, smustgrave, quietone, andypost, dww,...
  • Status changed to Fixed over 1 year ago
  • πŸ‡¦πŸ‡ΊAustralia larowlan πŸ‡¦πŸ‡ΊπŸ.au GMT+10

    Committed to 10.1.x and backported to 9.5.x and 10.0.x

    Thanks folks.

    I'm with @dww on that syntax, but not going to block the issue on that basis alone.

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

Production build 0.71.5 2024