Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / comment / src / Plugin / migrate / source / d6 / CommentVariable.php
1 <?php
2
3 namespace Drupal\comment\Plugin\migrate\source\d6;
4
5 use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
6 use Drupal\migrate\Plugin\migrate\source\DummyQueryTrait;
7
8 /**
9  * @MigrateSource(
10  *   id = "d6_comment_variable"
11  * )
12  */
13 class CommentVariable extends DrupalSqlBase {
14
15   use DummyQueryTrait;
16
17   /**
18    * {@inheritdoc}
19    */
20   protected function initializeIterator() {
21     return new \ArrayIterator($this->getCommentVariables());
22   }
23
24   /**
25    * {@inheritdoc}
26    */
27   public function count() {
28     return count($this->getCommentVariables());
29   }
30
31   /**
32    * Retrieves the values of the comment variables grouped by node type.
33    *
34    * @return array
35    */
36   protected function getCommentVariables() {
37     $comment_prefixes = array_keys($this->commentPrefixes());
38     $variables = [];
39     $node_types = $this->select('node_type', 'nt')
40       ->fields('nt', ['type'])
41       ->execute()
42       ->fetchCol();
43     foreach ($node_types as $node_type) {
44       foreach ($comment_prefixes as $prefix) {
45         $variables[] = $prefix . '_' . $node_type;
46       }
47     }
48     $return = [];
49     $values = $this->select('variable', 'v')
50       ->fields('v', ['name', 'value'])
51       ->condition('name', $variables, 'IN')
52       ->execute()
53       ->fetchAllKeyed();
54     foreach ($node_types as $node_type) {
55       foreach ($comment_prefixes as $prefix) {
56         $name = $prefix . '_' . $node_type;
57         if (isset($values[$name])) {
58           $return[$node_type][$prefix] = unserialize($values[$name]);
59         }
60       }
61     }
62     // The return key will not be used so move it inside the row. This could
63     // not be done sooner because otherwise empty rows would be created with
64     // just the node type in it.
65     foreach ($return as $node_type => $data) {
66       $return[$node_type]['node_type'] = $node_type;
67       $return[$node_type]['comment_type'] = empty($data['comment_subject_field']) ?
68         'comment_no_subject' : 'comment';
69     }
70     return $return;
71   }
72
73   /**
74    * {@inheritdoc}
75    */
76   public function fields() {
77     return $this->commentPrefixes() + [
78       'node_type' => $this->t('The node type'),
79       'comment_type' => $this->t('The comment type'),
80     ];
81   }
82
83   /**
84    * Comment related data for fields.
85    */
86   protected function commentPrefixes() {
87     return [
88       'comment' => $this->t('Default comment setting'),
89       'comment_default_mode' => $this->t('Default display mode'),
90       'comment_default_order' => $this->t('Default display order'),
91       'comment_default_per_page' => $this->t('Default comments per page'),
92       'comment_controls' => $this->t('Comment controls'),
93       'comment_anonymous' => $this->t('Anonymous commenting'),
94       'comment_subject_field' => $this->t('Comment subject field'),
95       'comment_preview' => $this->t('Preview comment'),
96       'comment_form_location' => $this->t('Location of comment submission form'),
97     ];
98   }
99
100   /**
101    * {@inheritdoc}
102    */
103   public function getIds() {
104     $ids['node_type']['type'] = 'string';
105     return $ids;
106   }
107
108 }