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

Created on 16 March 2022, almost 3 years ago
Updated 14 August 2024, 4 months 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 2 days 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.

Production build 0.71.5 2024