Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / taxonomy / src / Plugin / migrate / source / d7 / TermEntityTranslation.php
1 <?php
2
3 namespace Drupal\taxonomy\Plugin\migrate\source\d7;
4
5 use Drupal\migrate\Row;
6 use Drupal\migrate_drupal\Plugin\migrate\source\d7\FieldableEntity;
7
8 /**
9  * Provides Drupal 7 taxonomy term entity translation source plugin.
10  *
11  * @MigrateSource(
12  *   id = "d7_taxonomy_term_entity_translation",
13  *   source_module = "entity_translation"
14  * )
15  */
16 class TermEntityTranslation extends FieldableEntity {
17
18   /**
19    * {@inheritdoc}
20    */
21   public function query() {
22     $query = $this->select('entity_translation', 'et')
23       ->fields('et')
24       ->fields('td', [
25         'name',
26         'description',
27         'format',
28       ])
29       ->fields('tv', [
30         'machine_name',
31       ])
32       ->condition('et.entity_type', 'taxonomy_term')
33       ->condition('et.source', '', '<>');
34
35     $query->innerJoin('taxonomy_term_data', 'td', 'td.tid = et.entity_id');
36     $query->innerJoin('taxonomy_vocabulary', 'tv', 'td.vid = tv.vid');
37
38     if (isset($this->configuration['bundle'])) {
39       $query->condition('tv.machine_name', (array) $this->configuration['bundle'], 'IN');
40     }
41
42     return $query;
43   }
44
45   /**
46    * {@inheritdoc}
47    */
48   public function prepareRow(Row $row) {
49     $tid = $row->getSourceProperty('entity_id');
50     $vocabulary = $row->getSourceProperty('machine_name');
51     $language = $row->getSourceProperty('language');
52
53     // Get Field API field values.
54     foreach ($this->getFields('taxonomy_term', $vocabulary) as $field_name => $field) {
55       // Ensure we're using the right language if the entity is translatable.
56       $field_language = $field['translatable'] ? $language : NULL;
57       $row->setSourceProperty($field_name, $this->getFieldValues('taxonomy_term', $field_name, $tid, NULL, $field_language));
58     }
59
60     // If the term name or term description were replaced by real fields using
61     // the Drupal 7 Title module, use the fields value instead of the term name
62     // or term description.
63     if ($this->moduleExists('title')) {
64       $name_field = $row->getSourceProperty('name_field');
65       if (isset($name_field[0]['value'])) {
66         $row->setSourceProperty('name', $name_field[0]['value']);
67       }
68       $description_field = $row->getSourceProperty('description_field');
69       if (isset($description_field[0]['value'])) {
70         $row->setSourceProperty('description', $description_field[0]['value']);
71       }
72       if (isset($description_field[0]['format'])) {
73         $row->setSourceProperty('format', $description_field[0]['format']);
74       }
75     }
76
77     // Determine if this is a forum container.
78     $forum_container_tids = $this->variableGet('forum_containers', []);
79     $row->setSourceProperty('is_container', in_array($tid, $forum_container_tids));
80
81     return parent::prepareRow($row);
82   }
83
84   /**
85    * {@inheritdoc}
86    */
87   public function fields() {
88     return [
89       'entity_type' => $this->t('The entity type this translation relates to'),
90       'entity_id' => $this->t('The entity ID this translation relates to'),
91       'revision_id' => $this->t('The entity revision ID this translation relates to'),
92       'language' => $this->t('The target language for this translation.'),
93       'source' => $this->t('The source language from which this translation was created.'),
94       'uid' => $this->t('The author of this translation.'),
95       'status' => $this->t('Boolean indicating whether the translation is published (visible to non-administrators).'),
96       'translate' => $this->t('A boolean indicating whether this translation needs to be updated.'),
97       'created' => $this->t('The Unix timestamp when the translation was created.'),
98       'changed' => $this->t('The Unix timestamp when the translation was most recently saved.'),
99       'name' => $this->t('The name of the term.'),
100       'description' => $this->t('The term description.'),
101       'format' => $this->t('Format of the term description.'),
102       'machine_name' => $this->t('Vocabulary machine name'),
103     ];
104   }
105
106   /**
107    * {@inheritdoc}
108    */
109   public function getIds() {
110     return [
111       'entity_id' => [
112         'type' => 'integer',
113         'alias' => 'et',
114       ],
115       'language' => [
116         'type' => 'string',
117         'alias' => 'et',
118       ],
119     ];
120   }
121
122 }