931a2b94311c02f892cac258c7e9a8504b9d5966
[yaffs-website] / web / core / modules / node / src / Plugin / migrate / source / d7 / Node.php
1 <?php
2
3 namespace Drupal\node\Plugin\migrate\source\d7;
4
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;
14
15 /**
16  * Drupal 7 node source from database.
17  *
18  * @MigrateSource(
19  *   id = "d7_node",
20  *   source_module = "node"
21  * )
22  */
23 class Node extends FieldableEntity {
24   /**
25    * The module handler.
26    *
27    * @var \Drupal\Core\Extension\ModuleHandlerInterface
28    */
29   protected $moduleHandler;
30
31   /**
32    * {@inheritdoc}
33    */
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;
37   }
38
39   /**
40    * {@inheritdoc}
41    */
42   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
43     return new static(
44       $configuration,
45       $plugin_id,
46       $plugin_definition,
47       $migration,
48       $container->get('state'),
49       $container->get('entity.manager'),
50       $container->get('module_handler')
51     );
52   }
53
54   /**
55    * The join options between the node and the node_revisions table.
56    */
57   const JOIN = 'n.vid = nr.vid';
58
59   /**
60    * {@inheritdoc}
61    */
62   public function query() {
63     // Select node in its last revision.
64     $query = $this->select('node_revision', 'nr')
65       ->fields('n', [
66         'nid',
67         'type',
68         'language',
69         'status',
70         'created',
71         'changed',
72         'comment',
73         'promote',
74         'sticky',
75         'tnid',
76         'translate',
77       ])
78       ->fields('nr', [
79         'vid',
80         'title',
81         'log',
82         'timestamp',
83       ]);
84     $query->addField('n', 'uid', 'node_uid');
85     $query->addField('nr', 'uid', 'revision_uid');
86     $query->innerJoin('node', 'n', static::JOIN);
87
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');
93     }
94     $this->handleTranslations($query);
95
96     if (isset($this->configuration['node_type'])) {
97       $query->condition('n.type', $this->configuration['node_type']);
98     }
99
100     return $query;
101   }
102
103   /**
104    * {@inheritdoc}
105    */
106   public function prepareRow(Row $row) {
107     $nid = $row->getSourceProperty('nid');
108     $vid = $row->getSourceProperty('vid');
109     $type = $row->getSourceProperty('type');
110
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
114     // migration.
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');
118
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
122       // translatable.
123       $field_language = $entity_translatable && $field['translatable'] ? $language : NULL;
124       $row->setSourceProperty($field_name, $this->getFieldValues('node', $field_name, $nid, $vid, $field_language));
125     }
126
127     // Make sure we always have a translation set.
128     if ($row->getSourceProperty('tnid') == 0) {
129       $row->setSourceProperty('tnid', $row->getSourceProperty('nid'));
130     }
131
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']);
138       }
139     }
140
141     return parent::prepareRow($row);
142   }
143
144   /**
145    * {@inheritdoc}
146    */
147   public function fields() {
148     $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.'),
163     ];
164     return $fields;
165   }
166
167   /**
168    * {@inheritdoc}
169    */
170   public function getIds() {
171     $ids['nid']['type'] = 'integer';
172     $ids['nid']['alias'] = 'n';
173     return $ids;
174   }
175
176   /**
177    * Adapt our query for translations.
178    *
179    * @param \Drupal\Core\Database\Query\SelectInterface $query
180    *   The generated query.
181    */
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');
187     }
188     else {
189       // Translations: Yield only non-default translations.
190       $query->where('n.tnid <> 0 AND n.tnid <> n.nid');
191     }
192   }
193
194 }