forum.install 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /**
  3. * @file
  4. * Install, update, and uninstall functions for the Forum module.
  5. */
  6. use Drupal\field\Entity\FieldStorageConfig;
  7. use Drupal\taxonomy\Entity\Term;
  8. /**
  9. * Implements hook_install().
  10. */
  11. function forum_install() {
  12. // Set the weight of the forum.module to 1 so it is loaded after the taxonomy.module.
  13. module_set_weight('forum', 1);
  14. // Do not allow to delete the forum's node type machine name.
  15. $locked = \Drupal::state()->get('node.type.locked');
  16. $locked['forum'] = 'forum';
  17. \Drupal::state()->set('node.type.locked', $locked);
  18. if (!\Drupal::service('config.installer')->isSyncing()) {
  19. // Create a default forum so forum posts can be created.
  20. $term = Term::create([
  21. 'name' => t('General discussion'),
  22. 'description' => '',
  23. 'parent' => [0],
  24. 'vid' => 'forums',
  25. 'forum_container' => 0,
  26. ]);
  27. $term->save();
  28. }
  29. }
  30. /**
  31. * Implements hook_uninstall().
  32. */
  33. function forum_uninstall() {
  34. if ($field_storage = FieldStorageConfig::loadByName('node', 'taxonomy_forums')) {
  35. $field_storage->delete();
  36. }
  37. if ($field_storage = FieldStorageConfig::loadByName('node', 'comment_forum')) {
  38. $field_storage->delete();
  39. }
  40. if ($field_storage = FieldStorageConfig::loadByName('taxonomy_term', 'forum_container')) {
  41. $field_storage->delete();
  42. }
  43. // Purge field data now to allow taxonomy and options module to be uninstalled
  44. // if this is the only field remaining.
  45. field_purge_batch(10);
  46. // Allow to delete a forum's node type.
  47. $locked = \Drupal::state()->get('node.type.locked');
  48. unset($locked['forum']);
  49. \Drupal::state()->set('node.type.locked', $locked);
  50. }
  51. /**
  52. * Implements hook_schema().
  53. */
  54. function forum_schema() {
  55. $schema['forum'] = [
  56. 'description' => 'Stores the relationship of nodes to forum terms.',
  57. 'fields' => [
  58. 'nid' => [
  59. 'type' => 'int',
  60. 'unsigned' => TRUE,
  61. 'not null' => TRUE,
  62. 'default' => 0,
  63. 'description' => 'The {node}.nid of the node.',
  64. ],
  65. 'vid' => [
  66. 'type' => 'int',
  67. 'unsigned' => TRUE,
  68. 'not null' => TRUE,
  69. 'default' => 0,
  70. 'description' => 'Primary Key: The {node}.vid of the node.',
  71. ],
  72. 'tid' => [
  73. 'type' => 'int',
  74. 'unsigned' => TRUE,
  75. 'not null' => TRUE,
  76. 'default' => 0,
  77. 'description' => 'The {taxonomy_term_data}.tid of the forum term assigned to the node.',
  78. ],
  79. ],
  80. 'indexes' => [
  81. 'forum_topic' => ['nid', 'tid'],
  82. 'tid' => ['tid'],
  83. ],
  84. 'primary key' => ['vid'],
  85. 'foreign keys' => [
  86. 'forum_node' => [
  87. 'table' => 'node',
  88. 'columns' => [
  89. 'nid' => 'nid',
  90. 'vid' => 'vid',
  91. ],
  92. ],
  93. ],
  94. ];
  95. $schema['forum_index'] = [
  96. 'description' => 'Maintains denormalized information about node/term relationships.',
  97. 'fields' => [
  98. 'nid' => [
  99. 'description' => 'The {node}.nid this record tracks.',
  100. 'type' => 'int',
  101. 'unsigned' => TRUE,
  102. 'not null' => TRUE,
  103. 'default' => 0,
  104. ],
  105. 'title' => [
  106. 'description' => 'The node title.',
  107. 'type' => 'varchar',
  108. 'length' => 255,
  109. 'not null' => TRUE,
  110. 'default' => '',
  111. ],
  112. 'tid' => [
  113. 'description' => 'The term ID.',
  114. 'type' => 'int',
  115. 'unsigned' => TRUE,
  116. 'not null' => TRUE,
  117. 'default' => 0,
  118. ],
  119. 'sticky' => [
  120. 'description' => 'Boolean indicating whether the node is sticky.',
  121. 'type' => 'int',
  122. 'not null' => FALSE,
  123. 'default' => 0,
  124. 'size' => 'tiny',
  125. ],
  126. 'created' => [
  127. 'description' => 'The Unix timestamp when the node was created.',
  128. 'type' => 'int',
  129. 'unsigned' => TRUE,
  130. 'not null' => TRUE,
  131. 'default' => 0,
  132. ],
  133. 'last_comment_timestamp' => [
  134. 'type' => 'int',
  135. 'not null' => TRUE,
  136. 'default' => 0,
  137. 'description' => 'The Unix timestamp of the last comment that was posted within this node, from {comment}.timestamp.',
  138. ],
  139. 'comment_count' => [
  140. 'type' => 'int',
  141. 'unsigned' => TRUE,
  142. 'not null' => TRUE,
  143. 'default' => 0,
  144. 'description' => 'The total number of comments on this node.',
  145. ],
  146. ],
  147. 'indexes' => [
  148. 'forum_topics' => ['nid', 'tid', 'sticky', 'last_comment_timestamp'],
  149. 'created' => ['created'],
  150. 'last_comment_timestamp' => ['last_comment_timestamp'],
  151. ],
  152. 'foreign keys' => [
  153. 'tracked_node' => [
  154. 'table' => 'node',
  155. 'columns' => ['nid' => 'nid'],
  156. ],
  157. 'term' => [
  158. 'table' => 'taxonomy_term_data',
  159. 'columns' => [
  160. 'tid' => 'tid',
  161. ],
  162. ],
  163. ],
  164. ];
  165. return $schema;
  166. }