Please note I'm not referring here to showing a block with a list of content that shares the same taxonomy terms as the content it is displayed on, a.k.a. a "related content" block. There are reams and reams of tutorials out there showing you how to do that.
This use case is when you have a specific block, maybe just some custom output, that you only want to show on content that is tagged with certain taxonomy terms, or a term and all its children.
It's a slightly hackish workaround, but here's how to do it. I won't explain in great detail but assume you have some Drupal experience. Feel free to comment if you have questions.
Create a View
First, add a new View, showing the same type of content that you want to show the block on. Add a block display, showing Fields, and limit the number of items to 1.
By default, a View of content showing fields adds the content title field. Just leave this and don't add more fields.
Hide the view if empty
In the "other" section of the block view configuration, set "Hide block if the view output is empty" to "Yes".
Filter the View by taxonomy terms
Add a filter to your view, of type "Has taxonomy term" or "Has taxonomy terms (with depth)". The latter is what you want if you want to show the block on nodes with a specific term and the parent(s) or child(ren) of that term. Select your desired terms.
Note that in the depth filter settings, you will need to select the hierarchy depth you want the filter to use. The default is 0, so your block will only show if the content is tagged with the parent term. If you want it to show for children, select a positive number up to the number of taxonomy hierarchy levels you want; or for parent terms, a negative number.
Add a contextual filter
Add a contextual filter of Content: ID and in the filter settings, under "When the filter value is NOT available" choose "Provide default value > Content ID from URL".
This causes our view to show the same content as the node it is displayed on. We're already filtering it so it won't show anything if the content doesn't have the selected taxonomy terms, and this contextual filter limits it further to this specific piece of content.
Place the block
Using the regular Drupal block layout configuration (or Twig Tweak, or whatever) place the block where you want to show it. You may want to limit it by content type or other parameters.
Customize the block output
You'll need to override the View to show your block content instead of its default output, which is just the title of the node it's on. There are many ways to do this, and which you choose might depend on what kind of block you're showing. You can:
- Check "Override the output of this field with custom text" in the title field configuration and put your custom markup and content in the "rewrite results" field
- If your block is coming from another module, hide the title field by rewriting it and putting a non-breaking space character in it (
). Add a header to the view, of type "Global: Block area" and select the block you want to display. (Note I haven't tested this and how well this works may depend on the block you're showing) - Add custom template(s) for the view to your theme