comment.install 6.9 KB

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