Upgrade Your Drupal Skills

We trained 1,000+ Drupal Developers over the last decade.

See Advanced Courses NAH, I know Enough

A NotNodeType Condition Plugin for Drupal 8/9

Parent Feed: 

Have you ever wanted to exclude a block on just one content type? You would think that the existing block visibility plugin for "Content types: [x]" plus the "Negate this condition" would do it; however, what you quickly learn is that the plugin also returns false for non-node pages altogether! So, for example, when you say "show on NOT article" for example, it really evaluates to "show only on node pages that are not articles."

What we need to do is write a custom Condition plugin which has some more expansive logic, so we get what we want--"show on every page EXCEPT if that's a node page of bundle article."

To do this, we first need a custom module. For small things like this, I usually scaffold out a small module called "tweaks" using Drupal Console. In there, you need to create a Condition Plugin (inherits ConditionPluginBase), so create a folder called /src/Plugin/Condition, and in it put a NotNodeType.php with the code from the gist below!

https://gist.github.com/chrisfromredfin/d5b583c30129ac6f98d9687768eb2734.js

Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web