I need to iterate through the article nodes that have enabled the layout builder and replace the content block in the layout builder section with a new content block. I need to do this via migration script. But it's not working as expected.
yaml file
uuid: 599162cc-ba3b-4c4d-98c9-404958545305
langcode: en-gb
status: true
dependencies: { }
_core:
default_config_hash: lzIn3Lnhv-Efq1Lry7b0-99zKIaAiawY_AHBy_OJPxM
id: migrate_node
class: null
field_plugin_method: null
cck_plugin_method: null
migration_tags: null
migration_group: NAS
label: null
source:
plugin: get_article_nodes
key: default
process:
nid: nid
uid: uid
uuid: uuid
title: title
status: status
created: created
changed: changed
block_type_temp:
plugin: migrate_node
source_field: layout_builder_value
layout_builder__layout:
plugin: get
source:
- '@block_type_temp'
destination:
plugin: 'entity:node'
default_bundle: article
migration_dependencies: { }
Source plugin
<?php
namespace Drupal\my_custom_module\Plugin\migrate\source;
use Drupal\Core\Database\Query\SelectInterface;
use Drupal\migrate\Plugin\migrate\source\SqlBase;
use Drupal\migrate\Row;
/**
* Source plugin for article content.
*
* @MigrateSource(
* id = "get_article_nodes"
* )
*/
final class GetArticleNodes extends SqlBase
{
/**
* {@inheritdoc}
*/
public function query(): SelectInterface
{
$query = $this->select('node_field_data', 'n');
$query->fields('n', [
'nid',
'vid',
'type',
'title',
'uid',
'langcode',
'created',
'changed',
'status'
]);
// $query->fields('nlbl', [
// 'layout_builder__layout_section',
// ]);
// $query->leftJoin('node__layout_builder__layout', 'nlbl', 'nlbl.entity_id=n.nid');
$query->condition('n.type', 'article', '=');
return $query;
}
/**
* {@inheritdoc}
*/
public function fields(): array
{
$fields = [
'nid' => $this->t('Node ID'),
'vid' => $this->t('Vid'),
'uid' => $this->t('Uid'),
'title' => $this->t('Title'),
'type' => $this->t('Type'),
'created' => $this->t('Created timestamp'),
'changed' => $this->t('Modified timestamp'),
'status' => $this->t('Published'),
'layout_builder__layout_section' => $this->t('Layout builder section'),
];
return $fields;
}
/**
* {@inheritdoc}
*/
public function getIds(): array
{
return [
'nid' => [
'type' => 'integer',
'alias' => 'n',
],
];
}
/**
* {@inheritdoc}
*/
public function prepareRow(Row $row): bool
{
$nid = $row->getSourceProperty('nid');
$row->setSourceProperty('nid', $nid);
$result = $this->getDatabase()->query(
"SELECT bundle , entity_id, layout_builder__layout_section FROM node__layout_builder__layout
WHERE entity_id = $nid"
);
$layoutbuilder_data = [];
foreach($result as $record ){
$layoutbuilder_data[] = $record->layout_builder__layout_section;
}
$row->setSourceProperty('layout_builder_value', $layoutbuilder_data);
return parent::prepareRow($row);
}
}
Process plugin
<?php
namespace Drupal\my_custom_module\Plugin\migrate\process;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\Row;
use Drupal\node\Entity\Node;
use Drupal\layout_builder\Section;
use Drupal\layout_builder\SectionComponent;
use Drupal\migrate\MigrateException;
use Drupal\my_custom_module\LayoutBase;
use Drupal\my_custom_module\LayoutMigrationItem;
use Drupal\my_custom_module\LayoutMigrationMissingBlockException;
/**
* Custom process plugin for updating layout builder section.
*
* @MigrateProcessPlugin(
* id = "migrate_node"
* )
*/
class MigrateNode extends LayoutBase{
/**
* {@inheritdoc}
*/
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property)
{
$nid = $row->getSourceProperty('nid');
if (!isset($this->configuration['source_field'])) {
throw new MigrateException('Missing source_field for layout builder process plugin.');
}
if($nid == '1429'){ // testing for a single node
$values = $row->getSourceProperty($this->configuration['source_field']);
if(isset($values) && is_array($values)){
foreach($values as $key => $value){
$sectionComponents = unserialize($value);// am geting section components here
if ($sectionComponents instanceof Section) {
$components = $sectionComponents->getComponents();
foreach ($components as $key => $component) {
print_r($component->get('configuration'));
// $sectionComponents->insertComponent(1, $component);
$configurations = $component->get('configuration');
$configurations['id'] = "block_content:3208e4a9-24a2-489c-a0c2-122bcc9c5395"; // this is not working :(
$component->setConfiguration($configurations);
}
}
}
}
}
return $value;
}
}
Can somebody help me pls?