D8: Disable Block Programmatically on Just One View or Node

 

It's not always possible to use a path to determine block visibility.

Therefore, here is how you programmatically disable a block on specific view without recourse to path.

/**
 * Implements hook_preprocess_HOOK().
 */
function tns_breadcrumb_preprocess_page(&$vars) {
  // Remove block from all content that isn't a specific view.
  $view_id = Drupal::request()->attributes->get('view_id');
  if (empty($view_id) || $view_id == 'myviewID') {
    if (isset($vars['page']['myregion']['myblock'])) { // You have to look around and figure out where your block is.
      unset($vars['page']['myregion']['myblock']);
    }
  }
}

If you want to use parameters from the node to determine visibility, do:

$node = Drupal::request()->attributes->get('node');

References:

About the Author

Hi. My name is Jeremiah John. I'm a sf/f writer and activist.

I just completed a dystopian science fiction novel. I run a website which I created that connects farms with churches, mosques, and synagogues to buy fresh vegetables directly and distribute them on a sliding scale to those in need.

In 2003, I spent six months in prison for civil disobedience while working to close the School of the Americas, converting to Christianity, as one does, while I was in the clink.