I'd like to be able to print the block id/number of my custom block in its template. I feel like these must exist because Display Suite has a token for it: [block_content:id].

Like a url to edit a block is, it goes to the edit form: block/17

How do I get that 17 in the twig template? Looking in block.html.twig, I don't see anything about a block's number.


You find the block_content entity in the template variable elements to get the ID:


{{ elements.content['#block_content'].id() }}
  • For Drupal 9 it should be {{elements['#id']}}
    – yunzen
    Mar 18 '20 at 12:14
  • @yunzen, the id in the top level of the block render element is more likely the id of the block itself, not of the block content entity the question is about.
    – 4k4
    Mar 18 '20 at 12:32
  • yunzen's answer worked perfectly for me
    – camslice
    Sep 4 at 16:41

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.