StringFormatter shows link for entities that might not have a canonical URL

Created on 16 March 2022, over 2 years ago
Updated 14 August 2024, 25 days ago

Problem/Motivation

The Drupal\Core\Field\Plugin\Field\FieldFormatter\StringFormatter checks in its ::settingsForm method on the entity type, whether it has a canonical link template. That is correct.

However, it also checks for canonical link template existence within ::viewElements on the entity type. Since single entity instance are allowed to define whether they have a canonical link template on their own via EntityInterface::hasLinkTemplate, the StringFormatter should respect that accordingly.

Steps to reproduce

1. Generate new entity:content
drush generate entity:content

Module machine name:
 ➀ custom_entity
 Module name [Custom entity]:
 ➀ 
 Entity type label [Custom entity]:
 ➀ 
 Entity type ID [custom_entity]:
 ➀ 
 Entity class [CustomEntity]:
 ➀ 
 Entity base path [/custom-entity]:
 ➀ 
 Make the entity type fieldable? [Yes]:
 ➀ 
 Make the entity type revisionable? [No]:
 ➀ 
 Make the entity type translatable? [No]:
 ➀ 
 The entity type has bundle? [No]:
 ➀ 
 Create canonical page? [Yes]:
 ➀ 
 Create entity template? [Yes]:
 ➀ 
 Create CRUD permissions? [No]:
 ➀ 
 Add "label" base field? [Yes]:
 ➀ 
 Add "status" base field? [Yes]:
 ➀ 
 Add "created" base field? [Yes]:
 ➀ 
 Add "changed" base field? [Yes]:
 ➀ 
 Add "author" base field? [Yes]:
 ➀ 
 Add "description" base field? [Yes]:
 ➀ 
 Create REST configuration for the entity? [No]:
 ➀ 
 The following directories and files have been created or updated:
–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
 β€’ /var/www/html/web/modules/custom_entity/custom_entity.info.yml
 β€’ /var/www/html/web/modules/custom_entity/custom_entity.links.action.yml
 β€’ /var/www/html/web/modules/custom_entity/custom_entity.links.contextual.yml
 β€’ /var/www/html/web/modules/custom_entity/custom_entity.links.menu.yml
 β€’ /var/www/html/web/modules/custom_entity/custom_entity.links.task.yml
 β€’ /var/www/html/web/modules/custom_entity/custom_entity.module
 β€’ /var/www/html/web/modules/custom_entity/custom_entity.permissions.yml
 β€’ /var/www/html/web/modules/custom_entity/custom_entity.routing.yml
 β€’ /var/www/html/web/modules/custom_entity/config/install/system.action.custom_entity_delete_action.yml
 β€’ /var/www/html/web/modules/custom_entity/config/install/system.action.custom_entity_save_action.yml
 β€’ /var/www/html/web/modules/custom_entity/src/CustomEntityInterface.php
 β€’ /var/www/html/web/modules/custom_entity/src/CustomEntityListBuilder.php
 β€’ /var/www/html/web/modules/custom_entity/src/Entity/CustomEntity.php
 β€’ /var/www/html/web/modules/custom_entity/src/Form/CustomEntityForm.php
 β€’ /var/www/html/web/modules/custom_entity/src/Form/CustomEntitySettingsForm.php
 β€’ /var/www/html/web/modules/custom_entity/templates/custom-entity.html.twig

2. Edit custom_entity/src/Entity/CustomEntity.php file adding inside the CustomEntity class the following function

/**
* {@inheritdoc}
*/
public function hasLinkTemplate($rel): bool {
  if ($rel === 'canonical') {
    return FALSE;
  }
  return parent::hasLinkTemplate($rel);
}

3. Edit custom_entity.info.yml to change `core_version_requirement: ^10 || ^11`

4. Enable module custom_entity
drush en custom_entity

5. Manage custom_entity display (/admin/structure/custom-entity/display) define "Label" field format to "Plain text" with Link to the Custom entity

6. Add new content custom_entity (/custom-entity/add) and view the content /custom-entity/1
The field label is displayed has a link whereas class CustomEntity define hasLinkTemplate('canonical') to return FALSE.
The patch correcting this comportment to display field label without link.

Proposed resolution

Replace the check on entity type level within StringFormatter::viewElements by a check on the entity itself.
Meaning changing this line

