Is there a way to hide blocks on unpublished pages?

Comments

trantt’s picture

I am not sure this is what you wanted but it is worth a try: http://drupal.org/node/64135

GC_Chi’s picture

Thanks for suggestion, but I've checked that page several times and it doesn't offer this option. I'm talking about publishing options, when an article is unpublished, some blocks should remain hidden.

– – – – – – – – – – – – – – – – –
smart reviews for serious gamers
www.gamecritics.com

hedac’s picture

having the same problem here...
I would like to hide all the blocks associated with one page I have... Since the page is not published yet... I don't want the blocks to be seen either.

jeromewiley’s picture

Looking for a similar solution for Drupal 8.

nno’s picture

For Drupal 8:

function my_module_block_access(\Drupal\block\Entity\Block $block, $operation, \Drupal\Core\Session\AccountInterface $account) {
  if ($block->id() == 'my_block_id') {
    $node = \Drupal::routeMatch()->getParameter('node');
    // Hide the block if the node is not published
    if (($node instanceof \Drupal\node\NodeInterface) && ($node->isPublished() == FALSE)) {
      return AccessResult::forbiddenIf(true)->addCacheableDependency($block);
    }
  }
}