Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / comment / src / Plugin / migrate / source / d7 / Comment.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  * Drupal 7 comment source from database.
10  *
11  * @MigrateSource(
12  *   id = "d7_comment",
13  *   source_module = "comment"
14  * )
15  */
16 class Comment extends FieldableEntity {
17
18   /**
19    * {@inheritdoc}
20    */
21   public function query() {
22     $query = $this->select('comment', 'c')->fields('c');
23     $query->innerJoin('node', 'n', 'c.nid = n.nid');
24     $query->addField('n', 'type', 'node_type');
25     $query->orderBy('c.created');
26     return $query;
27   }
28
29   /**
30    * {@inheritdoc}
31    */
32   public function prepareRow(Row $row) {
33     $cid = $row->getSourceProperty('cid');
34
35     $node_type = $row->getSourceProperty('node_type');
36     $comment_type = 'comment_node_' . $node_type;
37     $row->setSourceProperty('comment_type', 'comment_node_' . $node_type);
38
39     // If this entity was translated using Entity Translation, we need to get
40     // its source language to get the field values in the right language.
41     // The translations will be migrated by the d7_comment_entity_translation
42     // migration.
43     $entity_translatable = $this->isEntityTranslatable('comment') && (int) $this->variableGet('language_content_type_' . $node_type, 0) === 4;
44     $source_language = $this->getEntityTranslationSourceLanguage('comment', $cid);
45     $language = $entity_translatable && $source_language ? $source_language : $row->getSourceProperty('language');
46
47     // Get Field API field values.
48     foreach ($this->getFields('comment', $comment_type) as $field_name => $field) {
49       // Ensure we're using the right language if the entity and the field are
50       // translatable.
51       $field_language = $entity_translatable && $field['translatable'] ? $language : NULL;
52       $row->setSourceProperty($field_name, $this->getFieldValues('comment', $field_name, $cid, NULL, $field_language));
53     }
54
55     // If the comment subject was replaced by a real field using the Drupal 7
56     // Title module, use the field value instead of the comment subject.
57     if ($this->moduleExists('title')) {
58       $subject_field = $row->getSourceProperty('subject_field');
59       if (isset($subject_field[0]['value'])) {
60         $row->setSourceProperty('subject', $subject_field[0]['value']);
61       }
62     }
63
64     return parent::prepareRow($row);
65   }
66
67   /**
68    * {@inheritdoc}
69    */
70   public function fields() {
71     return [
72       'cid' => $this->t('Comment ID.'),
73       'pid' => $this->t('Parent comment ID. If set to 0, this comment is not a reply to an existing comment.'),
74       'nid' => $this->t('The {node}.nid to which this comment is a reply.'),
75       'uid' => $this->t('The {users}.uid who authored the comment. If set to 0, this comment was created by an anonymous user.'),
76       'subject' => $this->t('The comment title.'),
77       'comment' => $this->t('The comment body.'),
78       'hostname' => $this->t("The author's host name."),
79       'created' => $this->t('The time that the comment was created, as a Unix timestamp.'),
80       'changed' => $this->t('The time that the comment was edited by its author, as a Unix timestamp.'),
81       'status' => $this->t('The published status of a comment. (0 = Published, 1 = Not Published)'),
82       'format' => $this->t('The {filter_formats}.format of the comment body.'),
83       'thread' => $this->t("The vancode representation of the comment's place in a thread."),
84       'name' => $this->t("The comment author's name. Uses {users}.name if the user is logged in, otherwise uses the value typed into the comment form."),
85       'mail' => $this->t("The comment author's email address from the comment form, if user is anonymous, and the 'Anonymous users may/must leave their contact information' setting is turned on."),
86       'homepage' => $this->t("The comment author's home page address from the comment form, if user is anonymous, and the 'Anonymous users may/must leave their contact information' setting is turned on."),
87       'language' => $this->t('The comment language.'),
88       'type' => $this->t("The {node}.type to which this comment is a reply."),
89     ];
90   }
91
92   /**
93    * {@inheritdoc}
94    */
95   public function getIds() {
96     $ids['cid']['type'] = 'integer';
97     return $ids;
98   }
99
100 }