How can I add a class attribute to a Drupal 8 block?
There are many examples in Drupal core such as the help modules that sets the role attributes for the block. So following that I can succesfully add a class.
/**
* Implements hook_preprocess_HOOK() for block templates.
*/
function mymodule_preprocess_block(&$variables) {
if ($variables['plugin_id'] == 'mymodule_my_block') {
$variables['attributes']['class'][] = 'my-nice-block';
}
}
Is it possible to add a class without the preprocess hook? Ideally within the block plugin file located in src/Plugin/Block/ ?
This would help keeping all the code related to one block contained in one location.