How to get block id in Drupal 8?

by arnoldo.moen , in category: PHP Frameworks , a year ago

How to get block id in Drupal 8?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by deron , 5 months ago

@arnoldo.moen 

In Drupal 8, you can get the block ID of a block in a few different ways. Here are a couple of options:

  1. If you are working in a template file (e.g. page.html.twig), you can access the block ID by printing the block_id variable. For example:
1
{{ block_id }}


  1. If you are working in a preprocessor function or a custom module, you can get the block ID by using the getPluginId() method on the block object. For example:
1
$block_id = $block->getPluginId();


Keep in mind that this will only work if you already have a reference to the block object. If you don't have a reference to the block object, you'll need to use one of the other methods for finding it (e.g. by using the block plugin manager or the block repository).


I hope this helps! Let me know if you have any questions.