How can I disable blocks (e.g., the menu block or the search block) from a custom module?

Concretely I have an access to my clients managed by a custom module, and I don't want anything appears on their access page except my company's logo.


If you want to change the visibility of many blocks at the same time, this module would be perfect:


Block Visibility Groups allows the site administrator to easily manage complex visibility settings that apply to any block placed in a visibility group. The visibility settings for all blocks in the group can be edited on one administration form.

This modules works in conjunction with the Drupal 8 core's block administration system. It is a simpler alternative to modules like Panels.

Even you want to code this yourself, you can use this module as a starting point. The author of this module has a 2 hour video how this code was built.


You can try to specify pages in visibility option field in block settings. Or try to use https://www.drupal.org/project/extended_block_visibility if you need specific access rules which placed in custom module

  • I don't see a Drupal 8 version of that module... – Scott Joudry Mar 22 '16 at 11:31
  • Hello Smile, Thanks for the response, but I would prefer to pilot this from my custom module. And when I disable/ enable my custom module the blocks are directly arranged. Any idea? Thanks! – 123pierre Mar 22 '16 at 11:38

If you don't want to install another module to achieve this you might want to disable blocks in hook_preprocess_page similar to this

function THEMENAME_preprocess_page(&$vars) {
  if ( /* custom logic */ ) {

It can certainly be used in your THEMENAME.theme file but I believe it can be used in a module as well


I've used hook_block_view() to do something like this. I defined conditions for my block to appear in the function. My block was a form, so I could assign $blocks['content'] to a drupal_get_form() call, and return my $blocks if my conditions were met.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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