a749f7107c555808c00ca36f88770d83d8ab14b0
[yaffs-website] / web / core / modules / comment / src / Plugin / migrate / source / d7 / CommentEntityTranslation.php
1 <?php
2
3 namespace Drupal\comment\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 comment entity translation source plugin.
10  *
11  * @MigrateSource(
12  *   id = "d7_comment_entity_translation",
13  *   source_module = "entity_translation"
14  * )
15  */
16 class CommentEntityTranslation extends FieldableEntity {
17
18   /**
19    * {@inheritdoc}
20    */
21   public function query() {
22     $query = $this->select('entity_translation', 'et')
23       ->fields('et')
24       ->fields('c', [
25         'subject',
26       ])
27       ->condition('et.entity_type', 'comment')
28       ->condition('et.source', '', '<>');
29
30     $query->innerJoin('comment', 'c', 'c.cid = et.entity_id');
31     $query->innerJoin('node', 'n', 'n.nid = c.nid');
32
33     $query->addField('n', 'type', 'node_type');
34
35     $query->orderBy('et.created');
36
37     return $query;
38   }
39
40   /**
41    * {@inheritdoc}
42    */
43   public function prepareRow(Row $row) {
44     $cid = $row->getSourceProperty('entity_id');
45     $language = $row->getSourceProperty('language');
46     $node_type = $row->getSourceProperty('node_type');
47     $comment_type = 'comment_node_' . $node_type;
48
49     // Get Field API field values.
50     foreach ($this->getFields('comment', $comment_type) as $field_name => $field) {
51       // Ensure we're using the right language if the entity is translatable.
52       $field_language = $field['translatable'] ? $language : NULL;
53       $row->setSourceProperty($field_name, $this->getFieldValues('comment', $field_name, $cid, NULL, $field_language));
54     }
55
56     // If the comment subject was replaced by a real field using the Drupal 7
57     // Title module, use the field value instead of the comment subject.
58     if ($this->moduleExists('title')) {
59       $subject_field = $row->getSourceProperty('subject_field');
60       if (isset($subject_field[0]['value'])) {
61         $row->setSourceProperty('subject', $subject_field[0]['value']);
62       }
63     }
64
65     return parent::prepareRow($row);
66   }
67
68   /**
69    * {@inheritdoc}
70    */
71   public function fields() {
72     return [
73       'entity_type' => $this->t('The entity type this translation relates to'),
74       'entity_id' => $this->t('The entity ID this translation relates to'),
75       'revision_id' => $this->t('The entity revision ID this translation relates to'),
76       'language' => $this->t('The target language for this translation.'),
77       'source' => $this->t('The source language from which this translation was created.'),
78       'uid' => $this->t('The author of this translation.'),
79       'status' => $this->t('Boolean indicating whether the translation is published (visible to non-administrators).'),
80       'translate' => $this->t('A boolean indicating whether this translation needs to be updated.'),
81       'created' => $this->t('The Unix timestamp when the translation was created.'),
82       'changed' => $this->t('The Unix timestamp when the translation was most recently saved.'),
83       'subject' => $this->t('The comment title.'),
84     ];
85   }
86
87   /**
88    * {@inheritdoc}
89    */
90   public function getIds() {
91     return [
92       'entity_id' => [
93         'type' => 'integer',
94         'alias' => 'et',
95       ],
96       'language' => [
97         'type' => 'string',
98         'alias' => 'et',
99       ],
100     ];
101   }
102
103 }