annotate core/modules/comment/comment.install @ 0:4c8ae668cc8c

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