Leverage view modes when rendering entities
Introduction
When rendering entities in layout builder or blocks leverage view mode over fields when possible.
Decision
When possible, render entities within layout builder blocks and reusable blocks using the view modes instead of rendering individual fields.
Context
If fields are rendered as the layout builder blocks, they lose consistent core classes that can be reused for front-end development. When the fields are wrapped in the view mode then front-end developer has access to the view mode template overrides and the fields are rendered consistently regardless of where the entity in this particular view mode is going to be used. It becomes an atomic component that looks styled independently of Layout Builder section or theme region. This promotes front-end code reusability, allows for cleaner themes and reduces effort needed for styling. It also allows to produce predictable results for the editors since components they are placing are semantically and functionally atomic.
Consequences
- Potentially increased amount of templates in the theme to accommodate all of the view modes.
- Cleaner Layout Builder UI (less fields are exposed to the editor)
Exceptions
- You need to place just one field and it’s not going to be reused anywhere else, it’s used only once and it doesn’t make sense to create view mode just for that e. g. Body field on the nodes.
Additional Resources
None