Error: "The 'node' entity cannot have a URI as it does not have an ID" when using [node:url:relative] token

Created on 27 May 2021, over 3 years ago
Updated 9 October 2023, about 1 year ago

We're using our website as a decoupled Drupal, and the front-end needs various Metatag information including the canonical URL.
As the default token for the canonical url in the Metatag module is [node:url], the front-end receives the absolute path to the Node while - for obvious reasons - they prefer to have the relative path to the Node.

However, when I change the [node:url] token to [node:url:relative], this fatal error is thrown on creating the Node:

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

This seems to be triggered by this block of code:

    if (($url_tokens = \Drupal::token()->findWithPrefix($tokens, 'url'))) {
      $replacements += \Drupal::token()->generate('url', $url_tokens, ['url' => $node->toUrl()], $options, $bubbleable_metadata);
    }

which calls $node->toUrl() where the error is triggered due to $node not yet having an ID.

๐Ÿ› Bug report
Status

Postponed: needs info

Version

1.9

Component

Code

Created by

๐Ÿ‡ง๐Ÿ‡ชBelgium vodde83

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.

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia StanleyFernandes Goa

    consider this issue also to fix the error reported :
    https://www.drupal.org/project/yoast_seo/issues/3110455 ๐Ÿ› Metatags depending on URL cause errors for unsaved entities after updating to Drupal 8.8.0 Fixed

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States gcb

    @Berdir the core module layout_builder regularly renders content with sample entities in its UX. These sample entities do not have IDs. As a result, if you use a URI token anywhere on a default layout, the layout builder screen is crashed by this issue.

    The function \Drupal\Core\Entity\EntityBase::toUrl() begins with a check for a Null entity ID, and throws an exception if there's no ID. Passing an entity to this function without checking for an ID first is asking for whitescreens -- new, unsaved entities are valid for rendering according to Drupal standards. See the discussion in https://www.drupal.org/project/drupal/issues/3069001 ๐Ÿ› Layout builder's use of sample data can cause exceptions during rendering of default entity view displays Needs work about having contrib modules sensibly handling "preview" content.

  • Status changed to Needs review about 1 year ago
  • Open in Jenkins โ†’ Open on Drupal.org โ†’
    Core: 9.5.x + Environment: PHP 7.3 & MariaDB 10.3.22
    last update about 1 year ago
    77 pass
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States gcb
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States tim.plunkett Philadelphia
    +++ b/token.tokens.inc
    @@ -569,7 +569,7 @@ function token_tokens($type, array $tokens, array $data, array $options, Bubblea
    +    if (($url_tokens = \Drupal::token()->findWithPrefix($tokens, 'url')) && $node->id() !== NULL) {
    

    Could also use !empty($node->in_preview). That pre-dates LB (think original node preview) but is also set when LB generates the sample entity.

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia prashant.c Dharamshala

    Prashant.c โ†’ made their first commit to this issueโ€™s fork.

  • Merge request !52Added check for node id โ†’ (Open) created by prashant.c
  • Open in Jenkins โ†’ Open on Drupal.org โ†’
    Core: 9.5.x + Environment: PHP 7.3 & MariaDB 10.3.22
    last update about 1 year ago
    77 pass
  • Status changed to Needs work about 1 year ago
  • ๐Ÿ‡จ๐Ÿ‡ญSwitzerland berdir Switzerland

    I don't think in preview is the right thing to check here. you can also preview an existing, already saved node with an ID.

    We have an existing case that checks for isNew(), for the generic entity:url token, no strong preference betweenn isNew() and id(). I suppose id() could in edge cases be more reliable.

    Remaining tasks:
    * As mentioned earlier, node_tokens() in core has the same issue, aka the :url and :edit-url tokens, and a core issue should be opened to improve this.
    * Either add checks for other toUrl() calls in token replacements in this module or create a follow-up.
    * As usual, tests will make it more likely that this gets committed. Look for exsting tests for those tokens, copy paste with a new node, ensure it doesn't throw an error (assert for not replacing the token).

  • Status changed to RTBC 1 day ago
  • ๐Ÿ‡จ๐Ÿ‡ฆCanada joelpittet Vancouver

    We used MR 52 to explicitly fix this error in preview (usually I don't enable preview on nodes...)

Production build 0.71.5 2024