ladymcderp
ladymcderp

Reputation: 53

Drupal 8 how to create a template for a custom block type

I want to create a template for a custom block type I've created by going to structure/block layout/ types/ add custom block type. The type I've created is called menu. How do i create a template for this custom block type from here? I'm not as familiar with Drupal so any help will be appreciated.

Upvotes: 3

Views: 4845

Answers (1)

lamp5
lamp5

Reputation: 402

First, add theme suggestion to your theme, paste it into yourthemename.theme file

function yourthemename_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());
  }
}

Now you can use theme suggestion per block bundle, copy block.html.twig to your theme and rename it to: block--bundle--your-block-type.html.twig

Upvotes: 5

Related Questions