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?
5 Answers
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.
-
2I'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, useunset($blocks[$key]);
– WebdripsCommented Sep 7, 2016 at 9:22 -
1the advantage is you're not running the view at all when not required– trosemanCommented Mar 22, 2017 at 23:57
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.
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.
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.
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';
}
hook_block_view_alter
nothook__block_view_alter
(one underscore after "hook" not two).