4

I have a page whose content is visible only in certain provinces. When people visit this page from a different province other than the one it is set for,it should be giving a message saying "the content is not available in the region" by overwriting the node's body in the same page, by using hook_node_view. But I have blocks enabled in the content area on this page. is there a way to disable the contents of the block? i tried hook__block_view and hook__block_view_alter but it did not work. Can someone help me?

3
  • are these blocks in a custom module you wrote or provided by other modules?
    – Jimajamma
    Commented Aug 16, 2012 at 20:16
  • Its a view block
    – Jons
    Commented Aug 16, 2012 at 20:19
  • You have a typo in your hook definition. It's hook_block_view_alter not hook__block_view_alter (one underscore after "hook" not two). Commented Apr 1, 2014 at 14:23

5 Answers 5

5

You could try hook_block_list_alter to hide a specific block.

Act on blocks prior to rendering.

This hook allows you to add, remove or modify blocks in the block list. The block list contains the block definitions, not the rendered blocks. The blocks are rendered after the modules have had a chance to manipulate the block list.

2
  • 2
    I'm surprised this had a negative vote. This answer worked for me, and the answer is relevant to the original question. Within this hook, you can use foreach ($blocks as $key => $block), and within your foreach, do some basic test (like check for $block->status ==1), then when you find the desired block to unset, use unset($blocks[$key]);
    – Webdrips
    Commented Sep 7, 2016 at 9:22
  • 1
    the advantage is you're not running the view at all when not required
    – troseman
    Commented Mar 22, 2017 at 23:57
4

This question is quite old, but unanswered, so I'd respond for the record.

When you install Drupal with the standard install profile, some blocks (Search, Login, Navigation, ...) are enabled automatically.

So if you take a look in profiles/standard/standard.install, you will see that blocks are enabled with a db query and are relative to a theme.

So disabling a block is just an update query:

db_update('block')
  ->fields([
    'status' => 0,
  ])
  ->condition('theme', 'your_theme')
  ->condition('module', 'the_module_providing_the_block')
  ->condition('delta', 'the_block_delta')
  ->execute()
;

You may need to clear caches after that.

2

The ninja solution for this case would be a hook_page_alter. Also, hook_block_view_alter is a valid hook so I don't know why you can't get it to work.

If your block is from Views, you can set some tricky arguments to hide the block with no extra settings or coding.

Also, you can set per-block visibility settings as a php code.

-1

If you can't get any of the block alter hooks to work, and/or you don't want to use PHP from the Blocks UI/Admin Page, you could dull ax it by creating your own block and then calling the view, eg, something along the lines of this in your own block:

 if ( "content is available" ) {

   $block['subject']='Subject....';
   $block['content']=views_embed_view('NAME_OF_VIEW','BLOCK_DISPLAY');

   return $block;

 }

and then include that block in your content region instead of the Views block.

-1

most simple way to implement is create a block-YOURBLOCKID tpl in your theme , and in that tpl write the condition

if(AVAILABLE){
 //display block//
} else {
 echo 'Not available in your region';
}

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.