f35a205ae4b48061efa94dc42d96ff0bcaad7299
[yaffs-website] / web / core / modules / content_translation / src / Plugin / migrate / source / d7 / EntityTranslationSettings.php
1 <?php
2
3 namespace Drupal\content_translation\Plugin\migrate\source\d7;
4
5 use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
6
7 /**
8  * Drupal 7 Entity Translation settings from variables.
9  *
10  * @MigrateSource(
11  *   id = "d7_entity_translation_settings",
12  *   source_module = "entity_translation"
13  * )
14  */
15 class EntityTranslationSettings extends DrupalSqlBase {
16
17   /**
18    * {@inheritdoc}
19    */
20   public function query() {
21     // Query all meaningful variables for entity translation.
22     $query = $this->select('variable', 'v')
23       ->fields('v', ['name', 'value']);
24     $condition = $query->orConditionGroup()
25       // The 'entity_translation_entity_types' variable tells us which entity
26       // type uses entity translation.
27       ->condition('name', 'entity_translation_entity_types')
28       // The 'entity_translation_taxonomy' variable tells us which taxonomy
29       // vocabulary uses entity_translation.
30       ->condition('name', 'entity_translation_taxonomy')
31       // The 'entity_translation_settings_%' variables give us the entity
32       // translation settings for each entity type and each bundle.
33       ->condition('name', 'entity_translation_settings_%', 'LIKE')
34       // The 'language_content_type_%' variables tells us which node type and
35       // which comment type uses entity translation.
36       ->condition('name', 'language_content_type_%', 'LIKE');
37     $query->condition($condition);
38     return $query;
39   }
40
41   /**
42    * {@inheritdoc}
43    */
44   protected function initializeIterator() {
45     $results = array_map('unserialize', $this->prepareQuery()->execute()->fetchAllKeyed());
46     $rows = [];
47
48     // Find out which entity type uses entity translation by looking at the
49     // 'entity_translation_entity_types' variable.
50     $entity_types = array_filter($results['entity_translation_entity_types']);
51
52     // If no entity type uses entity translation, there's nothing to do.
53     if (empty($entity_types)) {
54       return new \ArrayIterator($rows);
55     }
56
57     // Find out which node type uses entity translation by looking at the
58     // 'language_content_type_%' variables.
59     $node_types = [];
60     foreach ($results as $name => $value) {
61       if (preg_match('/^language_content_type_(.+)$/', $name, $matches) && (int) $value === 4) {
62         $node_types[] = $matches[1];
63       }
64     }
65
66     // Find out which vocabulary uses entity translation by looking at the
67     // 'entity_translation_taxonomy' variable.
68     $vocabularies = [];
69     if (isset($results['entity_translation_taxonomy']) && is_array($results['entity_translation_taxonomy'])) {
70       $vocabularies = array_keys(array_filter($results['entity_translation_taxonomy']));
71     }
72
73     if (in_array('node', $entity_types, TRUE) && !empty($node_types)) {
74       // For each node type that uses entity translation, check if a
75       // settings variable exists for that node type, otherwise use default
76       // values.
77       foreach ($node_types as $node_type) {
78         $settings = isset($results['entity_translation_settings_node__' . $node_type]) ? $results['entity_translation_settings_node__' . $node_type] : [];
79         $rows[] = [
80           'id' => 'node.' . $node_type,
81           'target_entity_type_id' => 'node',
82           'target_bundle' => $node_type,
83           'default_langcode' => isset($settings['default_language']) ? $settings['default_language'] : 'und',
84           // The Drupal 7 'hide_language_selector' configuration has become
85           // 'language_alterable' in Drupal 8 so we need to negate the value we
86           // receive from the source. The Drupal 7 'hide_language_selector'
87           // default value for the node entity type was FALSE so in Drupal 8 it
88           // should be set to TRUE, unlike the other entity types for which
89           // it's the opposite.
90           'language_alterable' => isset($settings['hide_language_selector']) ? (bool) !$settings['hide_language_selector'] : TRUE,
91           'untranslatable_fields_hide' => isset($settings['shared_fields_original_only']) ? (bool) $settings['shared_fields_original_only'] : FALSE,
92         ];
93       }
94     }
95
96     if (in_array('comment', $entity_types, TRUE) && !empty($node_types)) {
97       // A comment type uses entity translation if the associated node type
98       // uses it. So, for each node type that uses entity translation, check
99       // if a settings variable exists for that comment type, otherwise use
100       // default values.
101       foreach ($node_types as $node_type) {
102         $settings = isset($results['entity_translation_settings_comment__comment_node_' . $node_type]) ? $results['entity_translation_settings_comment__comment_node_' . $node_type] : [];
103         // Forum uses a hardcoded comment type name, so make sure we use it
104         // when we're dealing with forum comment type.
105         $bundle = $node_type == 'forum' ? 'comment_forum' : 'comment_node_' . $node_type;
106         $rows[] = [
107           'id' => 'comment.' . $bundle,
108           'target_entity_type_id' => 'comment',
109           'target_bundle' => $bundle,
110           'default_langcode' => isset($settings['default_language']) ? $settings['default_language'] : 'xx-et-current',
111           // The Drupal 7 'hide_language_selector' configuration has become
112           // 'language_alterable' in Drupal 8 so we need to negate the value we
113           // receive from the source. The Drupal 7 'hide_language_selector'
114           // default value for the comment entity type was TRUE so in Drupal 8
115           // it should be set to FALSE.
116           'language_alterable' => isset($settings['hide_language_selector']) ? (bool) !$settings['hide_language_selector'] : FALSE,
117           'untranslatable_fields_hide' => isset($settings['shared_fields_original_only']) ? (bool) $settings['shared_fields_original_only'] : FALSE,
118         ];
119       }
120     }
121
122     if (in_array('taxonomy_term', $entity_types, TRUE) && !empty($vocabularies)) {
123       // For each vocabulary that uses entity translation, check if a
124       // settings variable exists for that vocabulary, otherwise use default
125       // values.
126       foreach ($vocabularies as $vocabulary) {
127         $settings = isset($results['entity_translation_settings_taxonomy_term__' . $vocabulary]) ? $results['entity_translation_settings_taxonomy_term__' . $vocabulary] : [];
128         $rows[] = [
129           'id' => 'taxonomy_term.' . $vocabulary,
130           'target_entity_type_id' => 'taxonomy_term',
131           'target_bundle' => $vocabulary,
132           'default_langcode' => isset($settings['default_language']) ? $settings['default_language'] : 'xx-et-default',
133           // The Drupal 7 'hide_language_selector' configuration has become
134           // 'language_alterable' in Drupal 8 so we need to negate the value we
135           // receive from the source. The Drupal 7 'hide_language_selector'
136           // default value for the taxonomy_term entity type was TRUE so in
137           // Drupal 8 it should be set to FALSE.
138           'language_alterable' => isset($settings['hide_language_selector']) ? (bool) !$settings['hide_language_selector'] : FALSE,
139           'untranslatable_fields_hide' => isset($settings['shared_fields_original_only']) ? (bool) $settings['shared_fields_original_only'] : FALSE,
140         ];
141       }
142     }
143
144     if (in_array('user', $entity_types, TRUE)) {
145       // User entity type is not bundleable. Check if a settings variable
146       // exists, otherwise use default values.
147       $settings = isset($results['entity_translation_settings_user__user']) ? $results['entity_translation_settings_user__user'] : [];
148       $rows[] = [
149         'id' => 'user.user',
150         'target_entity_type_id' => 'user',
151         'target_bundle' => 'user',
152         'default_langcode' => isset($settings['default_language']) ? $settings['default_language'] : 'xx-et-default',
153         // The Drupal 7 'hide_language_selector' configuration has become
154         // 'language_alterable' in Drupal 8 so we need to negate the value we
155         // receive from the source. The Drupal 7 'hide_language_selector'
156         // default value for the user entity type was TRUE so in Drupal 8 it
157         // should be set to FALSE.
158         'language_alterable' => isset($settings['hide_language_selector']) ? (bool) !$settings['hide_language_selector'] : FALSE,
159         'untranslatable_fields_hide' => isset($settings['shared_fields_original_only']) ? (bool) $settings['shared_fields_original_only'] : FALSE,
160       ];
161     }
162
163     return new \ArrayIterator($rows);
164   }
165
166   /**
167    * {@inheritdoc}
168    */
169   public function fields() {
170     return [
171       'id' => $this->t('The configuration ID'),
172       'target_entity_type_id' => $this->t('The target entity type ID'),
173       'target_bundle' => $this->t('The target bundle'),
174       'default_langcode' => $this->t('The default language'),
175       'language_alterable' => $this->t('Whether to show language selector on create and edit pages'),
176       'untranslatable_fields_hide' => $this->t('Whether to hide non translatable fields on translation forms'),
177     ];
178   }
179
180   /**
181    * {@inheritdoc}
182    */
183   public function getIds() {
184     $ids['id']['type'] = 'string';
185     return $ids;
186   }
187
188   /**
189    * {@inheritdoc}
190    */
191   public function count($refresh = FALSE) {
192     // Since the number of variables we fetch with query() does not match the
193     // actual number of rows generated by initializeIterator(), we need to
194     // override count() to return the correct count.
195     return (int) $this->initializeIterator()->count();
196   }
197
198 }