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