D7NodeMateriau.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. // linked materials
  116. $linked_materials = [];
  117. if(!empty($row->getSourceProperty('field_materiau_ref'))){
  118. // print_r($row->getSourceProperty('field_materiau_ref'));
  119. foreach ($row->getSourceProperty('field_materiau_ref') as $key => $value) {
  120. $linked_materials[] = $value['target_id'];
  121. }
  122. // print_r($linked_materials);
  123. }
  124. $row->setSourceProperty('linked_materials', $linked_materials);
  125. //linked breves
  126. $linked_breves = [];
  127. if(!empty($row->getSourceProperty('field_breve_ref'))){
  128. foreach ($row->getSourceProperty('field_breve_ref') as $key => $value) {
  129. $linked_breves[] = $value['target_id'];
  130. }
  131. }
  132. $row->setSourceProperty('linked_breves', $linked_breves);
  133. // print_r($row->getSourceProperty('field_identifiant'));
  134. // print_r($row->getSourceProperty('field_reference_materio'));
  135. // Make sure we always have a translation set.
  136. if ($row->getSourceProperty('tnid') == 0) {
  137. $row->setSourceProperty('tnid', $row->getSourceProperty('nid'));
  138. }
  139. // If the node title was replaced by a real field using the Drupal 7 Title
  140. // module, use the field value instead of the node title.
  141. if ($this->moduleExists('title')) {
  142. $title_field = $row->getSourceProperty('title_field');
  143. if (isset($title_field[0]['value'])) {
  144. $row->setSourceProperty('title', $title_field[0]['value']);
  145. }
  146. }
  147. // workflow
  148. $query = $this->select('workflow_node', 'wn');
  149. $query->fields('wn', ['sid']);
  150. $query->condition('wn.nid', $nid);
  151. $results = $query->execute()->fetchField();
  152. if(!$results){
  153. $results = 2;
  154. // add bad workflow to memo field
  155. $memo .= "#migration : invalid workflow\n";
  156. drush_print('WARNING: no workflow');
  157. }
  158. $row->setSourceProperty('workflow', $results);
  159. // record migration errors in field_memo
  160. if(isset($memo)){
  161. $field_memo = $row->getSourceProperty('field_memo');
  162. $field_memo[0]['value'] .= "\n".$memo;
  163. $row->setSourceProperty('field_memo', $field_memo);
  164. }
  165. return parent::prepareRow($row);
  166. }
  167. /**
  168. * {@inheritdoc}
  169. */
  170. public function fields() {
  171. $fields = [
  172. 'nid' => $this->t('Node ID'),
  173. 'type' => $this->t('Type'),
  174. 'title' => $this->t('Title'),
  175. 'node_uid' => $this->t('Node authored by (uid)'),
  176. 'revision_uid' => $this->t('Revision authored by (uid)'),
  177. 'created' => $this->t('Created timestamp'),
  178. 'changed' => $this->t('Modified timestamp'),
  179. 'status' => $this->t('Published'),
  180. 'promote' => $this->t('Promoted to front page'),
  181. 'sticky' => $this->t('Sticky at top of lists'),
  182. 'revision' => $this->t('Create new revision'),
  183. 'language' => $this->t('Language (fr, en, ...)'),
  184. 'tnid' => $this->t('The translation set id for this node'),
  185. 'timestamp' => $this->t('The timestamp the latest revision of this node was created.'),
  186. ];
  187. return $fields;
  188. }
  189. /**
  190. * {@inheritdoc}
  191. */
  192. public function getIds() {
  193. $ids['nid']['type'] = 'integer';
  194. $ids['nid']['alias'] = 'n';
  195. return $ids;
  196. }
  197. /**
  198. * Adapt our query for translations.
  199. *
  200. * @param \Drupal\Core\Database\Query\SelectInterface $query
  201. * The generated query.
  202. */
  203. protected function handleTranslations(SelectInterface $query) {
  204. // Check whether or not we want translations.
  205. if (empty($this->configuration['translations'])) {
  206. // No translations: Yield untranslated nodes, or default translations.
  207. $query->where('n.tnid = 0 OR n.tnid = n.nid');
  208. }
  209. else {
  210. // Translations: Yield only non-default translations.
  211. $query->where('n.tnid <> 0 AND n.tnid <> n.nid');
  212. }
  213. }
  214. }