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_provider = "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 // 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));
114 // Make sure we always have a translation set.
115 if ($row->getSourceProperty('tnid') == 0) {
116 $row->setSourceProperty('tnid', $row->getSourceProperty('nid'));
118 return parent::prepareRow($row);
124 public function 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.'),
147 public function getIds() {
148 $ids['nid']['type'] = 'integer';
149 $ids['nid']['alias'] = 'n';
154 * Adapt our query for translations.
156 * @param \Drupal\Core\Database\Query\SelectInterface $query
157 * The generated query.
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');
166 // Translations: Yield only non-default translations.
167 $query->where('n.tnid <> 0 AND n.tnid <> n.nid');