Problem/Motivation
Component library is missing when it is not rendered via Twig.
If rendered with Drupal JavaScript API - such as a custom Drupal.theme.myComponent for instance - the component's library is not loaded.
Steps to reproduce
- Create a single-directory component in a module
- Render the component programmatically
- Attach the output in drupalSettings
- Create a custom
Drupal.theme.myComponentfunction and consume the output - Call the Drupal.theme
- Result: the component Twig is rendered but the library is missing (i.e. no CSS no JS)
Proposed resolution
TBD
Remaining tasks
TBD
Comments
Comment #2
matthieuscarset commentedThe scenario which leads me to this issue was the following: How to use a SDC to theme Drupal messages individually?
Since Drupal 10.3 and this bug in particular, there is no way for individual message to use a Twig template. Each Drupal messages are rendered via Drupal.theme.message.
Consequently there is no other choice than implementing a custom
Drupal.theme.messagemethod in a custom theme or a custom module and - if one wants to use a SDC to render messages - I don't see any other way than to pass the component output todrupalSettingsto be reused in JS.Example code in this snippet: https://git.drupalcode.org/-/snippets/214
Comment #3
pdureau commentedSo, maybe we need to fix the root cause instead of introducing a workaround: #3456176: 10.3 upgrade now missing status-message theme suggestions
Comment #4
matthieuscarset commentedThis is "the root cause" for this specific scenario (e.g. using SDC to theme Messages). The feature request is still relevant for any other use cases such as printing mycomponent:xyz using a custom Drupal.theme.xyz in JS.
If developers needs to print a single-directory component via JavaScript, the component fails its promises because it is missing its library.
The current workaround is to manually attach the library (see snippet above and code below) but the purpose of this issue is to find a sustainable solution in core.
Comment #5
pdureau commentedSo, we need to add attach the asset library from ComponentElement render element in
preRenderComponent()method?The asset library is already attached in ComponentNodeVisitor:
It would be the dream to remove
ComponentNodeVisitorand move all the logic toComponentElementbut it is not possible yet because it will be a compatibility break for the poor souls using Twigincludeorembedto call a component template.So, if we add asset library attachment in
ComponentElement, the library will be attached twice. It is supposed to be OK because the asset library API takes care of this, but we need to be sure there is no side effects.Comment #6
quietone commentedChanges are made on on 11.x (our main development branch) first, and are then back ported as needed according to our policies.