Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /**
|
Chris@0
|
4 * @file
|
Chris@0
|
5 * Install, update and uninstall functions for the Comment module.
|
Chris@0
|
6 */
|
Chris@0
|
7
|
Chris@17
|
8 use Drupal\comment\Entity\Comment;
|
Chris@0
|
9 use Drupal\Core\Entity\EntityTypeInterface;
|
Chris@0
|
10 use Drupal\Core\StringTranslation\PluralTranslatableMarkup;
|
Chris@0
|
11 use Drupal\Core\StringTranslation\TranslatableMarkup;
|
Chris@0
|
12 use Drupal\field\Entity\FieldStorageConfig;
|
Chris@0
|
13
|
Chris@0
|
14 /**
|
Chris@0
|
15 * Implements hook_uninstall().
|
Chris@0
|
16 */
|
Chris@0
|
17 function comment_uninstall() {
|
Chris@0
|
18 // Remove the comment fields.
|
Chris@0
|
19 $fields = entity_load_multiple_by_properties('field_storage_config', ['type' => 'comment']);
|
Chris@0
|
20 foreach ($fields as $field) {
|
Chris@0
|
21 $field->delete();
|
Chris@0
|
22 }
|
Chris@0
|
23
|
Chris@0
|
24 // Remove state setting.
|
Chris@0
|
25 \Drupal::state()->delete('comment.node_comment_statistics_scale');
|
Chris@0
|
26 }
|
Chris@0
|
27
|
Chris@0
|
28 /**
|
Chris@0
|
29 * Implements hook_install().
|
Chris@0
|
30 */
|
Chris@0
|
31 function comment_install() {
|
Chris@0
|
32 // By default, maintain entity statistics for comments.
|
Chris@0
|
33 // @see \Drupal\comment\CommentStatisticsInterface
|
Chris@0
|
34 \Drupal::state()->set('comment.maintain_entity_statistics', TRUE);
|
Chris@0
|
35 }
|
Chris@0
|
36
|
Chris@0
|
37 /**
|
Chris@0
|
38 * Implements hook_schema().
|
Chris@0
|
39 */
|
Chris@0
|
40 function comment_schema() {
|
Chris@0
|
41 $schema['comment_entity_statistics'] = [
|
Chris@0
|
42 'description' => 'Maintains statistics of entity and comments posts to show "new" and "updated" flags.',
|
Chris@0
|
43 'fields' => [
|
Chris@0
|
44 'entity_id' => [
|
Chris@0
|
45 'type' => 'int',
|
Chris@0
|
46 'unsigned' => TRUE,
|
Chris@0
|
47 'not null' => TRUE,
|
Chris@0
|
48 'default' => 0,
|
Chris@0
|
49 'description' => 'The entity_id of the entity for which the statistics are compiled.',
|
Chris@0
|
50 ],
|
Chris@0
|
51 'entity_type' => [
|
Chris@0
|
52 'type' => 'varchar_ascii',
|
Chris@0
|
53 'not null' => TRUE,
|
Chris@0
|
54 'default' => 'node',
|
Chris@0
|
55 'length' => EntityTypeInterface::ID_MAX_LENGTH,
|
Chris@0
|
56 'description' => 'The entity_type of the entity to which this comment is a reply.',
|
Chris@0
|
57 ],
|
Chris@0
|
58 'field_name' => [
|
Chris@0
|
59 'type' => 'varchar_ascii',
|
Chris@0
|
60 'not null' => TRUE,
|
Chris@0
|
61 'default' => '',
|
Chris@0
|
62 'length' => FieldStorageConfig::NAME_MAX_LENGTH,
|
Chris@0
|
63 'description' => 'The field_name of the field that was used to add this comment.',
|
Chris@0
|
64 ],
|
Chris@0
|
65 'cid' => [
|
Chris@0
|
66 'type' => 'int',
|
Chris@0
|
67 'not null' => TRUE,
|
Chris@0
|
68 'default' => 0,
|
Chris@0
|
69 'description' => 'The {comment}.cid of the last comment.',
|
Chris@0
|
70 ],
|
Chris@0
|
71 'last_comment_timestamp' => [
|
Chris@0
|
72 'type' => 'int',
|
Chris@0
|
73 'not null' => TRUE,
|
Chris@0
|
74 'default' => 0,
|
Chris@0
|
75 'description' => 'The Unix timestamp of the last comment that was posted within this node, from {comment}.changed.',
|
Chris@0
|
76 ],
|
Chris@0
|
77 'last_comment_name' => [
|
Chris@0
|
78 'type' => 'varchar',
|
Chris@0
|
79 'length' => 60,
|
Chris@0
|
80 'not null' => FALSE,
|
Chris@0
|
81 'description' => 'The name of the latest author to post a comment on this node, from {comment}.name.',
|
Chris@0
|
82 ],
|
Chris@0
|
83 'last_comment_uid' => [
|
Chris@0
|
84 'type' => 'int',
|
Chris@0
|
85 'unsigned' => TRUE,
|
Chris@0
|
86 'not null' => TRUE,
|
Chris@0
|
87 'default' => 0,
|
Chris@0
|
88 'description' => 'The user ID of the latest author to post a comment on this node, from {comment}.uid.',
|
Chris@0
|
89 ],
|
Chris@0
|
90 'comment_count' => [
|
Chris@0
|
91 'type' => 'int',
|
Chris@0
|
92 'unsigned' => TRUE,
|
Chris@0
|
93 'not null' => TRUE,
|
Chris@0
|
94 'default' => 0,
|
Chris@0
|
95 'description' => 'The total number of comments on this entity.',
|
Chris@0
|
96 ],
|
Chris@0
|
97 ],
|
Chris@0
|
98 'primary key' => ['entity_id', 'entity_type', 'field_name'],
|
Chris@0
|
99 'indexes' => [
|
Chris@0
|
100 'last_comment_timestamp' => ['last_comment_timestamp'],
|
Chris@0
|
101 'comment_count' => ['comment_count'],
|
Chris@0
|
102 'last_comment_uid' => ['last_comment_uid'],
|
Chris@0
|
103 ],
|
Chris@0
|
104 'foreign keys' => [
|
Chris@0
|
105 'last_comment_author' => [
|
Chris@0
|
106 'table' => 'users',
|
Chris@0
|
107 'columns' => [
|
Chris@0
|
108 'last_comment_uid' => 'uid',
|
Chris@0
|
109 ],
|
Chris@0
|
110 ],
|
Chris@0
|
111 ],
|
Chris@0
|
112 ];
|
Chris@0
|
113
|
Chris@0
|
114 return $schema;
|
Chris@0
|
115 }
|
Chris@0
|
116
|
Chris@0
|
117 /**
|
Chris@0
|
118 * Clear caches to fix Comment entity list builder and operations Views field.
|
Chris@0
|
119 */
|
Chris@0
|
120 function comment_update_8001() {
|
Chris@0
|
121 // Empty update to cause a cache flush to rebuild comment entity handler
|
Chris@0
|
122 // information, so that comment operation links work.
|
Chris@0
|
123 }
|
Chris@0
|
124
|
Chris@0
|
125 /**
|
Chris@0
|
126 * Clear caches to fix Comment Views context filter.
|
Chris@0
|
127 */
|
Chris@0
|
128 function comment_update_8002() {
|
Chris@0
|
129 // Empty update to cause a cache flush.
|
Chris@0
|
130 }
|
Chris@0
|
131
|
Chris@0
|
132 /**
|
Chris@0
|
133 * Add the 'view_mode' setting to displays having 'comment_default' formatter.
|
Chris@0
|
134 */
|
Chris@0
|
135 function comment_update_8200() {
|
Chris@0
|
136 $config_factory = \Drupal::configFactory();
|
Chris@0
|
137 $displays = [];
|
Chris@0
|
138 // Iterate on all entity view displays.
|
Chris@0
|
139 foreach ($config_factory->listAll('core.entity_view_display.') as $name) {
|
Chris@0
|
140 $changed = FALSE;
|
Chris@0
|
141 $display = $config_factory->getEditable($name);
|
Chris@0
|
142 $components = $display->get('content') ?: [];
|
Chris@0
|
143 foreach ($components as $field_name => $component) {
|
Chris@0
|
144 if (isset($component['type']) && ($component['type'] === 'comment_default')) {
|
Chris@0
|
145 if (empty($display->get("content.{$field_name}.settings.view_mode"))) {
|
Chris@0
|
146 $display->set("content.{$field_name}.settings.view_mode", 'default');
|
Chris@0
|
147 $displays[] = $display->get('id');
|
Chris@0
|
148 $changed = TRUE;
|
Chris@0
|
149 }
|
Chris@0
|
150 }
|
Chris@0
|
151 }
|
Chris@0
|
152
|
Chris@0
|
153 if ($changed) {
|
Chris@0
|
154 $display->save(TRUE);
|
Chris@0
|
155 }
|
Chris@0
|
156 }
|
Chris@0
|
157
|
Chris@0
|
158 if ($displays) {
|
Chris@0
|
159 return new PluralTranslatableMarkup(count($displays), '1 entity display updated: @displays.', '@count entity displays updated: @displays.', ['@displays' => implode(', ', $displays)]);
|
Chris@0
|
160 }
|
Chris@0
|
161 else {
|
Chris@0
|
162 return new TranslatableMarkup('No entity view display updated.');
|
Chris@0
|
163 }
|
Chris@0
|
164 }
|
Chris@0
|
165
|
Chris@0
|
166 /**
|
Chris@0
|
167 * Update status field.
|
Chris@0
|
168 */
|
Chris@0
|
169 function comment_update_8300() {
|
Chris@0
|
170 $entity_definition_update_manager = \Drupal::service('entity.definition_update_manager');
|
Chris@0
|
171 $field_definition = $entity_definition_update_manager->getFieldStorageDefinition('status', 'comment');
|
Chris@0
|
172 $field_definition->setDescription(new TranslatableMarkup('A boolean indicating the published state.'))
|
Chris@0
|
173 ->setRevisionable(TRUE);
|
Chris@0
|
174 $entity_definition_update_manager->updateFieldStorageDefinition($field_definition);
|
Chris@0
|
175 }
|
Chris@0
|
176
|
Chris@0
|
177 /**
|
Chris@0
|
178 * Set the 'published' entity key.
|
Chris@0
|
179 */
|
Chris@0
|
180 function comment_update_8301() {
|
Chris@0
|
181 $definition_update_manager = \Drupal::entityDefinitionUpdateManager();
|
Chris@0
|
182 $entity_type = $definition_update_manager->getEntityType('comment');
|
Chris@0
|
183 $keys = $entity_type->getKeys();
|
Chris@0
|
184 $keys['published'] = 'status';
|
Chris@0
|
185 $entity_type->set('entity_keys', $keys);
|
Chris@0
|
186 $definition_update_manager->updateEntityType($entity_type);
|
Chris@0
|
187 }
|
Chris@0
|
188
|
Chris@0
|
189 /**
|
Chris@0
|
190 * Update the status field.
|
Chris@0
|
191 */
|
Chris@0
|
192 function comment_update_8400() {
|
Chris@0
|
193 // The status field was promoted to an entity key in comment_update_8301(),
|
Chris@0
|
194 // which makes it NOT NULL in the default SQL storage, which means its storage
|
Chris@0
|
195 // definition needs to be updated as well.
|
Chris@0
|
196 $entity_definition_update_manager = \Drupal::service('entity.definition_update_manager');
|
Chris@0
|
197 $entity_definition_update_manager->updateFieldStorageDefinition($entity_definition_update_manager->getFieldStorageDefinition('status', 'comment'));
|
Chris@0
|
198 }
|
Chris@17
|
199
|
Chris@17
|
200 /**
|
Chris@17
|
201 * Configure the comment hostname base field to use a default value callback.
|
Chris@17
|
202 */
|
Chris@17
|
203 function comment_update_8600() {
|
Chris@17
|
204 $entity_definition_update_manager = \Drupal::entityDefinitionUpdateManager();
|
Chris@17
|
205 /** @var \Drupal\Core\Field\BaseFieldDefinition $field_storage_definition */
|
Chris@17
|
206 $field_storage_definition = $entity_definition_update_manager->getFieldStorageDefinition('hostname', 'comment');
|
Chris@17
|
207 $field_storage_definition->setDefaultValueCallback(Comment::class . '::getDefaultHostname');
|
Chris@17
|
208 $entity_definition_update_manager->updateFieldStorageDefinition($field_storage_definition);
|
Chris@17
|
209 }
|
Chris@18
|
210
|
Chris@18
|
211 /**
|
Chris@18
|
212 * Set the 'owner' entity key and update the field.
|
Chris@18
|
213 */
|
Chris@18
|
214 function comment_update_8700() {
|
Chris@18
|
215 $definition_update_manager = \Drupal::entityDefinitionUpdateManager();
|
Chris@18
|
216 $entity_type = $definition_update_manager->getEntityType('comment');
|
Chris@18
|
217 $keys = $entity_type->getKeys();
|
Chris@18
|
218 $keys['owner'] = 'uid';
|
Chris@18
|
219 $entity_type->set('entity_keys', $keys);
|
Chris@18
|
220 $definition_update_manager->updateEntityType($entity_type);
|
Chris@18
|
221 $definition_update_manager->updateFieldStorageDefinition($definition_update_manager->getFieldStorageDefinition('uid', 'comment'));
|
Chris@18
|
222 }
|
Chris@18
|
223
|
Chris@18
|
224 /**
|
Chris@18
|
225 * Make the 'entity_type' and 'field_name' comment fields required.
|
Chris@18
|
226 */
|
Chris@18
|
227 function comment_update_8701() {
|
Chris@18
|
228 $definition_update_manager = \Drupal::entityDefinitionUpdateManager();
|
Chris@18
|
229 $field_definition = $definition_update_manager->getFieldStorageDefinition('entity_type', 'comment');
|
Chris@18
|
230 $field_definition->setRequired(TRUE);
|
Chris@18
|
231 $definition_update_manager->updateFieldStorageDefinition($field_definition);
|
Chris@18
|
232
|
Chris@18
|
233 $field_definition = $definition_update_manager->getFieldStorageDefinition('field_name', 'comment');
|
Chris@18
|
234 $field_definition->setRequired(TRUE);
|
Chris@18
|
235 $definition_update_manager->updateFieldStorageDefinition($field_definition);
|
Chris@18
|
236 }
|