$block = Drupal\block\Entity\Block::load($block_id);
ksm($block);
This code loads only one specific block. But how can I load and display all available blocks?
Using the service plugin.manager.block we will get all blocks available definition.
$blockManager = \Drupal::service('plugin.manager.block');
$contextRepository = \Drupal::service('context.repository');
// Get blocks definition
$definitions = $blockManager->getDefinitionsForContexts($contextRepository->getAvailableContexts());
To get details about one specific block you can use below code.
print_r($definitions['help_block']);
Try this:
$blocks = Drupal\block\Entity\Block::loadMultiple();
ksm($blocks);
The core block module using "getFilteredDefinitions" method to list the blocks
$blockManager = \Drupal::service('plugin.manager.block');
$contextRepository = \Drupal::service('context.repository');
// Get blocks definition
$definitions = $blockManager->getFilteredDefinitions('block_ui', $contextRepository->getAvailableContexts());
print_r($definitions);
getFilteredDefinitions
to return just the content blocks (the ones created from the Blocks UI) and are visible on a given URL or a content type?
Commented
May 18, 2020 at 12:13
$query = \Drupal::entityQuery('block_content');
$ids = $query->execute();
You can also use 'block' instead of 'block_content' to get blocks from code