D7NodeBreveI18n.php 3.6 KB

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