forum.install 7.19 KB
Newer Older
1
2
<?php

3
4
/**
 * @file
5
 * Install, update, and uninstall functions for the Forum module.
6
7
 */

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

19
20
21
  // Create the 'taxonomy_forums' field if it doesn't already exist. If forum
  // is being enabled at the same time as taxonomy after both modules have been
  // enabled, the field might exist but still be marked inactive.
22
  if (!field_info_field('node', 'taxonomy_forums')) {
23
    entity_create('field_config', array(
24
25
      'name' => 'taxonomy_forums',
      'entity_type' => 'node',
26
27
28
29
      'type' => 'taxonomy_term_reference',
      'settings' => array(
        'allowed_values' => array(
          array(
30
            'vocabulary' => 'forums',
31
32
33
34
            'parent' => 0,
          ),
        ),
      ),
35
    ))->save();
36

37
    // Create a default forum so forum posts can be created.
38
    $term = entity_create('taxonomy_term', array(
39
40
41
      'name' => t('General discussion'),
      'description' => '',
      'parent' => array(0),
42
      'vid' => 'forums',
43
      'forum_container' => 0,
44
    ));
45
    $term->save();
46

47
    // Create the instance on the bundle.
48
    entity_create('field_instance_config', array(
49
      'field_name' => 'taxonomy_forums',
50
      'entity_type' => 'node',
51
      'label' => 'Forums',
52
53
      'bundle' => 'forum',
      'required' => TRUE,
54
    ))->save();
55

56
57
58
59
60
61
62
    // Assign form display settings for the 'default' form mode.
    entity_get_form_display('node', 'forum', 'default')
      ->setComponent('taxonomy_forums', array(
        'type' => 'options_select',
      ))
      ->save();

63
64
65
66
67
68
69
70
71
72
73
74
75
    // Assign display settings for the 'default' and 'teaser' view modes.
    entity_get_display('node', 'forum', 'default')
      ->setComponent('taxonomy_forums', array(
        'type' => 'taxonomy_term_reference_link',
        'weight' => 10,
      ))
      ->save();
    entity_get_display('node', 'forum', 'teaser')
      ->setComponent('taxonomy_forums', array(
        'type' => 'taxonomy_term_reference_link',
        'weight' => 10,
      ))
      ->save();
76
  }
77
  // Add the comment field to the forum node type.
78
  $fields = entity_load_multiple_by_properties('field_config', array(
79
80
81
82
83
    'type' => 'comment',
    'name' => 'comment_forum',
    'include_deleted' => FALSE,
  ));
  if (empty($fields)) {
84
    Drupal::service('comment.manager')->addDefaultField('node', 'forum', 'comment_forum');
85
  }
86
}
87

88
/**
89
 * Implements hook_module_preinstall().
90
 */
91
function forum_module_preinstall($module) {
92
  $list_boolean = \Drupal::service('plugin.manager.field.field_type')->getDefinition('list_boolean');
93
  if (empty($list_boolean) && $module == 'forum') {
94
95
96
97
98
99
    // Make sure that the list_boolean field type is available before our
    // default config is installed.
    field_info_cache_clear();
  }
}

100
/**
101
 * Implements hook_uninstall().
102
103
 */
function forum_uninstall() {
104
  if ($field = field_info_field('node', 'taxonomy_forums')) {
105
106
107
    $field->delete();
  }

108
109
110
111
  if ($field = field_info_field('node', 'comment_forum')) {
    $field->delete();
  }

112
113
114
115
116
117
118
119
120
  if ($field = field_info_field('taxonomy_term', 'forum_container')) {
    $field->delete();
  }

  // Purge field data now to allow taxonomy and options module to be uninstalled
  // if this is the only field remaining. We need to run it twice because
  // field_purge_batch() will not remove the instance and the field in the same
  // pass.
  field_purge_batch(10);
121
  field_purge_batch(10);
122
  // Allow to delete a forum's node type.
123
  $locked = \Drupal::state()->get('node.type.locked');
124
  unset($locked['forum']);
125
  \Drupal::state()->set('node.type.locked', $locked);
126
}
127
128

/**
129
 * Implements hook_schema().
130
131
132
 */
function forum_schema() {
  $schema['forum'] = array(
133
    'description' => 'Stores the relationship of nodes to forum terms.',
134
    'fields' => array(
135
136
137
138
139
      'nid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
140
        'description' => 'The {node}.nid of the node.',
141
142
143
144
145
146
      ),
      'vid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
147
        'description' => 'Primary Key: The {node}.vid of the node.',
148
149
150
151
152
153
      ),
      'tid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
154
        'description' => 'The {taxonomy_term_data}.tid of the forum term assigned to the node.',
155
      ),
156
157
    ),
    'indexes' => array(
158
      'forum_topic' => array('nid', 'tid'),
159
      'tid' => array('tid'),
160
161
    ),
    'primary key' => array('vid'),
162
    'foreign keys' => array(
163
164
165
166
167
168
169
      'forum_node' => array(
        'table' => 'node',
        'columns' => array(
          'nid' => 'nid',
          'vid' => 'vid',
        ),
      ),
170
    ),
171
172
  );

173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
  $schema['forum_index'] = array(
    'description' => 'Maintains denormalized information about node/term relationships.',
    'fields' => array(
      'nid' => array(
        'description' => 'The {node}.nid this record tracks.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'title' => array(
        'description' => 'The title of this node, always treated as non-markup plain text.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'tid' => array(
         'description' => 'The term ID.',
         'type' => 'int',
         'unsigned' => TRUE,
         'not null' => TRUE,
         'default' => 0,
      ),
      'sticky' => array(
        'description' => 'Boolean indicating whether the node is sticky.',
        'type' => 'int',
        'not null' => FALSE,
        'default' => 0,
        'size' => 'tiny',
      ),
      'created' => array(
        'description' => 'The Unix timestamp when the node was created.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
209
        'default' => 0,
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
      ),
      'last_comment_timestamp' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'description' => 'The Unix timestamp of the last comment that was posted within this node, from {comment}.timestamp.',
      ),
      'comment_count' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => 'The total number of comments on this node.',
      ),
    ),
    'indexes' => array(
226
      'forum_topics' => array('nid', 'tid', 'sticky', 'last_comment_timestamp'),
227
228
      'created' => array('created'),
      'last_comment_timestamp' => array('last_comment_timestamp'),
229
230
    ),
    'foreign keys' => array(
231
232
233
234
235
236
237
238
239
240
      'tracked_node' => array(
        'table' => 'node',
        'columns' => array('nid' => 'nid'),
      ),
      'term' => array(
        'table' => 'taxonomy_term_data',
        'columns' => array(
          'tid' => 'tid',
        ),
      ),
241
242
243
    ),
  );

244
245
  return $schema;
}
246
247
248
249
250

/**
 * Implements hook_update_last_removed().
 */
function forum_update_last_removed() {
251
  return 7003;
252
}