if ($this->getSetting('link_to_entity') && !$entity->isNew() && $entity_type->hasLinkTemplate('canonical')) {
to
if ($this->getSetting('link_to_entity') && !$entity->isNew() && $entity->hasLinkTemplate('canonical')) {

Remaining tasks

User interface changes

NA

API changes

NA

Data model changes

NA

Release notes snippet

NA

πŸ› Bug report
Status

Needs work

Version

11.0 πŸ”₯

Component
FieldΒ  β†’

Last updated 1 day ago

Created by

πŸ‡©πŸ‡ͺGermany mxh Offenburg

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

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

  • last update about 1 year ago
    Custom Commands Failed
  • πŸ‡«πŸ‡·France franck_lorancy

    @mxh I tried to follow your recommendations.
    Can you take a look and tell me if any adjustments are needed?
    Thank you in advance.

  • πŸ‡©πŸ‡ͺGermany tstoeckler Essen, Germany
  • πŸ‡©πŸ‡ͺGermany tstoeckler Essen, Germany

    Read the patch now and I guess the issues aren't really strictly related. Sorry for the noise.

  • Status changed to Needs work about 1 year ago
  • The Needs Review Queue Bot β†’ tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

    This does not mean that the patch needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

    Consult the Drupal Contributor Guide β†’ to find step-by-step guides for working with issues.

  • πŸ‡«πŸ‡·France franck_lorancy

    Fix Needs Review Queue Bot

  • Status changed to Needs review about 1 year ago
  • last update about 1 year ago
    30,100 pass
  • Status changed to Needs work about 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Ran testLinkEntitiesWithCanonical without the fix and it still passes. So not sure if it's covering the bug. Leaving tests tag for that.

  • πŸ‡«πŸ‡·France franck_lorancy

    @Thanks for the review @smustgrave.
    The goal of the issue is to replace the check on the entity_type ($entity_type->hasLinkTemplate('canonical')) inside StringFormatter::viewElements by a check on the entity itself ($entity->hasLinkTemplate('canonical')).
    The test "testLinkEntitiesWithCanonical" passes because the return of StringFormatter::viewElements stilling to be the same with and without the fix.
    Do you think it is necessary to add a new test to illustrate the check replacement on the entity_type by on the entity itself ?

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

    Typically for bugs we have a test case to show the issue. There are a few times where that’s not needed. This instance I can’t say, but leaning towards a test

  • Status changed to Needs review 7 months ago
  • πŸ‡«πŸ‡·France franck_lorancy

    @smustgrave the test case to show the issue is testLinkEntitiesWithoutCanonical.
    The following lines check render don't contains link when an entity without canonical is displayed with link to entity :

    // Test a link is not being generated for entities without canonical URLs
    // when link_to_entity is set to TRUE.
    $this->renderEntityFields($entity_without_canonical, $display_with_link);
    $this->assertNoLink($value);
    $this->assertNoLinkByHref($entity_without_canonical->toUrl()->toString());
    

    The 2 asserts failed when we keeping original core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/StringFormatter.php.
    The 2 asserts succeed when we apply patch into core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/StringFormatter.php.

    The testLinkEntitiesWithCanonical which passes with or without the fix can be removed, @mxh proposed to write it (#12).
    Say me if we keep or remove it. Thank you in advance.

  • Status changed to Needs work 7 months ago
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Removing tests tag as they appear to be added

    Can steps to reproduce be added to the issue summary. Put NA on sections I believe may not apply to this ticket.

    Recommend turning patch to MR for reviews and hiding all patches

    New functions should be typehinted example testLinkEntitiesWithCanonical(): void

  • Pipeline finished with Failed
    7 months ago
    Total: 162s
    #94778
  • Pipeline finished with Success
    7 months ago
    Total: 555s
    #94786
  • Pipeline finished with Success
    7 months ago
    Total: 566s
    #94805
  • Status changed to Needs review 7 months ago
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Is the MR good to review? See it's marked draft.

  • πŸ‡«πŸ‡·France franck_lorancy

    Yes, the MR is good to review.

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

    smustgrave β†’ changed the visibility of the branch 9.3.x to hidden.

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

    smustgrave β†’ changed the visibility of the branch 3269889-stringformatter-shows-link to hidden.

  • Status changed to Needs work 6 months ago
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Hiding old MRs for clarity.

    Ran the test-only feature https://git.drupalcode.org/issue/drupal-3269889/-/jobs/815018 unfortunately it passes when it should of failed.

  • πŸ‡«πŸ‡·France franck_lorancy

    I'm not sure Reverting core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/StringFormatter.php works, I read the following error :
    fatal: invalid reference: refs/heads/11.x

  • Pipeline finished with Success
    6 months ago
    Total: 1488s
    #113660
  • Pipeline finished with Success
    6 months ago
    Total: 735s
    #113757
  • πŸ‡«πŸ‡·France franck_lorancy

    I add the branch 11.x to the fork but now the 4 tests fails with the same error, even the tests elready present which have not been modified.

    Drupal\Tests\field\Kernel\KernelString\StringFormatterTest::testStringFormatter
    Drupal\Component\Plugin\Exception\PluginNotFoundException: The "string" plugin does not exist. Valid plugin IDs for Drupal\Core\Field\FormatterPluginManager are: text_trimmed, text_summary_or_trimmed, text_default, entity_reference_custom_cache_tag, user_name, author, uri_link, timestamp, timestamp_ago, number_unformatted, email_mailto, language, number_integer, entity_reference_label, entity_reference_entity_id, entity_reference_entity_view, number_decimal, boolean, basic_string

    https://git.drupalcode.org/issue/drupal-3269889/-/jobs/1013107

    I confirm, tests are ok with phpUnit on my locale.

  • Status changed to Needs review 6 months ago
  • Status changed to Needs work 6 months ago
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Some feedback on the MR.

  • Status changed to Needs review 2 months ago
  • Pipeline finished with Failed
    2 months ago
    Total: 977s
    #209623
  • Status changed to Needs work 2 months ago
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Did not re-review yet but MR appears to have failures.

  • πŸ‡­πŸ‡ΊHungary mxr576 Hungary

    Not just canonical url support is problematic, but the lack of url_callback support as well, see πŸ› StringFormatter does not support entity uri_callback Active .

Production build 0.71.5 2024