View source
<?php
namespace Drupal\node\Plugin\migrate\source\d7;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\migrate\Row;
use Drupal\migrate_drupal\Plugin\migrate\source\d7\FieldableEntity;
use Drupal\Core\Database\Query\SelectInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\State\StateInterface;
use Drupal\migrate\Plugin\MigrationInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Node extends FieldableEntity {
protected $moduleHandler;
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, StateInterface $state, EntityTypeManagerInterface $entity_type_manager, ModuleHandlerInterface $module_handler) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $state, $entity_type_manager);
$this->moduleHandler = $module_handler;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
return new static($configuration, $plugin_id, $plugin_definition, $migration, $container
->get('state'), $container
->get('entity_type.manager'), $container
->get('module_handler'));
}
const JOIN = '[n].[vid] = [nr].[vid]';
public function query() {
$query = $this
->select('node_revision', 'nr')
->fields('n', [
'nid',
'type',
'language',
'status',
'created',
'changed',
'comment',
'promote',
'sticky',
'tnid',
'translate',
])
->fields('nr', [
'vid',
'title',
'log',
'timestamp',
]);
$query
->addField('n', 'uid', 'node_uid');
$query
->addField('nr', 'uid', 'revision_uid');
$query
->innerJoin('node', 'n', static::JOIN);
if ($this->moduleHandler
->moduleExists('content_translation')) {
$query
->leftJoin('node', 'nt', '[n].[tnid] = [nt].[nid]');
$query
->addField('nt', 'language', 'source_langcode');
}
$this
->handleTranslations($query);
if (isset($this->configuration['node_type'])) {
$query
->condition('n.type', (array) $this->configuration['node_type'], 'IN');
}
return $query;
}
public function prepareRow(Row $row) {
$nid = $row
->getSourceProperty('nid');
$vid = $row
->getSourceProperty('vid');
$type = $row
->getSourceProperty('type');
$entity_translatable = $this
->isEntityTranslatable('node') && (int) $this
->variableGet('language_content_type_' . $type, 0) === 4;
$source_language = $this
->getEntityTranslationSourceLanguage('node', $nid);
$language = $entity_translatable && $source_language ? $source_language : $row
->getSourceProperty('language');
if ($row
->getSourceProperty('etr_created')) {
$language = $row
->getSourceProperty('language');
}
foreach ($this
->getFields('node', $type) as $field_name => $field) {
$field_language = $entity_translatable && $field['translatable'] ? $language : NULL;
$row
->setSourceProperty($field_name, $this
->getFieldValues('node', $field_name, $nid, $vid, $field_language));
}
if ($row
->getSourceProperty('tnid') == 0) {
$row
->setSourceProperty('tnid', $row
->getSourceProperty('nid'));
}
if ($this
->moduleExists('title')) {
$title_field = $row
->getSourceProperty('title_field');
if (isset($title_field[0]['value'])) {
$row
->setSourceProperty('title', $title_field[0]['value']);
}
}
return parent::prepareRow($row);
}
public function fields() {
$fields = [
'nid' => $this
->t('Node ID'),
'type' => $this
->t('Type'),
'title' => $this
->t('Title'),
'node_uid' => $this
->t('Node authored by (uid)'),
'revision_uid' => $this
->t('Revision authored by (uid)'),
'created' => $this
->t('Created timestamp'),
'changed' => $this
->t('Modified timestamp'),
'status' => $this
->t('Published'),
'promote' => $this
->t('Promoted to front page'),
'sticky' => $this
->t('Sticky at top of lists'),
'revision' => $this
->t('Create new revision'),
'language' => $this
->t('Language (fr, en, ...)'),
'tnid' => $this
->t('The translation set id for this node'),
'timestamp' => $this
->t('The timestamp the latest revision of this node was created.'),
];
return $fields;
}
public function getIds() {
$ids['nid']['type'] = 'integer';
$ids['nid']['alias'] = 'n';
return $ids;
}
protected function handleTranslations(SelectInterface $query) {
if (empty($this->configuration['translations'])) {
$query
->where('[n].[tnid] = 0 OR [n].[tnid] = [n].[nid]');
}
else {
$query
->where('[n].[tnid] <> 0 AND [n].[tnid] <> [n].[nid]');
}
}
}