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

Command icon 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

John Pitcairn created an issue. See original summary.

johnpitcairn’s picture

Issue summary: View changes
lauriii’s picture

Seems like a great idea 👍 Would be happy to review a patch/MR if you have time to create one.

johnpitcairn’s picture

This 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?

johnpitcairn’s picture

Issue summary: View changes

johnpitcairn’s picture

That'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:

@layout_builder/layout-builder.html.twig
@layout_builder/layout-builder--add-section.html.twig

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_render function to alter the output. The way to do that with this in place will be to implement hook_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.

johnpitcairn’s picture

We may need to move attribute definitions back into LayoutBuilder::layout() if contrib expect to have those in preprocess.

Or 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.

johnpitcairn’s picture

Status: Active » Postponed

I 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?

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.