D7NodeMateriau.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. namespace Drupal\materio_migrate\Plugin\migrate\source;
  3. use Drupal\Core\Extension\ModuleHandlerInterface;
  4. use Drupal\migrate\Row;
  5. use Drupal\migrate_drupal\Plugin\migrate\source\d7\FieldableEntity;
  6. use Drupal\Core\Database\Query\SelectInterface;
  7. use Drupal\Core\Entity\EntityManagerInterface;
  8. use Drupal\Core\Extension\ModuleHandler;
  9. use Drupal\Core\State\StateInterface;
  10. use Drupal\migrate\Plugin\MigrationInterface;
  11. use Symfony\Component\DependencyInjection\ContainerInterface;
  12. /**
  13. * Drupal 7 node source from database.
  14. *
  15. * @MigrateSource(
  16. * id = "d7_node_materiau",
  17. * source_module = "node"
  18. * )
  19. */
  20. class D7NodeMateriau extends FieldableEntity {
  21. /**
  22. * The module handler.
  23. *
  24. * @var \Drupal\Core\Extension\ModuleHandlerInterface
  25. */
  26. protected $moduleHandler;
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, StateInterface $state, EntityManagerInterface $entity_manager, ModuleHandlerInterface $module_handler) {
  31. parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $state, $entity_manager);
  32. $this->moduleHandler = $module_handler;
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
  38. return new static(
  39. $configuration,
  40. $plugin_id,
  41. $plugin_definition,
  42. $migration,
  43. $container->get('state'),
  44. $container->get('entity.manager'),
  45. $container->get('module_handler')
  46. );
  47. }
  48. /**
  49. * The join options between the node and the node_revisions table.
  50. */
  51. const JOIN = 'n.vid = nr.vid';
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function query() {
  56. // Select node in its last revision.
  57. $query = $this->select('node_revision', 'nr')
  58. ->fields('n', [
  59. 'nid',
  60. 'type',
  61. 'language',
  62. 'status',
  63. 'created',
  64. 'changed',
  65. 'comment',
  66. 'promote',
  67. 'sticky',
  68. 'tnid',
  69. 'translate',
  70. ])
  71. ->fields('nr', [
  72. 'vid',
  73. 'title',
  74. 'log',
  75. 'timestamp',
  76. ])
  77. ->orderBy('nid');
  78. $query->addField('n', 'uid', 'node_uid');
  79. $query->addField('nr', 'uid', 'revision_uid');
  80. $query->innerJoin('node', 'n', static::JOIN);
  81. // If the content_translation module is enabled, get the source langcode
  82. // to fill the content_translation_source field.
  83. if ($this->moduleHandler->moduleExists('content_translation')) {
  84. $query->leftJoin('node', 'nt', 'n.tnid = nt.nid');
  85. $query->addField('nt', 'language', 'source_langcode');
  86. }
  87. $this->handleTranslations($query);
  88. if (isset($this->configuration['node_type'])) {
  89. $query->condition('n.type', $this->configuration['node_type']);
  90. }
  91. return $query;
  92. }
  93. /**
  94. * {@inheritdoc}
  95. */
  96. public function prepareRow(Row $row) {
  97. $nid = $row->getSourceProperty('nid');
  98. $vid = $row->getSourceProperty('vid');
  99. $type = $row->getSourceProperty('type');
  100. $title = $row->getSourceProperty('title');
  101. drush_print('-- '.$nid."\t".$title);
  102. // If this entity was translated using Entity Translation, we need to get
  103. // its source language to get the field values in the right language.
  104. // The translations will be migrated by the d7_node_entity_translation
  105. // migration.
  106. $entity_translatable = $this->isEntityTranslatable('node') && (int) $this->variableGet('language_content_type_' . $type, 0) === 4;
  107. $language = $entity_translatable ? $this->getEntityTranslationSourceLanguage('node', $nid) : $row->getSourceProperty('language');
  108. // Get Field API field values.
  109. foreach ($this->getFields('node', $type) as $field_name => $field) {
  110. // Ensure we're using the right language if the entity and the field are
  111. // translatable.
  112. $field_language = $entity_translatable && $field['translatable'] ? $language : NULL;
  113. $row->setSourceProperty($field_name, $this->getFieldValues('node', $field_name, $nid, $vid, $field_language));
  114. }
  115. if(!empty($row->getSourceProperty('field_video_filter'))){
  116. print_r($row->getSourceProperty('field_video_filter'));
  117. }
  118. // Make sure we always have a translation set.
  119. if ($row->getSourceProperty('tnid') == 0) {
  120. $row->setSourceProperty('tnid', $row->getSourceProperty('nid'));
  121. }
  122. // If the node title was replaced by a real field using the Drupal 7 Title
  123. // module, use the field value instead of the node title.
  124. if ($this->moduleExists('title')) {
  125. $title_field = $row->getSourceProperty('title_field');
  126. if (isset($title_field[0]['value'])) {
  127. $row->setSourceProperty('title', $title_field[0]['value']);
  128. }
  129. }
  130. // workflow
  131. $query = $this->select('workflow_node', 'wn');
  132. $query->fields('wn', ['sid']);
  133. $query->condition('wn.nid', $nid);
  134. $results = $query->execute()->fetchField();
  135. if(!$results){
  136. $results = 2;
  137. // add bad workflow to memo field
  138. $memo .= "#migration : invalid workflow\n";
  139. drush_print('WARNING: no workflow');
  140. }
  141. $row->setSourceProperty('workflow', $results);
  142. // record migration errors in field_memo
  143. if(isset($memo)){
  144. $field_memo = $row->getSourceProperty('field_memo');
  145. $field_memo[0]['value'] .= "\n".$memo;
  146. $row->setSourceProperty('field_memo', $field_memo);
  147. }
  148. return parent::prepareRow($row);
  149. }
  150. /**
  151. * {@inheritdoc}
  152. */
  153. public function fields() {
  154. $fields = [
  155. 'nid' => $this->t('Node ID'),
  156. 'type' => $this->t('Type'),
  157. 'title' => $this->t('Title'),
  158. 'node_uid' => $this->t('Node authored by (uid)'),
  159. 'revision_uid' => $this->t('Revision authored by (uid)'),
  160. 'created' => $this->t('Created timestamp'),
  161. 'changed' => $this->t('Modified timestamp'),
  162. 'status' => $this->t('Published'),
  163. 'promote' => $this->t('Promoted to front page'),
  164. 'sticky' => $this->t('Sticky at top of lists'),
  165. 'revision' => $this->t('Create new revision'),
  166. 'language' => $this->t('Language (fr, en, ...)'),
  167. 'tnid' => $this->t('The translation set id for this node'),
  168. 'timestamp' => $this->t('The timestamp the latest revision of this node was created.'),
  169. ];
  170. return $fields;
  171. }
  172. /**
  173. * {@inheritdoc}
  174. */
  175. public function getIds() {
  176. $ids['nid']['type'] = 'integer';
  177. $ids['nid']['alias'] = 'n';
  178. return $ids;
  179. }
  180. /**
  181. * Adapt our query for translations.
  182. *
  183. * @param \Drupal\Core\Database\Query\SelectInterface $query
  184. * The generated query.
  185. */
  186. protected function handleTranslations(SelectInterface $query) {
  187. // Check whether or not we want translations.
  188. if (empty($this->configuration['translations'])) {
  189. // No translations: Yield untranslated nodes, or default translations.
  190. $query->where('n.tnid = 0 OR n.tnid = n.nid');
  191. }
  192. else {
  193. // Translations: Yield only non-default translations.
  194. $query->where('n.tnid <> 0 AND n.tnid <> n.nid');
  195. }
  196. }
  197. }