D7NodeMateriau.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. 'uid',
  62. 'language',
  63. 'status',
  64. 'created',
  65. 'changed',
  66. 'comment',
  67. 'promote',
  68. 'sticky',
  69. 'tnid',
  70. 'translate',
  71. ])
  72. ->fields('nr', [
  73. 'vid',
  74. 'title',
  75. 'log',
  76. 'timestamp',
  77. ])
  78. ->orderBy('changed');
  79. $query->addField('n', 'uid', 'node_uid');
  80. $query->addField('nr', 'uid', 'revision_uid');
  81. $query->innerJoin('node', 'n', static::JOIN);
  82. // If the content_translation module is enabled, get the source langcode
  83. // to fill the content_translation_source field.
  84. if ($this->moduleHandler->moduleExists('content_translation')) {
  85. $query->leftJoin('node', 'nt', 'n.tnid = nt.nid');
  86. $query->addField('nt', 'language', 'source_langcode');
  87. }
  88. $this->handleTranslations($query);
  89. if (isset($this->configuration['node_type'])) {
  90. $query->condition('n.type', $this->configuration['node_type']);
  91. }
  92. return $query;
  93. }
  94. /**
  95. * {@inheritdoc}
  96. */
  97. public function prepareRow(Row $row) {
  98. $nid = $row->getSourceProperty('nid');
  99. $vid = $row->getSourceProperty('vid');
  100. $type = $row->getSourceProperty('type');
  101. $title = $row->getSourceProperty('title');
  102. // drush_print('-- '.$nid."\t".$title);
  103. // If this entity was translated using Entity Translation, we need to get
  104. // its source language to get the field values in the right language.
  105. // The translations will be migrated by the d7_node_entity_translation
  106. // migration.
  107. $entity_translatable = $this->isEntityTranslatable('node') && (int) $this->variableGet('language_content_type_' . $type, 0) === 4;
  108. $language = $entity_translatable ? $this->getEntityTranslationSourceLanguage('node', $nid) : $row->getSourceProperty('language');
  109. // Get Field API field values.
  110. foreach ($this->getFields('node', $type) as $field_name => $field) {
  111. // Ensure we're using the right language if the entity and the field are
  112. // translatable.
  113. $field_language = $entity_translatable && $field['translatable'] ? $language : NULL;
  114. $row->setSourceProperty($field_name, $this->getFieldValues('node', $field_name, $nid, $vid, $field_language));
  115. }
  116. // linked materials
  117. $linked_materials = [];
  118. if(!empty($row->getSourceProperty('field_materiau_ref'))){
  119. // print_r($row->getSourceProperty('field_materiau_ref'));
  120. foreach ($row->getSourceProperty('field_materiau_ref') as $key => $value) {
  121. $linked_materials[] = $value['target_id'];
  122. }
  123. // drush_print("\n".'-- '.$nid."\t".$title);
  124. // drush_print(print_r($linked_materials, true));
  125. }
  126. $row->setSourceProperty('linked_materials', $linked_materials);
  127. //linked breves
  128. $linked_articles = [];
  129. if(!empty($row->getSourceProperty('field_breve_ref'))){
  130. foreach ($row->getSourceProperty('field_breve_ref') as $key => $value) {
  131. $linked_articles[] = $value['target_id'];
  132. }
  133. }
  134. $row->setSourceProperty('linked_articles', $linked_articles);
  135. // print_r($row->getSourceProperty('field_identifiant'));
  136. // print_r($row->getSourceProperty('field_reference_materio'));
  137. // Make sure we always have a translation set.
  138. if ($row->getSourceProperty('tnid') == 0) {
  139. $row->setSourceProperty('tnid', $row->getSourceProperty('nid'));
  140. }
  141. // If the node title was replaced by a real field using the Drupal 7 Title
  142. // module, use the field value instead of the node title.
  143. if ($this->moduleExists('title')) {
  144. $title_field = $row->getSourceProperty('title_field');
  145. if (isset($title_field[0]['value'])) {
  146. $row->setSourceProperty('title', $title_field[0]['value']);
  147. }
  148. }
  149. //location (samples)
  150. $locations = [];
  151. if(!empty($row->getSourceProperty('field_location'))){
  152. foreach ($row->getSourceProperty('field_location') as $key => $value) {
  153. if ($value['location'] == 'no sample') {
  154. $memo .= "#nosample (".$value["showroom_tid"].")\n";
  155. }else{
  156. $locations[] = $value;
  157. }
  158. }
  159. }
  160. $row->setSourceProperty('field_location', $locations);
  161. // reference
  162. if(!empty($row->getSourceProperty('field_reference_materio'))){
  163. $ref = $row->getSourceProperty('field_reference_materio')[0]['value'];
  164. $ref = str_replace('-','',$ref);
  165. $row->setSourceProperty('field_reference_materio', [["value"=>$ref]]);
  166. }
  167. // workflow
  168. $query = $this->select('workflow_node', 'wn');
  169. $query->fields('wn', ['sid']);
  170. $query->condition('wn.nid', $nid);
  171. $results = $query->execute()->fetchField();
  172. if(!$results){
  173. $results = 2;
  174. // add bad workflow to memo field
  175. $memo .= "#migration : invalid workflow\n";
  176. drush_print('WARNING: no workflow');
  177. }
  178. $row->setSourceProperty('workflow', $results);
  179. // drush_print('workflow: '.$results);
  180. // record migration errors in field_memo
  181. if(isset($memo)){
  182. $field_memo = $row->getSourceProperty('field_memo');
  183. $field_memo[0]['value'] .= "\n".$memo;
  184. $row->setSourceProperty('field_memo', $field_memo);
  185. }
  186. return parent::prepareRow($row);
  187. }
  188. /**
  189. * {@inheritdoc}
  190. */
  191. public function fields() {
  192. $fields = [
  193. 'nid' => $this->t('Node ID'),
  194. 'type' => $this->t('Type'),
  195. 'title' => $this->t('Title'),
  196. 'node_uid' => $this->t('Node authored by (uid)'),
  197. 'revision_uid' => $this->t('Revision authored by (uid)'),
  198. 'created' => $this->t('Created timestamp'),
  199. 'changed' => $this->t('Modified timestamp'),
  200. 'status' => $this->t('Published'),
  201. 'promote' => $this->t('Promoted to front page'),
  202. 'sticky' => $this->t('Sticky at top of lists'),
  203. 'revision' => $this->t('Create new revision'),
  204. 'language' => $this->t('Language (fr, en, ...)'),
  205. 'tnid' => $this->t('The translation set id for this node'),
  206. 'timestamp' => $this->t('The timestamp the latest revision of this node was created.'),
  207. ];
  208. return $fields;
  209. }
  210. /**
  211. * {@inheritdoc}
  212. */
  213. public function getIds() {
  214. $ids['nid']['type'] = 'integer';
  215. $ids['nid']['alias'] = 'n';
  216. return $ids;
  217. }
  218. /**
  219. * Adapt our query for translations.
  220. *
  221. * @param \Drupal\Core\Database\Query\SelectInterface $query
  222. * The generated query.
  223. */
  224. protected function handleTranslations(SelectInterface $query) {
  225. // Check whether or not we want translations.
  226. if (empty($this->configuration['translations'])) {
  227. // No translations: Yield untranslated nodes, or default translations.
  228. $query->where('n.tnid = 0 OR n.tnid = n.nid');
  229. }
  230. else {
  231. // Translations: Yield only non-default translations.
  232. $query->where('n.tnid <> 0 AND n.tnid <> n.nid');
  233. }
  234. }
  235. }