Overview
Quoting @mherchel from #3520487-57: Refuse editing an individual node's component tree (and bring back after 1.0 once exposed slots are allowed):
Had a discussion with @penyaskito at https://drupal.slack.com/archives/C072JMEPUS1/p1759435412275339, and he asked me leave a comment here.
I have a use case where I override the field template for the canvas field
field--canvas-page--components.html.twig. The use case to add a CSS class that sets default spacing on all direct children below it, and overriding the template works perfectly.However this template isn't called within the iframe preview within the canvas editor.
One of the main issues is that I'd hope that the markup of the page would be consistent (as possible). Removing this field template only on the editor iframe introduces inconsistencies that other people might run into.
- #3520487: Refuse editing an individual node's component tree (and bring back after 1.0 once exposed slots are allowed) made the editor frame's preview consistent with the live site, by making
field--component-tree.html.twignot render any markup. - #3548854: Add test coverage ensuring consistency on previews markup and live markup when using a content template still needs to add test coverage proving the editor frame preview is always consistent with the live site
However, both of those assume that field--component-tree.html.twig does NOT get customized.
@mherchel's theme is overriding that template, and even on a per-entity type/bundle basis.
Proposed resolution
Update the editor frame's preview rendering to be aware of the host entity type + bundle, and wrap the rendered component tree in the appropriate field--component-tree.html.twig.
\Drupal\canvas\Controller\ApiLayoutController::buildPreviewRenderable() is currently rendering the component tree directly, which is why this isn't showing up.
User interface changes
Imperfect → perfect previews!


Comments
Comment #2
wim leersHere's a first stab. AFAICT this should result in what you want, but it'll have broken the ability for the canvas UI to render outlines around the component instances. There's a
@todoin there with pointers on how to fix that.Comment #3
effulgentsia commentedI agree that we need to make Preview and Published consistent, but I think we should go the other way and make sure that when a Canvas field is rendered on the published page, it's rendered without all the
theme('field')stuff. The mental model should be that on a Page and on a Content Template (and when we get to it, within exposed slots of nodes), the Canvas defines the entirety of what's being rendered within the "content" region and/or exposed slot.The
<body>has acanvas-pageclass, and the content region has aregion--contentclass, so can you achieve this use case by using.canvas-page .region--contentas your selector or prefix?Comment #4
mherchelYep I can. I'm doing something similar as a work-around right now.
Agree that the most important thing is making it consistent.