Library Layouts Auto Selection & Selection Rules Like It Was In Panels (D7) for Page Variants

Created on 22 October 2019, about 5 years ago
Updated 31 January 2023, almost 2 years ago

Problem/Motivation

We had a project where customer requested to be able to select dynamically different layouts for different site elements like pages, headers, footers, separate blocks, paragraphs etc.
So it assumed to be exposed for site admin roles, but we didn't want to expose access to Content Type forms, it was important for us to be able export some layouts through configuration & on other hand blacklist some layouts that will be managed or even created by an administrator of the site.

Also we had a requirement to have different layouts for the same entities depending on context e.g. on cookie value, on entity field's value (e.g. for entities with taxonomy term Art show one template while for Science another and so on).

Proposed resolution

We checked layout library & it already had a layouts list & basic integration with display we decided to adjust list a bit in order to configure layouts by weights & add own selection rules (condition for selection one or another layout based on selection rule annotated plugin system)

So what it does: it adds an actions on library layout list for layout entities:
1) manage weights - it leads to a sub page & uses drag & drop table & limits all layouts from LL (Layouts Library) to layouts of a particular entity type bundle, layout entity was adjusted to have weights & on page save entities weights are updated
2) manage selection rules - this is another sub page for a chosen layout, it has a drag & drop list of all selection rules plugins & allows you to edit, delete & add new selection rules plugin istances

Entity Displays were adjusted & have no 'use layouts auto selection' checkbox, if ticket for a given entity display & if entity has no explicit layout reference field value then default layout will be automatically selected from LL by weight, first layout with most less weight will be used, however if layout has selection rules plugins they will be looped by weight & if any of the rules return false on the check routine then that layout will be skipped.

This way we can configure auto selection per view mode & export it through default display configurations.
On other hand layout entities now have all details about weight & selection rules plugin inside & are config entities, so we can split layouts by name wildcard pattern, e.g. config ignore all custom_layout_* entities to let users create it without deploy impacts but if we need to have exportable layouts we can create those without custom prefix

Selection rules plugin uses plugin manager & annotation system, we implemented one plugin called FieldValue - which allows you to select any entity type bundle's field & using native field widget select a value (e.g. term from vocabulary or status or other fields as well)

Once selection rule is configured it's stored inside of layout's thirdPartySettings & you can add many instance of the same plugin with different field, all selection rules have IDs which are deltas in settings array, so they are unique only per layout, SelectionRule::load function gets ID & layout argument, save, delete & the rest methods were implemented, also it uses exportedProperties annotation field & automatically export to configuration all new plugin props, you can check SelectionRuleInterface & FieldValue plugin implementatoin

Also selection rules plugin implements container injection interface & you can inject services as in forms classes

Remaining tasks

Adjust code style, more or less it's okay, but e.g. not all services were moved to injections, some trailing spaces etc.
Probably it's a good idea if somebody will cover this logic with tests.
We are working on some other plugins (like cookie value) & some other layout related logic for the project

User interface changes

Entity Display - added checkbox "use layout auto selection from Layout Library"
Layout Library listing has now new actions (manage weights, manage selection rules) which provide sub pages to configure auto selection & selection rules logic

Data model changes

Layout entity now has weight property, we added hook update to update entity schema as entup was removed since Drupal 8.7

Entity Display now has addition checkbox in thirdPartySettings
Layout entity uses thirdPartySettings to store selection rule plugin settings & weight

✨ Feature request
Status

Needs work

Version

1.0

Component

Code

Created by

πŸ‡¬πŸ‡·Greece Pavel Ruban

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024