How to remove block from the page based on cookie value? We have a block (myblock) that is only displayed on 4/5 content types. When a cookie, mycookie, is set and has a value of allow, I want to load the block on the page, but if the value is not set to allow, then I want to remove it from the page. The cookie is set through a third party Javascript library. 

How to remove block based on cookie value?

Setup:


Under block layout, we load the block on 4/5 content types.
Using twig template (myblock--name.html.twig), we add some css to the myblock
I'll assume this is not a block plugin, right (or at least you dont have access to modify it)? If it is or you can modify it, you should add the logic below to your build() method inside your block class.

Is the cookie you're looking for generated from Drupal? Assuming it is, you should be able to do something like this:

HOOK_preprocess_block__myblock__name(&amp;variables) {
  if (!\Drupal::request()-&gt;cookies-&gt;get('mycookie') == 'allow') {
    $variables = [];
  }
  // Don't forget about cache context for cookie!
  $variables['#cache']['contexts'][] = 'cookies:mycookie'
}