Leveraging the Context Module with Layout Builder in Drupal
Last year we wrote about condition plugins and the Context module in Drupal, and how they can help determine what content a user can see on a page. We also rolled out a new module called Views Minimum Condition, which enables site builders to use a powerful condition that shows or hides various areas of a page in Acquia Site Studio.
The Context module aids site builders by letting them quickly create their own custom contexts right in the Drupal admin interface. The Layout Builder module included with core Drupal lets site builders quickly create page layouts and add content components to them. However, there is no way to determine when something should display or when it should be hidden.
What if you wanted to do one of the following:
- Show a specific area of the page to privileged users
- Display a section with author details if the page was authored by certain roles
- Show different components under certain conditions
- Display a different call to action for visitors who have not been to the site in several weeks
- Hide certain page details beyond a specific date
Previously the only way you could do that is by using Contexts.
Wouldn’t it be great to add the power of custom contexts to Layout Builder for greater display control? Now you can.
Layout Builder Context Integration
Today, Velir has released the first version of the Layout Builder Context integration module for Drupal 9 and 10. The Layout Builder Context module makes it possible to assign Contexts to both layout sections as well as individual components within a layout. This makes it easy for you to dynamically show or hide entire areas of a page in Layout Builder in any way that you want.
When you enable the module and add a few Contexts, a new area displays in the off-canvas form when interacting with layouts or the components within them:
Like Site Studio, you can add one or more Contexts to meet your needs and require that all of them pass, or just one of them. The option will appear for all core layouts, custom and contributed layouts, and work with any contributed context functionality as well. Since Contexts, Layout Builder and page components can all be created right from the Drupal admin, this puts a ton of power into the hands of site builders, content editors, and marketers alike.
The Layout Builder Context module is crucial when you need more dynamic control over what appears on a page. This opens several possibilities for site builders that didn’t exist before. For example, since condition plugins and context definitions can be created to inspect user field values or session data/cookies, you could integrate your own content personalization logic or interact with an external service. You could A/B test different kinds of content for different audiences or just try different arrangements of components for them to see what works best. The possibilities are endless.
To get started you can download the module today for Drupal 9 and Drupal 10.
Meet Me at Drupalcon 2023
If you’re attending Drupalcon 2023 in Pittsburgh, PA this June, come see my lightning talk on OpenAI / ChatGPT and Drupal 10. I will also be available in the Velir booth every day to discuss all things AI, Drupal 10, and whatever questions you may have.
Get the Drupal 10 Development Cookbook
Are you curious about how to develop modules, themes, and plugins or how to be a great site builder for Drupal 10?
The "Drupal 10 Development Cookbook" is co-authored by Matt Glaman and me. We cover a wide variety of topics with hands-on examples so you can get up and running with Drupal 10 in no time. These topics include running Drupal locally with Docker-based tools, content modeling, creating custom modules, how to do automated testing, and migrating data into Drupal from various data sources. The book will help you understand how to develop and build modules just like the ones we talk about often at Velir.
You can pick up the book on Amazon as a physical copy or for Kindle.