0

I have created a block plugin.

/src/Plugin/Block/Myblock

/**
 * Provides a 'Download component' block.
 *
 * @Block(
 *   id = "my_custom_block",
 *   admin_label = @Translation("My custom block"),
 * )
 */
class Myblock extends BlockBase {
  // Other code
}

The above block has a settings form also where I am uploading some files to the block.

I placed this block in two different regions on a page with different settings. (Uploaded different files)

Now I want to update the content of one of them i-e add a new file programmatically.

How will I do that? Because I can't load the block and update the field and save the block again because both returns me the same id my_custom_block.

$this->entityTypeManager->getStorage('block_content')->load('my_custom_block')); 

And also this returns me null.

Any idea how will I do that? Just like we update the nod entity, I want to update an instance of this block plugin.

Updated code:

I get the block like this

$node = \Drupal::routeMatch()->getParameter('node');
 if ($node instanceof \Drupal\node\NodeInterface) {
    /** @var \Drupal\layout_builder\Field\LayoutSectionItemList $layout */
    $layout = $node->get('layout_builder__layout');
    $block = $layout->getSection(0)->getComponent('1bb8e7b4-8253-40bc-a1f9-7f02e90edb2f')->getPlugin();
    $block->setConfigurationValue('file', ['6338, 6336']);
 }

Now how will I save the node using the block new configuration values?

I tried this.

 $node->get('layout_builder__layout')->getSection(0)->getComponent('1bb8e7b4-8253-40bc-a1f9-7f02e90edb2f')->setConfiguration($block->getConfiguration());
 $node->save();
1

Placed blocks are stored in the config entity block. The IDs are more or less unpredictable but you can load the blocks by specifying properties:

$entity_type_manager = \Drupal::entityTypeManager();
$blocks = $entity_type_manager->getStorage('block')->loadByProperties([
  'plugin' => 'my_custom_block',
  'region' => $region,
  'theme'  => $theme,
]);

A code example to load all blocks placed for the custom block plugin, do some checks and modify the settings:

$entity_type_manager = \Drupal::entityTypeManager();
$blocks = $entity_type_manager->getStorage('block')->loadByProperties([
  'plugin' => 'my_custom_block',
]);

foreach ($blocks as $block) {
  if ($block->getRegion() == 'my_region') {
    $settings = $block->get('settings');
    // modify settings
    $block->set('settings', $settings);
    $block->save();
  }
}
14
  • How will I get the the region of the block instance. I am using layout builder. – Ahmad Oct 28 '20 at 9:47
  • If you don't know the region when you query the blocks you can check that later. See the second code example. – 4k4 Oct 28 '20 at 9:59
  • Layout modules can store block configuration in different places. If you don't find the placed block with this method I would export the config and search for the plugin id in the config YAML files to locate the config object. – 4k4 Oct 28 '20 at 10:08
  • This will not work if I place two instances in the same region? – Ahmad Oct 28 '20 at 10:21
  • The first loadByProperties() example returns then both instances. – 4k4 Oct 28 '20 at 10:26

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.