Problem/Motivation

When viewing a blog node (node.blog.full) that does not have a value in field_featured_image, the page crashes with a Twig RuntimeError.

Full error

Twig\Error\RuntimeError occurred during rendering of component
14f8bf3e-e882-48fb-ba4e-83e932f0748b in Content template
Blog post content items — Full content view (node.blog.full):
An exception has been thrown during the rendering of a template
("[canvas:image/src] NULL value found, but a string or an object is required...
[canvas:image/alt] NULL value found, but a string or an object is required...
[canvas:image/width] NULL value found, but an integer or an object is required.
[canvas:image/height] NULL value found, but an integer or an object is required.")
in "canvas:image" at line 1.

Root cause

The issue is in components/hero-blog/hero-blog.twig. The template unconditionally includes the canvas:image component, passing media.src, media.alt, media.width, and media.height. When the blog node has no featured image, these values are NULL, and the canvas:image component's typed props reject NULL, causing the Twig render error.

Steps to reproduce

  1. Install Drupal CMS 2.0.x with the default Canvas content templates.
  2. Create a new Blog post.
  3. Fill in only the Title and Description (leave "Featured image" empty).
  4. Save and view the blog post.
  5. Observe the Twig RuntimeError.

Proposed resolution

Guard the canvas:image include with an {% if media.src %} check so the image only renders when a featured image is set. Also add |default('') on alt as a safety net.

Remaining tasks

  • Review patch
  • Commit

User interface changes

None when a featured image is present. When no featured image is set, the hero area renders without an image instead of crashing.

API changes

None.

Data model changes

None.

CommentFileSizeAuthor
byte_theme-hero-blog-optional-image.patch961 byteskruser

Issue fork byte_theme-3583766

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

kruser created an issue. See original summary.

pameeela’s picture

Status: Active » Needs work

@kruser thanks for the patch! Could you convert it to a merge request so we can commit the fix?

kruser’s picture

Status: Needs work » Needs review

MR !32 (https://git.drupalcode.org/project/byte_theme/-/merge_requests/32) opened, same fix as the patch, just wrapped in {% if media.src %} so the hero-blog component renders cleanly when a blog post has no featured image. CI is green. Thanks for the review!