93b0fc4aaa45b4d98a8629197de52b0d8f2d9063
[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     // Get Field API field values.
60     foreach (array_keys($this->getFields('taxonomy_term', $row->getSourceProperty('machine_name'))) as $field) {
61       $tid = $row->getSourceProperty('tid');
62       $row->setSourceProperty($field, $this->getFieldValues('taxonomy_term', $field, $tid));
63     }
64
65     // Find parents for this row.
66     $parents = $this->select('taxonomy_term_hierarchy', 'th')
67       ->fields('th', ['parent', 'tid'])
68       ->condition('tid', $row->getSourceProperty('tid'))
69       ->execute()
70       ->fetchCol();
71     $row->setSourceProperty('parent', $parents);
72
73     // Determine if this is a forum container.
74     $forum_container_tids = $this->variableGet('forum_containers', []);
75     $current_tid = $row->getSourceProperty('tid');
76     $row->setSourceProperty('is_container', in_array($current_tid, $forum_container_tids));
77
78     return parent::prepareRow($row);
79   }
80
81   /**
82    * {@inheritdoc}
83    */
84   public function getIds() {
85     $ids['tid']['type'] = 'integer';
86     return $ids;
87   }
88
89 }