D7NodeMateriauI18n.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace Drupal\materio_migrate\Plugin\migrate\source;
  3. use Drupal\migrate\Row;
  4. use Drupal\migrate_drupal\Plugin\migrate\source\d7\FieldableEntity;
  5. /**
  6. * Provides Drupal 7 node entity translations source plugin.
  7. *
  8. * @MigrateSource(
  9. * id = "d7_node_materiau_i18n",
  10. * source_module = "entity_translation"
  11. * )
  12. */
  13. class D7NodeMateriauI18n extends FieldableEntity {
  14. /**
  15. * {@inheritdoc}
  16. */
  17. public function query() {
  18. $query = $this->select('entity_translation', 'et')
  19. ->fields('et', [
  20. 'entity_id',
  21. 'revision_id',
  22. 'language',
  23. 'source',
  24. 'uid',
  25. 'status',
  26. 'created',
  27. 'changed',
  28. ])
  29. ->fields('n', [
  30. 'nid',
  31. 'title',
  32. 'type',
  33. 'promote',
  34. 'sticky',
  35. ])
  36. ->fields('nr', [
  37. 'log',
  38. 'timestamp',
  39. ])
  40. ->condition('et.entity_type', 'node')
  41. ->condition('et.source', '', '<>');
  42. $query->addField('nr', 'uid', 'revision_uid');
  43. $query->innerJoin('node', 'n', 'n.nid = et.entity_id');
  44. $query->innerJoin('node_revision', 'nr', 'nr.vid = et.revision_id');
  45. if (isset($this->configuration['node_type'])) {
  46. $query->condition('n.type', $this->configuration['node_type']);
  47. }
  48. return $query;
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function prepareRow(Row $row) {
  54. $nid = $row->getSourceProperty('entity_id');
  55. $vid = $row->getSourceProperty('revision_id');
  56. $type = $row->getSourceProperty('type');
  57. $language = $row->getSourceProperty('language');
  58. $title = $row->getSourceProperty('title');
  59. // drush_print('-- '.$nid."\t".$title."\t".$language);
  60. // Get Field API field values.
  61. foreach ($this->getFields('node', $type) as $field_name => $field) {
  62. // Ensure we're using the right language if the entity is translatable.
  63. $field_language = $field['translatable'] ? $language : NULL;
  64. $row->setSourceProperty($field_name, $this->getFieldValues('node', $field_name, $nid, $vid, $field_language));
  65. }
  66. // If the node title was replaced by a real field using the Drupal 7 Title
  67. // module, use the field value instead of the node title.
  68. if ($this->moduleExists('title')) {
  69. $title_field = $row->getSourceProperty('title_field');
  70. if (isset($title_field[0]['value'])) {
  71. $row->setSourceProperty('title', $title_field[0]['value']);
  72. }
  73. }
  74. return parent::prepareRow($row);
  75. }
  76. /**
  77. * {@inheritdoc}
  78. */
  79. public function fields() {
  80. return [
  81. 'entity_id' => $this->t('Entity ID'),
  82. 'revision_id' => $this->t('Revision ID'),
  83. 'language' => $this->t('Node translation language'),
  84. 'source' => $this->t('Node translation source language'),
  85. 'uid' => $this->t('Node translation authored by (uid)'),
  86. 'status' => $this->t('Published'),
  87. 'created' => $this->t('Created timestamp'),
  88. 'changed' => $this->t('Modified timestamp'),
  89. 'title' => $this->t('Node title'),
  90. 'type' => $this->t('Node type'),
  91. 'promote' => $this->t('Promoted to front page'),
  92. 'sticky' => $this->t('Sticky at top of lists'),
  93. 'log' => $this->t('Revision log'),
  94. 'timestamp' => $this->t('The timestamp the latest revision of this node was created.'),
  95. 'revision_uid' => $this->t('Revision authored by (uid)'),
  96. ];
  97. }
  98. /**
  99. * {@inheritdoc}
  100. */
  101. public function getIds() {
  102. return [
  103. 'entity_id' => [
  104. 'type' => 'integer',
  105. 'alias' => 'et',
  106. ],
  107. 'language' => [
  108. 'type' => 'string',
  109. 'alias' => 'et',
  110. ],
  111. ];
  112. }
  113. }