Trigger SDC render element when using Twig include or embed

Created on 30 October 2024, 22 days ago

Problem/Motivation

SDC documentation is currently promoting using Twig's include function and & embed tag to call components from template. And it seems to be a popular way of using SDC components (for now, let's hope it will disappear as soon as SDC components are becoming usable directly from the render API and the display builders)

There is a big issue about include and embed: they don't pass through the SDC render element and don't trigger the Render API mechanisms.

So, SDC is currently doing a complex workaround.

ComponentsTwigExtension is adding 2 twig functions:

  • add_component_context which is adding the attribute Attribute object if missing
  • validate_component_props which is executing the JSON schema validator

They are not supposed to be used by templates owner, there exists because ComponentNodeVisitor is printing those Twig functions on every template, which will be executed on rendering in this order:

  1. attach_library
  2. add_component_context
  3. validate_component_props

In order to make the use of "template to template" Twig functions or tags (include and embed) more similar to the use of the Render API, leveraging the render element. For example, attach_library is used here because #attached is not executed.

This is a mess which can be fixed.

Proposed resolution

  1. Add asset library attachments (#attached) and prop validation to the render element itself
  2. Override Twig native include and embed to return the SDC render element when the "template" is a full component plugin ID instead of a template path.
  3. Remove everything but ::getNodeVisitors() in ComponentsTwigExtension and most of ComponentNodeVisitor

So, people using SDC from the Render API and people using SDC from templates will have , implemented in a cleaner way.

API changes

If done right, this may be free of breaking changes.

📌 Task
Status

Active

Version

11.0 🔥

Component

single-directory components

Created by

🇫🇷France pdureau Paris

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

Comments & Activities

Production build 0.71.5 2024