1

I have created a custom block

/**
 * @Block(
 *   id = "block_text",
 *   admin_label = @Translation("Text"),
 *   category = @Translation("Text"),
 * )
 */

And here is a build() function

/**
 * {@inheritdoc}
 */
public function build() {

  return [
    '#type' => 'markup',
    '#markup' => $render,
    '#cache' => [
      'max-age' => 0
    ],
  ];

}

On the page my block is wrapped into

<div id="block_text"> </div> tags 

Could you please tell me, how can I avoid this wrapping?

  • Which theme do you use? Let's hope it's a sub-theme. Then you simply can copy block.html.twig from the base theme to your sub-theme and remove that <div>. Alternatively you can implement a theme suggestion to pick-up any other custom block template from a custom location, if I remember it right. – leymannx Aug 30 '18 at 8:32
  • How can I tell my class to use a sub-theme? Currently it uses block.html.twig, but by editing this file I destroy other pages. – badm Aug 30 '18 at 8:48
  • Enable Twig debugging to get template suggestion printed into your Markup as HTML comments. Then you will see what other template names you also can use to only override this one specific block. Something like block--machine-name.html.twig. – leymannx Aug 30 '18 at 8:58
1

I think you should copy block.html.twig (perhaps from /core/themes/classy/block/block.html.twig) in your custom module/theme and make it work for your specific block by naming it block--BLOCKID.html.twig and remove the outer <div> from this twig file.

Alternativelly you can create a custom template

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.