1

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

1 Answer 1

2

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 ...
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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