0

How to update a component in the node's layout builder? I have used the following code.

$node = \Drupal\node\Entity\Node::load($nid);
$layout = $node->get('layout_builder__layout');
$sections = $layout->getSections();
foreach ($sections as $key => $section) {
      $components = $section->getComponents();
      foreach ($components as $component) {
        if ($component instanceof SectionComponent && $component->getPluginId() === 'myplugin_id') {
          $configurations = $component->get('configuration');
          $configurations['label_display'] = "0";
          $component->setConfiguration($configurations);
        }
      }
}

But now I don't know how to update the section and layout of the node.

Because $section->setComponent($component) is a protected method.

1

I have done it like this.

$node = \Drupal\node\Entity\Node::load($nid);
$layout = $node->get('layout_builder__layout')->getValue();
$sections = $layout->getSections();

foreach ($sections as $key => $section) {
  $components = $section->getComponents();
  foreach ($components as $component) {
    if ($component instanceof SectionComponent && $component->getPluginId() === 'myplugin_id') {
       $configurations = $component->get('configuration');
       $configurations['label_display'] = "0";
       $component->setConfiguration($configurations);
    }
  }
}

$node->set('layout_builder__layout', $layout);
$node->save();

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.