1

In Drupal, there are two different kinds of blocks: content blocks and plugin blocks.

We get content blocks like this:

\Drupal\block\Entity\Block::load('block_id');

and plugin blocks like this:

\Drupal::service('plugin.manager.block')->createInstance(
    'plugin_id',
    $config
);

I have a scenario where I don't know whether the block I'm looking for is a content or a plugin. I simply want to obtain the block given its id.

Of course, I can first try one version, test if I found something, an then try the other version.

But the problem to obtain a block by its id should be very common. So, I would expect that there's some builtin Drupal function that I should use. Which one?

1
  • 1
    The first line gets not a block content entity, this is a configured block which contains the plugin id. So you could query this entity by the plugin id to get all blocks configured for this block plugin. Don't know how the second example to create a plugin instance is connected. It might help if you provide an example what "block id" you are really looking for.
    – 4k4
    Jul 31 '18 at 13:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.