1a9037e7a00a4f0b6772815117cd6ad0bb96a800
[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     // Get Field API field values.
108     foreach (array_keys($this->getFields('node', $row->getSourceProperty('type'))) as $field) {
109       $nid = $row->getSourceProperty('nid');
110       $vid = $row->getSourceProperty('vid');
111       $row->setSourceProperty($field, $this->getFieldValues('node', $field, $nid, $vid));
112     }
113
114     // Make sure we always have a translation set.
115     if ($row->getSourceProperty('tnid') == 0) {
116       $row->setSourceProperty('tnid', $row->getSourceProperty('nid'));
117     }
118     return parent::prepareRow($row);
119   }
120
121   /**
122    * {@inheritdoc}
123    */
124   public function fields() {
125     $fields = [
126       'nid' => $this->t('Node ID'),
127       'type' => $this->t('Type'),
128       'title' => $this->t('Title'),
129       'node_uid' => $this->t('Node authored by (uid)'),
130       'revision_uid' => $this->t('Revision authored by (uid)'),
131       'created' => $this->t('Created timestamp'),
132       'changed' => $this->t('Modified timestamp'),
133       'status' => $this->t('Published'),
134       'promote' => $this->t('Promoted to front page'),
135       'sticky' => $this->t('Sticky at top of lists'),
136       'revision' => $this->t('Create new revision'),
137       'language' => $this->t('Language (fr, en, ...)'),
138       'tnid' => $this->t('The translation set id for this node'),
139       'timestamp' => $this->t('The timestamp the latest revision of this node was created.'),
140     ];
141     return $fields;
142   }
143
144   /**
145    * {@inheritdoc}
146    */
147   public function getIds() {
148     $ids['nid']['type'] = 'integer';
149     $ids['nid']['alias'] = 'n';
150     return $ids;
151   }
152
153   /**
154    * Adapt our query for translations.
155    *
156    * @param \Drupal\Core\Database\Query\SelectInterface $query
157    *   The generated query.
158    */
159   protected function handleTranslations(SelectInterface $query) {
160     // Check whether or not we want translations.
161     if (empty($this->configuration['translations'])) {
162       // No translations: Yield untranslated nodes, or default translations.
163       $query->where('n.tnid = 0 OR n.tnid = n.nid');
164     }
165     else {
166       // Translations: Yield only non-default translations.
167       $query->where('n.tnid <> 0 AND n.tnid <> n.nid');
168     }
169   }
170
171 }