1

How can I hide a block on specific content types in Drupal 8? There's the setting for displaying blocks only on specified content types, but not for hiding them. Looks like the PHP filter is out in D8, so not sure how to go about this. I'm sure it's a simple fix.

3

You can use this module:

Block Visibility Groups

Here you can configure a visibility group which hides blocks for a specific content type.

The visibility groups not only provide more options for block visibility, they are also reusable, that means you can apply the same settings on multiple blocks.

Ctools also provides the functionality to hide blocks for content types, but there was an issue, I don't know if this is resolved.

  • Hah, good idea. I needed a solution to make up for settings in another Block Visibility Group, so I don't know why I didn't think to set up a second one with the negated condition. Thanks! – Mrweiner Oct 18 '16 at 17:46
3

Another way you can do this, just via code (in this example, in your theme's .theme file), is like so:

/**
 * Implements theme_preprocess_block().
 */
function themename_preprocess_block(&$variables) {
  // Remove page title block on 'article' content type.
  if ($variables['elements']['#id'] == 'themename_page_title') {
    $node = \Drupal::routeMatch()->getParameter('node');
    if ($node && $node->getType() == 'article') {
      $variables['content'] = [];
    }
  }
}
  • The above code has some issue in if ($block_id == 'themename_page_title') because there is no variable define for $block_id. So the actual code is if($variables['plugin_id'] == 'themename_page_title'). – Manav Mar 27 '18 at 9:13
1

Using geerlingguy's code as is, you'll get a notice about $block_id variable being undefined. Place $block_id = $variables['elements']['#id']; before the if statement to get rid of the notice.

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.