drupaldrupal-8drupal-themingdrupal-blockscustom-code

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.


Solution

  • 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