1

Using the block_example module, I created two instances of the example_configurable_text block in the UI and would like to now access their underlying metadata, specifically uuids. By looking at the config table in the database, I can see that there is a record for each (select * from config where name like "%example%";). However, when I debug ExampleConfigurableTextBlock::build, I can't seem to access any of it.

I'm aware that configuration can be loaded via \Drupal::config('<name>'), but I need to be able to access the configuration dynamically from with my plugin class instance.

Is extending BlockBase the right way to go? Do I need to use derivatives? What is the connection between a block instance in the config table and an instance of a block plugin? So many questions.

2

Generate unique form ID based on context contains the answer you're looking for.

Short answer is, you have to make this unique id yourself. See the mentioned simplenews project and how I solved it there.

Keep in mind that block plugins are not only usable by core block entities, they can also be used by Page Manager/Panels, where you have many blocks in a single variant and even by projects like block_field, which allow to use block plugins on content entities. You must not rely on anything but your own configuration array.

I think I once opened a core issue about this, to introduce a method so that a unique ID can bet set on a block plugin, but I can't find it anymore, only another core issue (https://www.drupal.org/node/2405879) where I mentioned that I can't find that issue anymore :)

  • This was what I was suspecting, but I really appreciate the confirmation. Thanks for pointing out the simplenews module. In SimplenewsSubscriptionBlock::build, I noticed you got the form object via $form_object = $this->entityTypeManager->getFormObject('simplenews_subscriber', 'block');. Is there a way to get a form object aside from using an entity? I just have a simple form and block. – cole Feb 14 '17 at 22:45
  • Sure, my answer there actually explains exactly that, either just create an instance yourself and pass in the arguments you need, or use the class resolver as shown there so that dependency injection works through the ::create() method – Berdir Feb 14 '17 at 23:20
  • That's my bad. You were totally clear. I actually figured it out after realizing that FormBuilder::getForm can take any object that implements \Drupal\Core\Form\FormInterface, which is pretty much any form. Thanks! – cole Feb 14 '17 at 23:37
1

When you create an instances of the block you see in the Configure block window a machine name (in the first line to the right of the title). This is the id of the block configuration entity.

In code you can access the block configuration by loading this entity:

$block = Block::load($block_id);

To get all of the configuration use $block->toArray().

By using the method getPlugin() on the block you can get the block plugin:

$plugin = $block->getPlugin();
$configuration = $plugin->getConfiguration();
$plugin_id = $plugin->getPluginId();
$base_plugin_id = $plugin->getBaseId();
$derivative_plugin_id = $plugin->getDerivativeId();

The plugin_id is the base_plugin_id (ID from the block class) + the derivate_plugin_id, for example the menu name or the uuid from a custom block.

If you don't have data in other places like configuration or content you don't need a derivative for the block. You store all information you need in the configuration of the block itself.

  • is it possible to access the block's id dynamically from within the block instance? I'm aware that I can get it manually from a variety of places in the UI, but it would be helpful to do so within my class that extends BlockBase. – cole Feb 14 '17 at 17:33
  • This is kind of a one way relationship: block->plugin. If you are in the plugin you should have all information available. What do you want to achieve? – 4k4 Feb 14 '17 at 17:39
  • I want to put multiple instances of a block on the same page. The block returns a form, and because I want to use ajax for validation and submission, I need a way to differentiate the instances. The config table has uuids, so it'd be great to access this. – cole Feb 14 '17 at 17:43
  • You can't do this from inside a block plugin. You would have to do this what the Block Layout does for the User Interface. Collect the information and create the blocks with Block::create() – 4k4 Feb 14 '17 at 17:50
  • That's helpful to know. I was hoping to use blocks generated in the UI so that they're configurable with some different content in custom fields, but I suppose I could configure them upon creation. Thank you so much for your help. – cole Feb 14 '17 at 18:00

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.