Hiding regions for a node page without creating a unique twig file for that node? In Drupal 7 I could hide regions as reactions to certain conditions with Context; Here is a pseudocode to explain what I actually did with this module:


  If $node = 1;
  
  Than programmatically hide $region = sidebar;


In Drupal 8 for some reason, at least in this era, this functionality has been removed from the module. You can no longer hide regions as a reaction to whatever condition.

Hiding regions is still possible with CSS but it should be noted that programmatically hiding regions is better than CSS hiding as it is more swift and sound, as well as more elegant.

What I've tried instead:

Given the fact Context 8 doesn't include that functionality, I tried to use CSS to hide the region in the given node; I knew from Drupal 7 that if you are inside an node and go to the DOM tree, you can see a unique node class that will serve you in such a particular CSS action (hiding a region in a particular node).

In my Drupal 8 site I went to the DOM but didn't quite find such a class (as appears in Drupal 7 sites).

Therefore I tried using what worked for me in Drupal 7 wondering if from any case it just no longer appear in the DOM. I went to my CSS file and added:

.page-node-364 .region-sidebar-first {display: none !important}


It didn't work, nor similar tryings such as:

#node-364 .region-sidebar-first {display: none !important}
.node-364 .region-sidebar-first {display: none !important}
.364 .region-sidebar-first {display: none !important}


And so forth...

Other actions on that node class variations (neglecting region-sidebar-first) also didn't work even with !important declarations on the CSS and what I tried seemed just to be invalid.


I flushed all caches after each try.


Why I'm confused:

What I did saw at DOM and used as a class didn't cause any effect (.364 as mentioned above):





My question:

Due that Context isn't the solution this time, and that seemingly, no usable id or class appears in DOM, I thus ask professional themers here if it is even possible to hide regions for a node in Drupal 8, solely through the CSS, without creating a unique twig file to that particular node.
4k4 suggestion to use the preprocess_page Drupal-function can be done like this:

Preprocess functions go in your themes themeName.theme file, you may need to create this file if your theme does not have one - be sure to clear the Drupal cache after adding or changing code in this file.

function theme-name_preprocess_page(&amp;$variables) {
  // Set the node ID if we're on a node page.
  $nid = isset($variables['node']) ? $variables['node']-&gt;id() : '';

  // Unset a region for one node ID.
  if ($nid == '7') {
    unset($variables['page']['sidebar_second']);
  }

  // Unset a region for multiple node ID's.
  if (in_array($nid, ['1','2','3'])) {
    unset($variables['page']['sidebar_first']);
  }
}


If you would rather use CSS (not a bad technique at all), you can add path classes to body also from theme-name.theme, but this time via the preprocess_html function:

use Drupal\Component\Utility\Html;

function theme-name_preprocess_html(&amp;$variables) {
  // Add path classes to body.
  $path = \Drupal::request()-&gt;getPathInfo();
  $path_args = explode('/', $path);
  if (count($path_args) &gt;= 3) {
    $variables['attributes']['class'][] = 'path-' . Html::cleanCssIdentifier(ltrim($path, '/'));
  }
}