0

I have a block and I want to show it for specific content types and taxonomy terms. There is no common pattern for those pages.

For example, the URLs could be the following ones:

I don't want to show the block on the Demo content type, but on nodes of the content types, and term pages, without cloning/replicating the block.

How can I achieve this?

4
  • You'll need to use the conditions available when you edit the block. If conditions you need are missing, it's easy to create your own using the Condition API
    – Clive
    Commented Jan 9, 2018 at 12:09
  • 2
    Code won't help much when there is no common pattern. To build complex conditions try drupal.org/project/block_visibility_groups and the linked modules on the project page.
    – 4uk4
    Commented Jan 9, 2018 at 12:12
  • @4k4 Thank You, this module helped me and I got the results as desired.
    – FAB
    Commented Jan 10, 2018 at 6:40
  • 1
    Does this answer your question? How can I control block visibility with code?
    – leymannx
    Commented Jan 27, 2022 at 18:29

1 Answer 1

0

If you control the code for the block, then it really is a simple matter of:

  • Defining the correct cache contexts (path probably)
  • In the build method, determining the context (getting current node, tax info from the node, and the like.)
  • Deciding what to output for the context
  • If you don't want to display it, return and empty build array (or NULL).

If you don't control the block content (e.g. view or 3rd party module), you can do the same as above and use the various methods to render the block from code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.