Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / taxonomy / src / Plugin / migrate / source / d7 / Term.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  * Taxonomy term source from database.
10  *
11  * @todo Support term_relation, term_synonym table if possible.
12  *
13  * @MigrateSource(
14  *   id = "d7_taxonomy_term",
15  *   source_module = "taxonomy"
16  * )
17  */
18 class Term extends FieldableEntity {
19
20   /**
21    * {@inheritdoc}
22    */
23   public function query() {
24     $query = $this->select('taxonomy_term_data', 'td')
25       ->fields('td')
26       ->distinct()
27       ->orderBy('tid');
28     $query->leftJoin('taxonomy_vocabulary', 'tv', 'td.vid = tv.vid');
29     $query->addField('tv', 'machine_name');
30
31     if (isset($this->configuration['bundle'])) {
32       $query->condition('tv.machine_name', (array) $this->configuration['bundle'], 'IN');
33     }
34
35     return $query;
36   }
37
38   /**
39    * {@inheritdoc}
40    */
41   public function fields() {
42     $fields = [
43       'tid' => $this->t('The term ID.'),
44       'vid' => $this->t('Existing term VID'),
45       'machine_name' => $this->t('Vocabulary machine name'),
46       'name' => $this->t('The name of the term.'),
47       'description' => $this->t('The term description.'),
48       'weight' => $this->t('Weight'),
49       'parent' => $this->t("The Drupal term IDs of the term's parents."),
50       'format' => $this->t("Format of the term description."),
51     ];
52     return $fields;
53   }
54
55   /**
56    * {@inheritdoc}
57    */
58   public function prepareRow(Row $row) {
59     $tid = $row->getSourceProperty('tid');
60     $vocabulary = $row->getSourceProperty('machine_name');
61     $default_language = (array) $this->variableGet('language_default', ['language' => 'en']);
62
63     // If this entity was translated using Entity Translation, we need to get
64     // its source language to get the field values in the right language.
65     // The translations will be migrated by the d7_node_entity_translation
66     // migration.
67     $translatable_vocabularies = array_keys(array_filter($this->variableGet('entity_translation_taxonomy', [])));
68     $entity_translatable = $this->isEntityTranslatable('taxonomy_term') && in_array($vocabulary, $translatable_vocabularies, TRUE);
69     $source_language = $this->getEntityTranslationSourceLanguage('taxonomy_term', $tid);
70     $language = $entity_translatable && $source_language ? $source_language : $default_language['language'];
71     $row->setSourceProperty('language', $language);
72
73     // Get Field API field values.
74     foreach ($this->getFields('taxonomy_term', $vocabulary) as $field_name => $field) {
75       // Ensure we're using the right language if the entity and the field are
76       // translatable.
77       $field_language = $entity_translatable && $field['translatable'] ? $language : NULL;
78       $row->setSourceProperty($field_name, $this->getFieldValues('taxonomy_term', $field_name, $tid, NULL, $field_language));
79     }
80
81     // Find parents for this row.
82     $parents = $this->select('taxonomy_term_hierarchy', 'th')
83       ->fields('th', ['parent', 'tid'])
84       ->condition('tid', $row->getSourceProperty('tid'))
85       ->execute()
86       ->fetchCol();
87     $row->setSourceProperty('parent', $parents);
88
89     // Determine if this is a forum container.
90     $forum_container_tids = $this->variableGet('forum_containers', []);
91     $current_tid = $row->getSourceProperty('tid');
92     $row->setSourceProperty('is_container', in_array($current_tid, $forum_container_tids));
93
94     // If the term name or term description were replaced by real fields using
95     // the Drupal 7 Title module, use the fields value instead of the term name
96     // or term description.
97     if ($this->moduleExists('title')) {
98       $name_field = $row->getSourceProperty('name_field');
99       if (isset($name_field[0]['value'])) {
100         $row->setSourceProperty('name', $name_field[0]['value']);
101       }
102       $description_field = $row->getSourceProperty('description_field');
103       if (isset($description_field[0]['value'])) {
104         $row->setSourceProperty('description', $description_field[0]['value']);
105       }
106       if (isset($description_field[0]['format'])) {
107         $row->setSourceProperty('format', $description_field[0]['format']);
108       }
109     }
110
111     return parent::prepareRow($row);
112   }
113
114   /**
115    * {@inheritdoc}
116    */
117   public function getIds() {
118     $ids['tid']['type'] = 'integer';
119     return $ids;
120   }
121
122 }