5 * Install, update and uninstall functions for the Comment module.
8 use Drupal\Core\Entity\EntityTypeInterface;
9 use Drupal\Core\StringTranslation\PluralTranslatableMarkup;
10 use Drupal\Core\StringTranslation\TranslatableMarkup;
11 use Drupal\field\Entity\FieldStorageConfig;
14 * Implements hook_uninstall().
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) {
23 // Remove state setting.
24 \Drupal::state()->delete('comment.node_comment_statistics_scale');
28 * Implements hook_install().
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);
37 * Implements hook_schema().
39 function comment_schema() {
40 $schema['comment_entity_statistics'] = [
41 'description' => 'Maintains statistics of entity and comments posts to show "new" and "updated" flags.',
48 'description' => 'The entity_id of the entity for which the statistics are compiled.',
51 'type' => 'varchar_ascii',
54 'length' => EntityTypeInterface::ID_MAX_LENGTH,
55 'description' => 'The entity_type of the entity to which this comment is a reply.',
58 'type' => 'varchar_ascii',
61 'length' => FieldStorageConfig::NAME_MAX_LENGTH,
62 'description' => 'The field_name of the field that was used to add this comment.',
68 'description' => 'The {comment}.cid of the last comment.',
70 'last_comment_timestamp' => [
74 'description' => 'The Unix timestamp of the last comment that was posted within this node, from {comment}.changed.',
76 'last_comment_name' => [
80 'description' => 'The name of the latest author to post a comment on this node, from {comment}.name.',
82 'last_comment_uid' => [
87 'description' => 'The user ID of the latest author to post a comment on this node, from {comment}.uid.',
94 'description' => 'The total number of comments on this entity.',
97 'primary key' => ['entity_id', 'entity_type', 'field_name'],
99 'last_comment_timestamp' => ['last_comment_timestamp'],
100 'comment_count' => ['comment_count'],
101 'last_comment_uid' => ['last_comment_uid'],
104 'last_comment_author' => [
107 'last_comment_uid' => 'uid',
117 * Clear caches to fix Comment entity list builder and operations Views field.
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.
125 * Clear caches to fix Comment Views context filter.
127 function comment_update_8002() {
128 // Empty update to cause a cache flush.
132 * Add the 'view_mode' setting to displays having 'comment_default' formatter.
134 function comment_update_8200() {
135 $config_factory = \Drupal::configFactory();
137 // Iterate on all entity view displays.
138 foreach ($config_factory->listAll('core.entity_view_display.') as $name) {
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');
153 $display->save(TRUE);
158 return new PluralTranslatableMarkup(count($displays), '1 entity display updated: @displays.', '@count entity displays updated: @displays.', ['@displays' => implode(', ', $displays)]);
161 return new TranslatableMarkup('No entity view display updated.');
166 * Update status field.
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);
177 * Set the 'published' entity key.
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);