3 namespace Drupal\node\Plugin\migrate\source\d7;
5 use Drupal\Core\Extension\ModuleHandlerInterface;
6 use Drupal\migrate\Row;
7 use Drupal\migrate_drupal\Plugin\migrate\source\d7\FieldableEntity;
8 use Drupal\Core\Database\Query\SelectInterface;
9 use Drupal\Core\Entity\EntityManagerInterface;
10 use Drupal\Core\Extension\ModuleHandler;
11 use Drupal\Core\State\StateInterface;
12 use Drupal\migrate\Plugin\MigrationInterface;
13 use Symfony\Component\DependencyInjection\ContainerInterface;
16 * Drupal 7 node source from database.
20 * source_module = "node"
23 class Node extends FieldableEntity {
27 * @var \Drupal\Core\Extension\ModuleHandlerInterface
29 protected $moduleHandler;
34 public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, StateInterface $state, EntityManagerInterface $entity_manager, ModuleHandlerInterface $module_handler) {
35 parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $state, $entity_manager);
36 $this->moduleHandler = $module_handler;
42 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
48 $container->get('state'),
49 $container->get('entity.manager'),
50 $container->get('module_handler')
55 * The join options between the node and the node_revisions table.
57 const JOIN = 'n.vid = nr.vid';
62 public function query() {
63 // Select node in its last revision.
64 $query = $this->select('node_revision', 'nr')
84 $query->addField('n', 'uid', 'node_uid');
85 $query->addField('nr', 'uid', 'revision_uid');
86 $query->innerJoin('node', 'n', static::JOIN);
88 // If the content_translation module is enabled, get the source langcode
89 // to fill the content_translation_source field.
90 if ($this->moduleHandler->moduleExists('content_translation')) {
91 $query->leftJoin('node', 'nt', 'n.tnid = nt.nid');
92 $query->addField('nt', 'language', 'source_langcode');
94 $this->handleTranslations($query);
96 if (isset($this->configuration['node_type'])) {
97 $query->condition('n.type', $this->configuration['node_type']);
106 public function prepareRow(Row $row) {
107 $nid = $row->getSourceProperty('nid');
108 $vid = $row->getSourceProperty('vid');
109 $type = $row->getSourceProperty('type');
111 // If this entity was translated using Entity Translation, we need to get
112 // its source language to get the field values in the right language.
113 // The translations will be migrated by the d7_node_entity_translation
115 $entity_translatable = $this->isEntityTranslatable('node') && (int) $this->variableGet('language_content_type_' . $type, 0) === 4;
116 $source_language = $this->getEntityTranslationSourceLanguage('node', $nid);
117 $language = $entity_translatable && $source_language ? $source_language : $row->getSourceProperty('language');
119 // Get Field API field values.
120 foreach ($this->getFields('node', $type) as $field_name => $field) {
121 // Ensure we're using the right language if the entity and the field are
123 $field_language = $entity_translatable && $field['translatable'] ? $language : NULL;
124 $row->setSourceProperty($field_name, $this->getFieldValues('node', $field_name, $nid, $vid, $field_language));
127 // Make sure we always have a translation set.
128 if ($row->getSourceProperty('tnid') == 0) {
129 $row->setSourceProperty('tnid', $row->getSourceProperty('nid'));
132 // If the node title was replaced by a real field using the Drupal 7 Title
133 // module, use the field value instead of the node title.
134 if ($this->moduleExists('title')) {
135 $title_field = $row->getSourceProperty('title_field');
136 if (isset($title_field[0]['value'])) {
137 $row->setSourceProperty('title', $title_field[0]['value']);
141 return parent::prepareRow($row);
147 public function fields() {
149 'nid' => $this->t('Node ID'),
150 'type' => $this->t('Type'),
151 'title' => $this->t('Title'),
152 'node_uid' => $this->t('Node authored by (uid)'),
153 'revision_uid' => $this->t('Revision authored by (uid)'),
154 'created' => $this->t('Created timestamp'),
155 'changed' => $this->t('Modified timestamp'),
156 'status' => $this->t('Published'),
157 'promote' => $this->t('Promoted to front page'),
158 'sticky' => $this->t('Sticky at top of lists'),
159 'revision' => $this->t('Create new revision'),
160 'language' => $this->t('Language (fr, en, ...)'),
161 'tnid' => $this->t('The translation set id for this node'),
162 'timestamp' => $this->t('The timestamp the latest revision of this node was created.'),
170 public function getIds() {
171 $ids['nid']['type'] = 'integer';
172 $ids['nid']['alias'] = 'n';
177 * Adapt our query for translations.
179 * @param \Drupal\Core\Database\Query\SelectInterface $query
180 * The generated query.
182 protected function handleTranslations(SelectInterface $query) {
183 // Check whether or not we want translations.
184 if (empty($this->configuration['translations'])) {
185 // No translations: Yield untranslated nodes, or default translations.
186 $query->where('n.tnid = 0 OR n.tnid = n.nid');
189 // Translations: Yield only non-default translations.
190 $query->where('n.tnid <> 0 AND n.tnid <> n.nid');