A Handy guideline on using Drupal 8 Condition Plugins API

Drupal has undoubtedly emerged as one of the finest open source web development platforms. With its latest version 8, Drupal has introduced new features, functionalities, bug fixes etc., which have helped web designers and developers build out-of-the-box websites and web applications. The amazing collection of Drupal plugins and extensions makes room for creation of feature-rich web apps and portals. One such Drupal plugin that has gained grounds among Drupal programmers is the conditions plugin. Although the conditions plugin API available with Drupal 8 has been used by global Drupal developers, it wasn't until the emergence of DrupalCon Austin sprint that these professionals managed to get blocks that could use the Conditions Plugin API for ensuring block visibility. My aim behind writing this post is to make you familiar with all the steps involved with usage of Drupal 8 Condition Plugins API. So, let's gear up and unleash everything about these steps.

What exactly is a Condition Plugin?

Installing the Condition Plugin in your Drupal powered website allows you to use generalized conditions during the conditional block placement operation. One of the greatest advantages of using a Condition Plugin is that it comprises of re-usable chunks of code, allowing designers and developers to take its advantage during numerous contrib projects.

How will I cover the use of Drupal8 Condition Plugins API in this post?

Thanks for your continued reading. In this blog, I'll show you the way in which you can create a sample Page Message module that uses a RequestPath condition plugin for displaying a message on the configured page. In my example, I'll be creating an administration form that would allow the user to enter a list of paths wherein the message can appear.

I'm sure you might be familiar with the list of paths. Well, it's exactly the same thing that's being used by the block visibility forms. Hence, we'll be using the conditions plugin for re-using the paths. Here's the list of steps involved with the same:

Step 1- Setting up the plugin instance

Prior to allowing the users to set the paths, you need to create an admin form. Below is the code you need to use for doing the same with the help of Conditions plugin:

class PathMessageAdminForm extends ConfigFormBase {

  protected $condition;

  public function __construct(ConfigFactoryInterface $config_factory, FactoryInterface $plugin_factory) {


    $this->condition = $plugin_factory->createInstance('request_path');


  public static function create(ContainerInterface $container) {

    return new static(  $container->get('config.factory'),  $container->get('plugin.manager.condition')    ); 


For the above:

Ø  ConfigFactory: used for saving the settings that are passed to the parent constructor

Ø  Condition plugin factory: used for creating an instance of a Drupal\system\Plugin\Condition\RequestPath condition plugin


Step 2- Building the admin form

Here's the code that needs to be used for building the admin form already explained in step no.1:

  public function buildForm(array $form, array &$form_state) {

    // Load our default configuration.

    $config = $this->config('path_message.settings');

    // Set the default condition configuration.


    $form['message'] = array(

      '#type' => 'textfield',

      '#title' => $this->t('Message'),

      '#description' => $this->t('Enter the message you want to appear'),

      '#default_value' => $config->get('message'),


    $form += $this->condition->buildConfigurationForm($form, $form_state);

    return parent::buildForm($form, $form_state);


In the above code, there are two form fields: one for the message and other is the one created by the RequestPath plugin.

Step 3- Saving the form values

Below is the code that allows you to save the form values:

  public function submitForm(array &$form, array &$form_state) {

    $this->condition->submitConfigurationForm($form, $form_state);


      ->set('message', String::checkPlain($form_state['values']['message']))

      ->set('request_path', $this->condition->getConfiguration())


    parent::submitForm($form, $form_state);


The above mentioned code lets the Conditions plugin process the form values and validate the received input.                                                                                                           

Step 4- Evaluating the condition

Now, with a specific configuration stored in config, you need to evaluate the condition for using the same. In case of block visibility, you can use it for showing or hiding a block.

Wrapping Up

With that, it's a wrap on this post. Hope you'd have loved going through the process of using a single condition plugin. Honestly speaking, there's a lot more to explore with Drupal 8's core condition plugin types and you can make the most of them by installing them onto your Drupal enriched website.

Author Bio :

Jack Calder is a talented developer at Markupcloud - PSD to Drupal Conversion company. He has been in this industry for more than eight years and like to write on latest topics related to PSD to Drupal conversion, PSD to HTML Conversion, PSD to Drupal Template, etc.