Twig include of SVG generates 500 errror

Created on 22 April 2025, 20 days ago

Version

  • Drupal version: 11.1.6
  • Twig version: v3.19.0

Problem/Motivation

When I have a Twig file in a component, with

{{ include(componentMetadata.path ~ '/images/pager-first.svg') }}

, we get a 500 error

Steps to reproduce

Modify a component's twig file to have only the following
{{ include(componentMetadata.path ~ '/images/pager-first.svg') }}

Note that I know this is not correct, but it should not blow up the page with a 500 error.

This is the error:

The website encountered an unexpected error. Try again later.

LogicException: Attribute "value" does not exist for Node "Twig\Node\Expression\Binary\ConcatBinary". in Twig\Node\Node->getAttribute() (line 158 of /var/www/html/vendor/twig/twig/src/Node/Node.php).
Drupal\sdc_devel\Plugin\TwigValidatorRule\TwigValidatorRuleFunction::includeIsComponent() (Line: 135)
Drupal\sdc_devel\Plugin\TwigValidatorRule\TwigValidatorRuleFunction::include() (Line: 83)
Drupal\sdc_devel\Plugin\TwigValidatorRule\TwigValidatorRuleFunction->processNode() (Line: 43)
Drupal\sdc_devel\TwigValidator\TwigRulePluginVisitor->enterNode() (Line: 62)
Twig\NodeTraverser->traverseForVisitor() (Line: 65)
Twig\NodeTraverser->traverseForVisitor() (Line: 65)
Twig\NodeTraverser->traverseForVisitor() (Line: 65)
Twig\NodeTraverser->traverseForVisitor() (Line: 65)
Twig\NodeTraverser->traverseForVisitor() (Line: 65)
Twig\NodeTraverser->traverseForVisitor() (Line: 65)
Twig\NodeTraverser->traverseForVisitor() (Line: 53)
Twig\NodeTraverser->traverse() (Line: 136)
Drupal\sdc_devel\TwigValidator\TwigValidator->processSource() (Line: 74)
Drupal\sdc_devel\TwigValidator\TwigValidator->validateComponent() (Line: 45)
Drupal\sdc_devel\Validator->validateComponent() (Line: 37)
Drupal\sdc_devel\Validator->validate() (Line: 212)
Drupal\sdc_devel\Controller\ComponentValidatorOverview->getComponentMessages() (Line: 111)
Drupal\sdc_devel\Controller\ComponentValidatorOverview->overview()
call_user_func_array() (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 593)
Drupal\Core\Render\Renderer->executeInRenderContext() (Line: 121)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext() (Line: 97)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 183)
Symfony\Component\HttpKernel\HttpKernel->handleRaw() (Line: 76)
Symfony\Component\HttpKernel\HttpKernel->handle() (Line: 53)
Drupal\Core\StackMiddleware\Session->handle() (Line: 48)
Drupal\Core\StackMiddleware\KernelPreHandle->handle() (Line: 28)
Drupal\Core\StackMiddleware\ContentLength->handle() (Line: 32)
Drupal\big_pipe\StackMiddleware\ContentLength->handle() (Line: 116)
Drupal\page_cache\StackMiddleware\PageCache->pass() (Line: 90)
Drupal\page_cache\StackMiddleware\PageCache->handle() (Line: 48)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle() (Line: 51)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle() (Line: 36)
Drupal\Core\StackMiddleware\AjaxPageState->handle() (Line: 51)
Drupal\Core\StackMiddleware\StackedHttpKernel->handle() (Line: 709)
Drupal\Core\DrupalKernel->handle() (Line: 19)

Proposed resolution

The site should not crash. It should show any errors (like maybe using source() instead of include())

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

πŸ› Bug report
Status

Active

Version

1.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States mherchel Gainesville, FL, US

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

Comments & Activities

  • Issue created by @mherchel
  • πŸ‡ΊπŸ‡ΈUnited States mherchel Gainesville, FL, US
  • πŸ‡«πŸ‡·France pdureau Paris

    Hi Mike, thanks for the ticket.

    It should show any errors (like maybe using source() instead of include())

    I agree, there is 2 things to do here:

    • Prevent crashing when the first parameter of include function is an expression instead of string scalar. Example: include(componentMetadata.path ~ '/images/pager-first.svg')
    • Add a warning when include function is used for an SVG file

    Logic proposal for this warning

    • The first parameter of the include function is a string scalar:
      • Raise a warning if the string is not a full (provider:component) component ID: "Include function must only be used for component inclusion."
    • The first parameter of the include function is a CONCAT expression:
      • Raise a warning if (the last part of the expression is a string ending by ".svg") AND there is no other parameters in the include function: "Use source() function instead of include() to embed an SVG"

    (the last part of the expression of the first parameter of an include function is

Production build 0.71.5 2024