Hreflangs lead to an error in the node preview if the node has no ID (new node)

Created on 3 September 2024, 3 months ago

Problem/Motivation

If a node has a hreflang tag configured and a new node (never saved) is to be displayed in the preview, an error is triggered:

Drupal\Core\Entity\EntityMalformedException: The "node" entity cannot have a URI as it does not have an ID at Drupal\Core\Entity\EntityBase->toUrl() (Line 161 in /core/lib/Drupal/Core/Entity/EntityBase.php).

The error source comes from /metatag/metatag_hreflang/metatag_hreflang.tokens.inc (Line 124): Drupal\Core\Entity\EntityBase->toUrl('canonical')

Steps to reproduce

1. Activate the hreflang tags in the metatags settings for nodes.
2. Create a new node and click on the Preview button.

Proposed resolution

In several places in metatag_hreflang.tokens.inc there are calls to Entity::toUrl() e.g. $entity->toUrl('canonical') .

Since the new entity has no ID, Entity::toUrl() throws the EntityMalformedException.

One possible solution is to surround the toUrl() calls with a try-catch and return an empty value in the event of an error.

🐛 Bug report
Status

Active

Version

2.0

Component

Code

Created by

🇩🇪Germany J-Lee 🇩🇪🇪🇺

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

Comments & Activities

Production build 0.71.5 2024