Start of Main Content

At this year’s Drupalcon, Dries Buytaert outlined his vision for Drupal's roadmap: a focus on Ambitious Site Builders and the site builder experience.

In the spirit of that, we plan to release a series of Drupal modules this year that augment functionality for the Ambitious Site Builder and the site-building experience. Our first module is Views Minimum Condition. Before we talk about it though, we’ll explore what a “Condition” and “Context” are in Drupal and how they power the Views Minimum Condition module.

Learn more about the custom Drupal modules we create.

As an Acquia Certified Drupal Cloud Practice with a Triple Certified Drupal Expert we can build custom Drupal modules to save your content authors valuable time so they can focus on the marketing tasks that matter the most to your organization.

What is a Condition?

Views Minimum Condition takes advantage of the Condition Plugin API in Drupal core. As a site builder, you may not have heard of the Condition Plugin API, but you’re probably aware of it if you’ve ever configured or placed a block in Drupal. Conditions are used to drive visibility of the elements they’re attached to. Blocks make the most of Condition plugins. For example, if you wanted to show a block on two content types only, you can set them on the block configuration screen:

The “Configure block” screen in Drupal where the option “Content type” is highlighted and boxes for the “Homepage” and “Landing Page” are checked.
How to set the same block to appear on two separate pages.

Blocks have several options for setting if they should show on a page or not. You can show a block based on the page language, if the page contains certain taxonomy terms, or if the block is on a user profile page. The options are numerous, and they’re all driven by the Condition Plugin API.

Any Entity class can implement an interface to hook into these plugins that interact with and manipulate certain behaviors of that object. For example, a Block is an instance of an Entity that implements this interface. While blocks are the most common place you’ll encounter visibility conditions, there are other areas you’ll see them too. For instance, you can set visibility conditions on your Google Tag Manager (GTM) container to dictate when the GTM script should be inserted into a page:

The “Add container” screen in Drupal where a container is labeled “Google Tag” and in the settings, you can put your Google Tag Manager ID in the “Container ID” field. The section “User role” is highlighted. Under the heading “Insert snippet for specific roles” the radio button “Only the selected roles” is highlighted, and under the heading “Selected roles” the box “Anonymous user” is checked.
How to set visibility conditions for when a GTM script is inserted on a page.

What is a Context?

As an ambitious site builder, you can take Conditions a step further by installing the Context module, which adds an interface in the Drupal admin that lets you compose Conditions together into a single visibility rule. The power of the Context module is that it lets you create as many Contexts as you want. You can configure them in different ways around the site to dictate visibility of elements on a page. Contexts can also be grouped together for more advanced cases.

That means that any tool that hooks into the Context system can take full advantage of visibility for content or components. In the case of Acquia Site Studio, if you’re a site builder, you can apply their custom contexts to a variety of elements to determine if they should show or hide for a user.

This is where our module Views Minimum Condition comes into play. It’s not uncommon to have a requirement when you’re building a site to show content or data using Views. A common example of this is a Related Content or a More Like This section of a page where you display other content to check out that’s related to the page you’re viewing.

The following is an example of a Related Content display component powered by Views inserted at the bottom of an Article content type page:

A “Related Content” component with two article headlines and links under each that say, “Read more”.

This looks great! On the page it’s displaying on, there are two results from the View. What happens though if you visit a page that has no related content? You may see something like this:

A “Related Content” component with no content or links underneath it.

The component has no results, yet it still shows. Depending on how you construct your components or displays in Drupal, setting the Views option of “hide if no results” may not be enough to control the display of these, particularly when they are composed together in a broader component with tools like Acquia Site Studio or Layout Builder in Drupal.

Adding Views Minimum Condition in a Context

Site builders: this is where you can take advantage of Context and visibility rules. You want to create a Context that is true when the Related Content View has results to display. You can create this quickly in the Context admin screen:

The “Edit context” screen in Drupal. Under the “General Details” section there is a required field for “Label” which says, “Article has related content”. The field under “Group” says “Content” and the field under “Description” says “Checks that the embedded View has results.” The box “Content enabled” is checked. Another box that says “Require all conditions” is checked. The section “View is not empty” is highlighted and the required dropdown field “View & View Display” is set to “Article Related Content”.
How to create a Context that shows the Related Content component when there are pieces of related content.

What you’ve done is create a Context, gave it a clear name and description, and add a few conditions into it. First, check that the current theme is your site theme and that the content type being viewed is “Article”—this keeps the Context from being fired on other content types and administrative pages.

After that, add a “View is not empty” condition. This is a new condition provided by the Views Minimum Condition module that we recently released. Once added, you can select a View to check for results. When the Context is evaluated, that View will be checked to see if its total results are greater than 0. After that, you can use this Context to dictate visibility of page level items. We’ll show you how that would work in Acquia Site Studio.

After placing a component into a page or template, you have the option to set “Context Visibility” on it. This lets you select from one or more Contexts to apply which will determine if it shows or not for the user. You can add the Context you just created:

Settings for a container in Acquia Site Studio. Under the expanded section “Context” there is a field called “Context” which says, “Article has related content”. Under the section “Pass condition” there is a field called “Selection condition” which says, “All criteria must pass”.

Now the container that you applied it to will only appear if that Context is true. This means everything in the container is affected, line dividers, text headings, descriptions, and your Related Content View itself. With the Context now applied, a page without any related content is now completely hidden and looks correct:

The bottom of an article page with no “Related Content” component, which just shows the website’s footer.

If you were to visit a page that had results in the Related Content View, it would show the component again with results.

This is a very powerful way to show/hide parts of a page to the user based on Views. As a site builder, you can think of various cases around this. If you wanted to show a different layout, different component, or additional information if the View is empty, you can. You can tag components with the same Context for the same effect. You could also create a Context with the same conditions, but “negate” it for more control. This would be useful in cases where you want to check that a View does not have results. That is achievable by checking off the “Negate this condition” checkbox.

We added an additional feature to the condition as well. What if you only wanted to show this component if the View had more than a certain number of results? Sometimes it can look odd if there was one result for a component designed for three or more. There is an option in the condition to check for that, too:

A screen in Drupal where the highlighted and the required dropdown field “View & View Display” is set to “Article Related Content”. A box is checked which says, “Enforce a minimum threshold” and a field for “Minimum result count” is set to 3.

Now this will only show if the selected View has three or more results.

Get Site Building!

The Contexts and Condition plugins are a very powerful tool if you’re an ambitious site builder looking to leverage additional control in your site building right from the Drupal admin. This adds a lot of flexibility in implementing display logic without needing to write any code. The Views Minimum Condition module helps site builders like you wield more control over what displays and when it can be combined with several other conditions to create expressive Contexts for a site.

You can download Views Minimum Condition today for Drupal 8 or 9 right here on Drupal.org. The module will have a Drupal 10 release when Drupal 10 is released this December. It’s opted into security coverage on Drupal.org, backed by unit tests, and production ready.

Keep an eye out for future articles where we’ll discuss other modules, we are releasing to help bolster the site builder experience for Drupal!

Want to learn more about the custom modules we create for Drupal or ask for our assistance creating a new Drupal site for your organization? As an Acquia Gold Partner with an Acquia Certified Drupal Cloud Practice and a Triple Certified Drupal Expert, we’re ready to help. Reach out to start a conversation with us.

Published:

Latest Ideas

Take advantage of our expertise with your next project.