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

Command icon 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

fernly’s picture

Anyone?
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.

fernly’s picture

Issue summary: View changes
fernly’s picture

Version: 8.0.0-beta12 » 8.0.0-rc3

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

Version: 8.0.0-rc3 » 8.0.x-dev

Core issues are now filed against the dev versions where changes will be made. Document the specific release you are using in your issue comment. More information about choosing a version.

Version: 8.0.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Branches prior to 8.8.x are not supported, and Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

scott.whittaker’s picture

This 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!

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

rosemaryreilman’s picture

I'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:

$config = $this->getConfiguration();
$blockId = $config['block_id'];
rbrownell’s picture

I'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?

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

devkinetic’s picture

Version: 9.5.x-dev » 10.1.x-dev

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

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

_randy’s picture

#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

  if(method_exists($form_state->getBuildInfo()['callback_object'], 'getEntity')) {
      // Likely this is the stock drupal block layout config.
      $this->configuration['block_id'] = $form_state->getBuildInfo()['callback_object']->getEntity()->id();
      $this->configuration['block_offset'] = '';
    }
    else {
      /** @var \Drupal\layout_builder\Form\UpdateBlockForm */
      $callback_obj = $form_state->getBuildInfo()['callback_object'];
      // Likely this is Layout builder
      $current_component = $callback_obj->getCurrentComponent();
      $uuid = $current_component->getUuid();
      $region = $current_component->getRegion();
      $weight =  $current_component->getWeight();

      $layout_offset = $weight . '/' . $region;

      $this->configuration['block_id'] = $uuid;
      $this->configuration['block_offset'] = $layout_offset;
    }

quietone’s picture

Status: Active » Closed (works as designed)

I 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

nigelheinsius made their first commit to this issue’s fork.