Problem/Motivation
For a frontend theme wishing to provide its own layout builder element widgets, toolbars, etc, the structure of the layout builder render element can be very limiting, forcing some very hacky and fragile overrides.
There does not appear to be a specific theme hook, preprocess or template for themers to cleanly override the element output. You either get to muck around with the very generic and nested "container" and "link" theme hooks, add a #pre_render handler and manipulate the render array directly, or override and extend the render element class to implement a dedicated theme and template yourself.
Proposed resolution
Implement hook_theme() for layout builder module, provide specific twig templates for the layout render element, the section configure links, and the section/block add links.
Issue fork drupal-3373330
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
Comment #2
johnpitcairn commentedComment #3
lauriiiSeems like a great idea 👍 Would be happy to review a patch/MR if you have time to create one.
Comment #4
johnpitcairn commentedThis would affect any existing contrib that adds a #pre_render callback to modify the element, like section_library. Are render elements expected to provide any stability in this respect?
Comment #5
johnpitcairn commentedComment #7
johnpitcairn commentedThat's a start - I added templates for the main layout builder element, and an included template for the add-section elements, with most of the logic moved into those:
Not ready for review beyond that (and general approach). I'll move onto adding templates for the individual sections and their widget links within the next few days.
This WILL break any contrib currently adding a
#pre_renderfunction to alter the output. The way to do that with this in place will be to implementhook_preprocess_layout_builder(),hook_preprocess_layout_builder__add_section(), etc. Themes can just override the templates, preprocess, or both.We may need to move attribute definitions back into
LayoutBuilder::layout()if contrib expect to have those in preprocess.Comment #8
johnpitcairn commentedOr implement
template_preprocess_layout_builder. Maybe add an explicit theme hook for each template instead of just using twig include(), so those have an explicit preprocess with accessible attributes and variables.Comment #9
johnpitcairn commentedI think the best course of action might be to postpone this until the current initiative to improve the layout builder experience is well bounded.
Switching to a theme function and adding twig templates now is likely to collide with everything happening there.
See #3361359: [META] Layout builder editorial improvements
Feedback?