comparison core/modules/comment/comment.install @ 0:c75dbcec494b

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