How can I get the numeric ID for a block from within the block template or hook_preprocess_block().
By numeric ID, I mean the ID that we see in the edit link for a block. For example, mysite.com/admin/content/block/123
Drupal version: 10
You can check yourself with Xdebug or Kint.
/**
* Implements hook_preprocess_HOOK().
*/
function MYMODULE_preprocess_block(&$variables) {
// Check if it's a content block.
if ($variables['elements']['#base_plugin_id'] === 'block_content') {
/** @var \Drupal\block_content\Entity\BlockContent $block_content */
$block_content = $variables['content']['#block_content'];
// Get the numeric ID.
$id = $block_content->id();
// Code on ...
}
}