comment.install 6.3 KB

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