Drupal - How to add a class attribute to a block?

Have you tried to just set #attributes on the render array returned by our block plugin?

$build['#attributes']['class'][] = 'my-nice-block';

Looking at BlockViewBuilder::preRender(), that should work.


You can check Block Class module.

Block Class allows users to add classes to any block through the block's configuration interface. By adding a very short snippet of PHP to a theme's block.tpl.php file, classes can be added to the parent element of a block. Hooray for more powerful block theming!


find a twig template name suggestion for your block ( using twig debug comments ) and override it

themes/templates/block--my-custom-block.html.twig

{% extends "block.html.twig" %}
{#
/**
 * @file
 * Theme override for tabs.
 */
#}

{% block title %}
  {{ title_prefix }}
  {% if label %}
    <h2{{ title_attributes.addClass(title_classes, 'accordion') }}{{ title_attributes.setAttribute('id', 'campus') }}>
      {{ label }}
      <i class="fa fa-plus" aria-hidden="true"></i>
    </h2>
  {% endif %}
  {{ title_suffix }}
{% endblock %}

Tags:

8