Block Exclude Pages Module (Drupal 8)
The improved blocks system in Drupal 8 provides a lot of flexibility to the site builder. But, have you ever had the problem of blocks showing on undesired pages because of the limits to visibility patterns?
The Problem Scenario
Say you added a custom block that only shows for users, so you set a visibility path with a wildcard like so: “/user/*”. All works great, as it should, and life is great!
Oh but no! Don’t celebrate too fast says your project manager. Here comes the problem. Your project manager, now (despite that it all works like it should) for no reason whatsoever, wants to hide that neat custom block from an specific page under “user/*”path.
Ok, all hell breaks loose. How do you prevent that specific page from showing the block when you have already set to show on all pages under the “/user*”path? Sure, in Drupal 7 you had the option of adding some custom PHP code to accomplish that. But what about Drupal 8, where you are not allowed to use any PHP code in the interface?
The Answer: Block Exclude Pages
At Texas Creative, we work for the most part on large scale websites with data connection from multiple sources such as API’s and interactive pages. The block excluding problem was a common issue that we kept stumbling across. Each developer was coming up with creative ways to work around it, but that made it hard to maintain as each site is different and some solutions were just too “hacky”. It was time to create a module, called Block Exclude Pages, that would solve this issue using the standard Drupal block visibility interface.
How to Exclude Pages From Block Visibility
When installed in your Drupal website, the Block Exclude Pages module adds a new pattern for page visibility that will exclude a path pattern. Simply prefix the path pattern with an ‘!’.
When the page visibility option is set to: “Show for the listed pages”, then the excluded paths, prefixed with “!” will hide the block on those pages despite the other matches. On the other hand, if the page visibility option is set to “Hide for the listed pages” the excluded paths, prefixed with “!”, will show the block on those pages despite other matches in the list.
In the following example, this block is placed on a user page, but the block is excluded from the ‘jc’ user page and all subpages for that user page:
This module has become an important tool in our arsenal for complex websites. I hope it will become a good tool for you too.
Happy Coding! ;)
Below are some other Drupal related blogs by Texas Creative: