0

I am trying to get block id in my custom module in build function of class which extends BlockBase.

How can i do this?

i want to use this block_id in another class controller to have configuration of that block so i think best place to get block id and send it to controller class is build function like below code

        class XController extends ControllerBase{
        ...
        $block = \Drupal\block\Entity\Block::load($block_id);
        if ($block) {
        ...

any other suggestion to do this is appreciated.

11
  • Possible duplicate of How can I programmatically display a block?
    – leymannx
    Commented Oct 19, 2018 at 15:20
  • @leymannx that question is when we have block id and we want to display it. but i need that block id and after that i know what i want to do with it.
    – user780
    Commented Oct 19, 2018 at 15:23
  • It's unclear what id you mean, but here drupal.stackexchange.com/questions/228593/… you'll find all id's you can get from a block.
    – 4uk4
    Commented Oct 19, 2018 at 16:31
  • @4k4 thanks. i checked that question. i need $block_id to use it in \Drupal\block\Entity\Block::load($block_id); in a controller . i am trying to get that in build function of an extended BlockBase class and send it to that controller
    – user780
    Commented Oct 19, 2018 at 16:56
  • 2
    The build() function in the block plugin class is unaware of the machine name (block id). There can be multiple blocks configured or no one at all if the plugin is built on the fly in custom code or by a layout module where the block configuration is stored in a different place than a block config entity you load in the question.
    – 4uk4
    Commented Oct 19, 2018 at 18:20

1 Answer 1

1

BlockBase is a plugin, and as such has a getPluginId method. As you need this inside the class itself, it's as easy as:

$id = $this->getPluginId();
6
  • thanks. i wish that Drupal guide was simpler to find this things. I was able to get $id but does 'getpluginid()' return $block_id which i need that $block = \Drupal\block\Entity\Block::load($block_id)? because i tested it and returns null.
    – user780
    Commented Oct 19, 2018 at 15:54
  • maybe i have ask in another question but if i have two instance of block at page getPluginId() returns same id for both of them,. block id shouln't be unique?
    – user780
    Commented Oct 19, 2018 at 16:03
  • What’s the ID you’re expecting to see for each? Maybe you need getDerivativeId instead.
    – Clive
    Commented Oct 19, 2018 at 16:11
  • i don't know format of id but i expect different ids. because different instances have different configuration. i need this ids to load their configuration in another controller with \Drupal\block\Entity\Block::load($block_id).
    – user780
    Commented Oct 19, 2018 at 16:16
  • Did you try the derivative ID?
    – Clive
    Commented Oct 19, 2018 at 16:20

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.