query_manipulate.php
<?php
namespace Drupal\my_module_migrate\Plugin\migrate\source\d8;
use Drupal\migrate\Row;
/**
* Base class for D8 source plugins to collect field values from Field API.
*
* Available configuration keys:
* - id: (optional) The id of the content which should get migrated. This can
* be useful to only migrate a selected set of nodes. Excepts multiple ids.
*
* @MigrateSource(
* id = "my_module_d8_gallery",
* source_provider = "my_module_migrate"
* )
*/
class MyModuleGallery extends ContentEntity {
/**
* {@inheritdoc}
*/
public function query() {
$query = parent::query();
if (isset($this->configuration['id'])) {
$entityDefinition = $this->entityTypeManager->getDefinition($this->configuration['entity_type']);
$idKey = $entityDefinition->getKey('id');
$query->condition("d.{$idKey}", $this->configuration['id'], 'IN');
}
// Left join to get the nid of the old image node.
$query->leftJoin('paragraph__field_referenzfeld_galerie', 'pfrg', 'pfrg.entity_id = b.id AND pfrg.revision_id = b.revision_id');
$query->leftJoin('node__field_gallery_bild', 'nfgb', 'nfgb.entity_id = pfrg.field_referenzfeld_galerie_target_id');
$query->leftJoin('node__field_base_public_title', 'nfbpt', 'nfbpt.entity_id = pfrg.field_referenzfeld_galerie_target_id');
$query->addExpression('GROUP_CONCAT(nfgb.field_gallery_bild_target_id)', 'gallery_image_ids');
// Add public title field.
$query->addField('nfbpt', 'field_base_public_title_value', 'field_base_public_title_value');
$query->groupBy('b.id');
return $query;
}
/**
* {@inheritdoc}
*/
public function prepareRow(Row $row) {
$return = parent::prepareRow($row);
$nids = [];
$ids_string = $row->getSourceProperty('gallery_image_ids');
if (!empty($ids_string)) {
$ids = explode(',', $ids_string);
foreach ($ids as $id) {
$nids[] = ['nid' => (int) $id];
}
// Add new source property to skip old gallery node within migration.
$row->setSourceProperty('field_referenzfeld_galerie', $nids);
}
$row->setSourceProperty('field_base_public_title', $row->getSourceProperty('field_base_public_title_value'));
return $return;
}
}
query.sql
SELECT
GROUP_CONCAT(nfgb.field_gallery_bild_target_id),
nfbpt.field_base_public_title_value
FROM paragraph__field_referenzfeld_galerie AS pfrg
LEFT JOIN node__field_gallery_bild AS nfgb
ON nfgb.entity_id = pfrg.field_referenzfeld_galerie_target_id
LEFT JOIN node__field_base_public_title AS nfbpt
ON nfbpt.entity_id = pfrg.field_referenzfeld_galerie_target_id
WHERE pfrg.entity_id = 100810 AND pfrg.revision_id = 1919384
10.07.2025 | Lothar Ferreira Neumann