Redfin Solutions logoRedfin Solutions logoContact

A NotNodeType Condition Plugin for Drupal 8/9

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