Support entity types that don't have link templates (like Paragraphs)

Created on 16 January 2025, about 2 months ago

Problem/Motivation

When the parent entity containing a field using Element Class Formatter does NOT implement the toUrl() a WSOD is displayed and internally a PHP Exception is thrown. Paragraphs do not implement toUrl() because they don't contain a canonical link URL.


TypeError: Drupal\Core\Link::fromTextAndUrl(): Argument #2 ($url) must be of type Drupal\Core\Url, string given, called in modules/contrib/element_class_formatter/src/Plugin/Field/FieldFormatter/WrapperClassFormatter.php on line 215 in Drupal\Core\Link::fromTextAndUrl() (line 93 of core/lib/Drupal/Core/Link.php).
Drupal\element_class_formatter\Plugin\Field\FieldFormatter\WrapperClassFormatter->viewElements(Object, 'en') (Line: 91)

Related generic error message:


Drupal\Core\Entity\Exception\UndefinedLinkTemplateException: Cannot generate default URL because no link template 'canonical' or 'edit-form' was found for the 'paragraph' entity type in Drupal\Core\Entity\EntityBase->toUrl() (line 211 of core/lib/Drupal/Core/Entity/EntityBase.php).

Proposed resolution

Check whether the parent entity will cause a WSOD when calling ->toUrl() before calling the method. This can be confirmed via $entity->hasLinkTemplate('canonical'). See #3238515-14: No link template 'canonical' found for the 'paragraph' entity type for additional context.

Remaining tasks

User interface changes

API changes

Data model changes

📌 Task
Status

Active

Version

2.1

Component

Code

Created by

🇪🇨Ecuador jwilson3

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

Merge Requests

Comments & Activities

Production build 0.71.5 2024