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