2

In Drupal 8 I'm struggling to remove classes from a given block. Here's a quick snippet from a preprocess function:

function mytheme_preprocess_block(&$vars) {
  $block_classes = array(
    'block_id' => array('custom-class'),
  );
  $id = $vars['elements']['#id'];
  if (isset($block_classes[$id])) {
    $vars['attributes']['class'] = $block_classes[$id];
  }
  return $vars;
}

Interestingly this works, and adds custom-class to the block, but even though it is overriding the existing class array, all the default Drupal classes are still added (later on maybe?). I know that I can remove classes in twig with {{ attributes.removeClass('class-to-remove') }}, but ideally I would like to have a preprocess function that doesn't require exposing more template files.

2

You can't do that. You can see node and file template. Unless, in your theme you set

base theme: stable 

or when you don't define a base theme.

  • Correct answer but the reference should be to the block template, not node or file. But as you can see in block.html.twig as well, the default classes are added there. The focus to make it easier for themers to change class. Which made it impossible for preprocess/modules to prevent them. – Berdir Jul 14 '16 at 17:47

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.