adrian-d of Rouge Devs
1/11/2017 - 1:35 PM

Drupal 8 - templating custom block types

Drupal 8 - templating custom block types

# Some pre-processing is required in order to have this work. Block type templates aren't currently available in the default theming layer, but a light function will take care of this.

function <THEME NAME>_theme_suggestions_block_alter(array &$suggestions, array $variables) {
  // Block suggestions for custom block bundles.
  if (isset($variables['elements']['content']['#block_content'])) {
    array_splice($suggestions, 1, 0, 'block__bundle__' . $variables['elements']['content']['#block_content']->bundle());

# The result of this is that templates are now readily available. If you have theme_debug enabled or are working with Devel, you will be able to check the code comments to know the template name you will require next.

# I created a custom block type of 'custom_block_type', the template file I was able to use was:


# The base content for this template is of course within the core modules directory.


# This template can easily be extended to provide support for an individual field. For example, I created a field which has a machine name of 'field_email'.

{% block content %}
  <div{{ content_attributes.addClass('content') }}>
    {{ content.field_email }}
{% endblock %}

# If you need to further amend the layout or base HTML behind this field, you can then drill down further into the template for that specific field and render its output as you want, or use the View configuration to see if you can get what you need from there.