Problem/Motivation
When the CSS field formatter is configured to render the <style> element in <head>, Layout Builder's live update is unable to update the CSS code. The live update feature works when the CSS field formatter is configured to render the <style> element in <body>.
Proposed resolution
Modify \Drupal\field_css\Plugin\Field\FieldFormatter\CssFormatter::view() to check the current route. If it belongs to the following Layout Builder routes, then override the formatter's configuration and render the <style> element in <body>:
- layout_builder.add_block
- layout_builder.update_block
- layout_builder.remove_block
- layout_builder.defaults.*
- layout_builder.overrides.*
Remaining tasks
- Submit patch
- Update tests
User interface changes
Layout Builder's live preview will now reflect changes in real time for CSS field formatters that are configured to render <style> element in <head>.
API changes
None.
Data model changes
None.
Release notes snippet
Release [version] adds support for live updates in Layout Builder when a CSS field formatter is configured to <style> element in <head>.
Original report by ericras
When this is used in a custom block inside layout builder, the CSS isn't inserted/updated on the content preview screen upon creation/update of a block with a CSS field.
This is because Layout Builder renders the whole content section anew and sends it back via AJAX, but this module doesn't output the CSS in the regular content region.
Approach to solve this:
- the current <style> block needs an ID so it can be removed upon update.
- a couple AJAX commands need to be triggered by an update to Layout Builder https://www.drupal.org/docs/8/api/ajax-api/core-ajax-callback-commands
| Comment | File | Size | Author |
|---|---|---|---|
| #4 | field_css-layout_builder_live_preview-3154655-4.patch | 3.85 KB | chris burge |
Comments
Comment #2
ericras commentedComment #3
chris burge commentedI tried pursuing the approach laid out in the issue summary and was unsuccessful.
An alternative approach would be to check the current route and in certain cases, override the location from 'head' to 'body'.
Drupal\field_css\Plugin\Field\FieldFormatter\CssFormatter:Comment #4
chris burge commentedPatch attached.
Comment #6
chris burge commented