5 * Install, update and uninstall functions for the Comment module.
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;
15 * Implements hook_uninstall().
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) {
24 // Remove state setting.
25 \Drupal::state()->delete('comment.node_comment_statistics_scale');
29 * Implements hook_install().
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);
38 * Implements hook_schema().
40 function comment_schema() {
41 $schema['comment_entity_statistics'] = [
42 'description' => 'Maintains statistics of entity and comments posts to show "new" and "updated" flags.',
49 'description' => 'The entity_id of the entity for which the statistics are compiled.',
52 'type' => 'varchar_ascii',
55 'length' => EntityTypeInterface::ID_MAX_LENGTH,
56 'description' => 'The entity_type of the entity to which this comment is a reply.',
59 'type' => 'varchar_ascii',
62 'length' => FieldStorageConfig::NAME_MAX_LENGTH,
63 'description' => 'The field_name of the field that was used to add this comment.',
69 'description' => 'The {comment}.cid of the last comment.',
71 'last_comment_timestamp' => [
75 'description' => 'The Unix timestamp of the last comment that was posted within this node, from {comment}.changed.',
77 'last_comment_name' => [
81 'description' => 'The name of the latest author to post a comment on this node, from {comment}.name.',
83 'last_comment_uid' => [
88 'description' => 'The user ID of the latest author to post a comment on this node, from {comment}.uid.',
95 'description' => 'The total number of comments on this entity.',
98 'primary key' => ['entity_id', 'entity_type', 'field_name'],
100 'last_comment_timestamp' => ['last_comment_timestamp'],
101 'comment_count' => ['comment_count'],
102 'last_comment_uid' => ['last_comment_uid'],
105 'last_comment_author' => [
108 'last_comment_uid' => 'uid',
118 * Clear caches to fix Comment entity list builder and operations Views field.
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.
126 * Clear caches to fix Comment Views context filter.
128 function comment_update_8002() {
129 // Empty update to cause a cache flush.
133 * Add the 'view_mode' setting to displays having 'comment_default' formatter.
135 function comment_update_8200() {
136 $config_factory = \Drupal::configFactory();
138 // Iterate on all entity view displays.
139 foreach ($config_factory->listAll('core.entity_view_display.') as $name) {
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');
154 $display->save(TRUE);
159 return new PluralTranslatableMarkup(count($displays), '1 entity display updated: @displays.', '@count entity displays updated: @displays.', ['@displays' => implode(', ', $displays)]);
162 return new TranslatableMarkup('No entity view display updated.');
167 * Update status field.
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);
178 * Set the 'published' entity key.
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);
190 * Update the status field.
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'));
201 * Configure the comment hostname base field to use a default value callback.
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);