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();