
Is it possible to retrieve the unique block id or the machine name in the build() method of a block? When creating multiple instances of the same block definition, I can't manage to get a unique value per block from within the block methods (like for example the ID that gets appended to the machine name, or the machine name itself). I tried out all methods and properties of the BlockBase class, but no results.
Thanks in advance for any help on this.
Issue fork drupal-2540088
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #1
fernly CreditAttribution: fernly as a volunteer commentedAnyone?
It seems like a simple task but I can't make it work. If my question is not clear enough, please say so. I'll try to clear things up then.
Comment #2
fernly CreditAttribution: fernly as a volunteer commentedComment #3
fernly CreditAttribution: fernly as a volunteer commentedStill looking for this one. I'll rephrase the issue in a simpler form: What do you have to do to create a block which prints it's own unique ID as content.
Sounds simple and I'm probably overlooking something.
Comment #6
scott.whittaker CreditAttribution: scott.whittaker commentedThis would be very useful but doesn't seem to be possible for some inexplicable reason. Currently the best I can do is to load all the block instances of the same type and loop through them, looking for a matching label. Which seems incredibly inefficient and will break if two blocks are ever created with the same label which isn't unlikely.
Is there no sensible way to get the ID for a block given a BlockBase class instance? How about getting the Block class object from it? It seems every piece of configuration info for the block is available to BlockBase except a reference to the actual block!
Comment #10
rosemaryreilmanI've been trying to do this as well. One workaround I've found is inside the
blockSubmit
method put the following:$this->configuration['block_id'] = $form_state->getBuildInfo()['callback_object']->getEntity()->id();
Then in the
build
method you can access it via:Comment #11
rbrownellI'm interested in contributing a patch for this but I have found myself chasing ghosts while trying to figure it out. I've explored the BlockManager (for plugins), the BlockBase class and the BlockPluginTrait Is this even possible with Drupal? If so, is there a particular direction or pattern I should be following?
Comment #13
devkinetic CreditAttribution: devkinetic as a volunteer and commentedMoving this up to the next major version, in the meantime, the workaround in #10 does the trick. I will note that blocks need to be resaved before the new info is stored in the the configuration.
Comment #15
_randy CreditAttribution: _randy commented#10 seems to only work for non-Layout Builder instances. Otherwise the callback_object in layout builder does not have a getEntity() method and fails.
Edit:
For those who stumble upon this later, here's a solution that should be augmented for your use case, however, works for mine. This is in my blockSubmit() method
Comment #16
quietone CreditAttribution: quietone at PreviousNext commentedI see that there are two solutions offered here, in #10 and #15. So, I am closing this Support Request and asking that further discussion happen outside the core issue queue.
The Drupal Core issue queue is not the ideal place for support requests (that option is mostly there for filing support issues for contributed modules and themes). There are several support options listed on our support page (Community > Support at the top of Drupal.org) and there is Drupal Slack. Drupal Slack and the Forums, which are our two main support mechanisms in the Drupal community.
Cheers