<?php
namespace Drupal\paragraphs\Plugin\migrate\source\d7;
use Drupal\Core\Database\DatabaseExceptionWrapper;
use Drupal\migrate\Row;
class ParagraphsItem extends FieldableEntity {
const JOIN = "p.revision_id = pr.revision_id";
const PARENT_FIELD_TABLE_PREFIX = 'field_data_';
public function defaultConfiguration() {
return [
'bundle' => '',
] + parent::defaultConfiguration();
}
public function query() {
$query = $this
->select('paragraphs_item', 'p')
->fields('p', [
'item_id',
'bundle',
'field_name',
'archived',
])
->fields('pr', [
'revision_id',
]);
$query
->innerJoin('paragraphs_item_revision', 'pr', static::JOIN);
if ($this->configuration['bundle']) {
$query
->condition('p.bundle', $this->configuration['bundle']);
}
return $query;
}
public function prepareRow(Row $row) {
[
'item_id' => $paragraph_id,
'revision_id' => $paragraph_revision_id,
'field_name' => $paragraph_parent_field_name,
'bundle' => $bundle,
] = $row
->getSource();
if (!$paragraph_parent_field_name || !is_string($paragraph_parent_field_name)) {
return FALSE;
}
foreach (array_keys($this
->getFields('paragraphs_item', $bundle)) as $field_name) {
$row
->setSourceProperty($field_name, $this
->getFieldValues('paragraphs_item', $field_name, $paragraph_id, $paragraph_revision_id));
}
try {
$parent_data_query = $this
->getDatabase()
->select(static::PARENT_FIELD_TABLE_PREFIX . $paragraph_parent_field_name, 'fd');
$parent_data_query
->addField('fd', 'entity_type', 'parent_type');
$parent_data_query
->addField('fd', 'entity_id', 'parent_id');
$parent_data = $parent_data_query
->condition("fd.{$paragraph_parent_field_name}_value", $paragraph_id)
->condition("fd.{$paragraph_parent_field_name}_revision_id", $paragraph_revision_id)
->execute()
->fetchAssoc();
} catch (DatabaseExceptionWrapper $e) {
return FALSE;
}
if (!is_iterable($parent_data)) {
return FALSE;
}
foreach ($parent_data as $property_name => $property_value) {
$row
->setSourceProperty($property_name, $property_value);
}
return parent::prepareRow($row);
}
public function fields() {
$fields = [
'item_id' => $this
->t('The paragraph_item id'),
'revision_id' => $this
->t('The paragraph_item revision id'),
'bundle' => $this
->t('The paragraph bundle'),
'field_name' => $this
->t('The paragraph field_name'),
];
return $fields;
}
public function getIds() {
return [
'item_id' => [
'type' => 'integer',
'alias' => 'p',
],
];
}
}