comment.install 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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_requirements().
  13. */
  14. function comment_requirements($phase) {
  15. $requirements = [];
  16. if ($phase === 'update' && drupal_get_installed_schema_version('comment') < 8701) {
  17. $has_empty_columns = \Drupal::entityQuery('comment', 'OR')
  18. ->condition('entity_type', NULL, 'IS NULL')
  19. ->condition('field_name', NULL, 'IS NULL')
  20. ->range(0, 1)
  21. ->accessCheck(FALSE)
  22. ->execute();
  23. if ($has_empty_columns) {
  24. $requirements['comment_update_8701'] = [
  25. 'title' => t('Comment required fields update'),
  26. 'description' => t('The comment_update_8701() function requires that the %field_1 and %field_2 fields have values for all comment entities. See the <a href=":change_record">change record</a> for more information.', [
  27. '%field_1' => 'entity_type',
  28. '%field_2' => 'field_name',
  29. ':change_record' => 'https://www.drupal.org/node/3053046',
  30. ]),
  31. 'severity' => REQUIREMENT_ERROR,
  32. ];
  33. }
  34. }
  35. return $requirements;
  36. }
  37. /**
  38. * Implements hook_uninstall().
  39. */
  40. function comment_uninstall() {
  41. // Remove the comment fields.
  42. $storage = \Drupal::entityTypeManager()->getStorage('field_storage_config');
  43. $fields = $storage->loadByProperties(['type' => 'comment']);
  44. $storage->delete($fields);
  45. // Remove state setting.
  46. \Drupal::state()->delete('comment.node_comment_statistics_scale');
  47. }
  48. /**
  49. * Implements hook_install().
  50. */
  51. function comment_install() {
  52. // By default, maintain entity statistics for comments.
  53. // @see \Drupal\comment\CommentStatisticsInterface
  54. \Drupal::state()->set('comment.maintain_entity_statistics', TRUE);
  55. }
  56. /**
  57. * Implements hook_schema().
  58. */
  59. function comment_schema() {
  60. $schema['comment_entity_statistics'] = [
  61. 'description' => 'Maintains statistics of entity and comments posts to show "new" and "updated" flags.',
  62. 'fields' => [
  63. 'entity_id' => [
  64. 'type' => 'int',
  65. 'unsigned' => TRUE,
  66. 'not null' => TRUE,
  67. 'default' => 0,
  68. 'description' => 'The entity_id of the entity for which the statistics are compiled.',
  69. ],
  70. 'entity_type' => [
  71. 'type' => 'varchar_ascii',
  72. 'not null' => TRUE,
  73. 'default' => 'node',
  74. 'length' => EntityTypeInterface::ID_MAX_LENGTH,
  75. 'description' => 'The entity_type of the entity to which this comment is a reply.',
  76. ],
  77. 'field_name' => [
  78. 'type' => 'varchar_ascii',
  79. 'not null' => TRUE,
  80. 'default' => '',
  81. 'length' => FieldStorageConfig::NAME_MAX_LENGTH,
  82. 'description' => 'The field_name of the field that was used to add this comment.',
  83. ],
  84. 'cid' => [
  85. 'type' => 'int',
  86. 'not null' => TRUE,
  87. 'default' => 0,
  88. 'description' => 'The {comment}.cid of the last comment.',
  89. ],
  90. 'last_comment_timestamp' => [
  91. 'type' => 'int',
  92. 'not null' => TRUE,
  93. 'default' => 0,
  94. 'description' => 'The Unix timestamp of the last comment that was posted within this node, from {comment}.changed.',
  95. ],
  96. 'last_comment_name' => [
  97. 'type' => 'varchar',
  98. 'length' => 60,
  99. 'not null' => FALSE,
  100. 'description' => 'The name of the latest author to post a comment on this node, from {comment}.name.',
  101. ],
  102. 'last_comment_uid' => [
  103. 'type' => 'int',
  104. 'unsigned' => TRUE,
  105. 'not null' => TRUE,
  106. 'default' => 0,
  107. 'description' => 'The user ID of the latest author to post a comment on this node, from {comment}.uid.',
  108. ],
  109. 'comment_count' => [
  110. 'type' => 'int',
  111. 'unsigned' => TRUE,
  112. 'not null' => TRUE,
  113. 'default' => 0,
  114. 'description' => 'The total number of comments on this entity.',
  115. ],
  116. ],
  117. 'primary key' => ['entity_id', 'entity_type', 'field_name'],
  118. 'indexes' => [
  119. 'last_comment_timestamp' => ['last_comment_timestamp'],
  120. 'comment_count' => ['comment_count'],
  121. 'last_comment_uid' => ['last_comment_uid'],
  122. ],
  123. 'foreign keys' => [
  124. 'last_comment_author' => [
  125. 'table' => 'users',
  126. 'columns' => [
  127. 'last_comment_uid' => 'uid',
  128. ],
  129. ],
  130. ],
  131. ];
  132. return $schema;
  133. }
  134. /**
  135. * Clear caches to fix Comment entity list builder and operations Views field.
  136. */
  137. function comment_update_8001() {
  138. // Empty update to cause a cache flush to rebuild comment entity handler
  139. // information, so that comment operation links work.
  140. }
  141. /**
  142. * Clear caches to fix Comment Views context filter.
  143. */
  144. function comment_update_8002() {
  145. // Empty update to cause a cache flush.
  146. }
  147. /**
  148. * Add the 'view_mode' setting to displays having 'comment_default' formatter.
  149. */
  150. function comment_update_8200() {
  151. $config_factory = \Drupal::configFactory();
  152. $displays = [];
  153. // Iterate on all entity view displays.
  154. foreach ($config_factory->listAll('core.entity_view_display.') as $name) {
  155. $changed = FALSE;
  156. $display = $config_factory->getEditable($name);
  157. $components = $display->get('content') ?: [];
  158. foreach ($components as $field_name => $component) {
  159. if (isset($component['type']) && ($component['type'] === 'comment_default')) {
  160. if (empty($display->get("content.{$field_name}.settings.view_mode"))) {
  161. $display->set("content.{$field_name}.settings.view_mode", 'default');
  162. $displays[] = $display->get('id');
  163. $changed = TRUE;
  164. }
  165. }
  166. }
  167. if ($changed) {
  168. $display->save(TRUE);
  169. }
  170. }
  171. if ($displays) {
  172. return new PluralTranslatableMarkup(count($displays), '1 entity display updated: @displays.', '@count entity displays updated: @displays.', ['@displays' => implode(', ', $displays)]);
  173. }
  174. else {
  175. return new TranslatableMarkup('No entity view display updated.');
  176. }
  177. }
  178. /**
  179. * Update status field.
  180. */
  181. function comment_update_8300() {
  182. $entity_definition_update_manager = \Drupal::service('entity.definition_update_manager');
  183. $field_definition = $entity_definition_update_manager->getFieldStorageDefinition('status', 'comment');
  184. $field_definition->setDescription(new TranslatableMarkup('A boolean indicating the published state.'))
  185. ->setRevisionable(TRUE);
  186. $entity_definition_update_manager->updateFieldStorageDefinition($field_definition);
  187. }
  188. /**
  189. * Set the 'published' entity key.
  190. */
  191. function comment_update_8301() {
  192. $definition_update_manager = \Drupal::entityDefinitionUpdateManager();
  193. $entity_type = $definition_update_manager->getEntityType('comment');
  194. $keys = $entity_type->getKeys();
  195. $keys['published'] = 'status';
  196. $entity_type->set('entity_keys', $keys);
  197. $definition_update_manager->updateEntityType($entity_type);
  198. }
  199. /**
  200. * Update the status field.
  201. */
  202. function comment_update_8400() {
  203. // The status field was promoted to an entity key in comment_update_8301(),
  204. // which makes it NOT NULL in the default SQL storage, which means its storage
  205. // definition needs to be updated as well.
  206. $entity_definition_update_manager = \Drupal::service('entity.definition_update_manager');
  207. $entity_definition_update_manager->updateFieldStorageDefinition($entity_definition_update_manager->getFieldStorageDefinition('status', 'comment'));
  208. }
  209. /**
  210. * Configure the comment hostname base field to use a default value callback.
  211. */
  212. function comment_update_8600() {
  213. $entity_definition_update_manager = \Drupal::entityDefinitionUpdateManager();
  214. /** @var \Drupal\Core\Field\BaseFieldDefinition $field_storage_definition */
  215. $field_storage_definition = $entity_definition_update_manager->getFieldStorageDefinition('hostname', 'comment');
  216. $field_storage_definition->setDefaultValueCallback(Comment::class . '::getDefaultHostname');
  217. $entity_definition_update_manager->updateFieldStorageDefinition($field_storage_definition);
  218. }
  219. /**
  220. * Set the 'owner' entity key and update the field.
  221. */
  222. function comment_update_8700() {
  223. $definition_update_manager = \Drupal::entityDefinitionUpdateManager();
  224. $entity_type = $definition_update_manager->getEntityType('comment');
  225. $keys = $entity_type->getKeys();
  226. $keys['owner'] = 'uid';
  227. $entity_type->set('entity_keys', $keys);
  228. $definition_update_manager->updateEntityType($entity_type);
  229. $definition_update_manager->updateFieldStorageDefinition($definition_update_manager->getFieldStorageDefinition('uid', 'comment'));
  230. }
  231. /**
  232. * Make the 'entity_type' and 'field_name' comment fields required.
  233. */
  234. function comment_update_8701() {
  235. $definition_update_manager = \Drupal::entityDefinitionUpdateManager();
  236. $field_definition = $definition_update_manager->getFieldStorageDefinition('entity_type', 'comment');
  237. $field_definition->setRequired(TRUE);
  238. $definition_update_manager->updateFieldStorageDefinition($field_definition);
  239. $field_definition = $definition_update_manager->getFieldStorageDefinition('field_name', 'comment');
  240. $field_definition->setRequired(TRUE);
  241. $definition_update_manager->updateFieldStorageDefinition($field_definition);
  242. }