-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfaq.install
More file actions
429 lines (401 loc) · 14.1 KB
/
faq.install
File metadata and controls
429 lines (401 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
<?php
/**
* @file
* FAQ module install file.
*/
/**
* Define the 'faq_weights' and 'faq_questions' table structures.
*
* @return array
* The schema which contains the structure for the faq module's tables.
*/
function faq_schema() {
$schema['faq_weights'] = array(
'description' => 'A table containing the weight of each faq node by category.',
'fields' => array(
'tid' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => 'The primary identifier for a term or category. This will be 0 for non-categorized nodes.',
),
'nid' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => 'The primary identifier for a node.',
),
'weight' => array(
'type' => 'int',
'size' => 'tiny',
'not null' => TRUE,
'default' => 0,
'description' => 'A number representing the weight of a node. Nodes with lower weight values will appear above those with higher weight values.',
),
),
'primary key' => array('nid', 'tid'),
);
$schema['faq_questions'] = array(
'description' => 'A table containing the long question text of each faq node revision.',
'fields' => array(
'nid' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => 'The primary identifier for a node.',
),
'vid' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => 'The primary identifier for a node revision.',
),
'question' => array(
'type' => 'text',
'size' => 'normal',
'not null' => TRUE,
'description' => 'The faq short question text.',
),
'detailed_question' => array(
'type' => 'text',
'size' => 'normal',
'not null' => FALSE,
'description' => 'The faq long question text.',
),
),
'primary key' => array('nid', 'vid'),
);
return $schema;
}
/**
* Implements hook_install().
*
* Inserts the FAQ module's schema in the SQL database.
*/
function faq_install() {
$config = config('faq.settings');
$config->set('node_type_faq', array('status'));
$config->set('faq_description_format', filter_fallback_format());
$config->set('faq_description', array('value' => '', 'format' => filter_fallback_format()));
$config->save();
$t = get_t();
// Ensure the FAQ node type is available.
$faq_node_type = array(
'type' => 'faq',
'name' => t('FAQ'),
'base' => 'faq',
'description' => t('A frequently asked question and its answer.'),
'has_title' => TRUE,
'title_label' => t('Question'),
'custom' => 1,
'modified' => 1,
'locked' => 0,
'is_new' => TRUE,
'settings' => array(
'promote_enabled' => FALSE,
),
);
$faq_node_type = node_type_set_defaults($faq_node_type);
node_type_save($faq_node_type);
node_add_body_field($faq_node_type);
// Change the default label on the body field.
$body_instance = field_info_instance('node', 'body', 'faq');
$body_instance['label'] = $t('Answer');
field_update_instance($body_instance);
// Add the detailed question field.
_faq_add_custom_fields();
// Shift all fields below the body field one down and put detailed question field where the body field was.
_faq_shift_fields_down();
}
/**
* Implements hook_uninstall().
*
* Remove the nodes and schema corresponding to the FAQ module.
*/
function faq_uninstall() {
// Remove content type and the fields created.
$faq_type = 'faq';
$sql = 'SELECT nid FROM {node} n WHERE n.type = :type';
$result = db_query($sql, array(':type' => $faq_type));
$nodeids = array();
foreach ($result as $row) {
$nodeids[] = $row->nid;
}
node_delete_multiple($nodeids);
_faq_delete_custom_fields();
node_type_delete($faq_type);
field_purge_batch(500);
// Remove content type and the fields created.
$faq_type = 'faq';
$sql = 'SELECT nid FROM {node} n WHERE n.type = :type';
$result = db_query($sql, array(':type' => $faq_type));
$nodeids = array();
foreach ($result as $row) {
$nodeids[] = $row->nid;
}
node_delete_multiple($nodeids);
_faq_delete_custom_fields();
node_type_delete($faq_type);
field_purge_batch(500);
// Clear the cache tables.
cache_clear_all('*', 'cache', TRUE);
cache_clear_all('*', 'cache_filter', TRUE);
cache_clear_all('*', 'cache_menu', TRUE);
cache_clear_all('*', 'cache_page', TRUE);
}
/**
* Code examples modified.
*
* @see http://www.sitepoint.com/creating-a-new-drupal-node-type/
* @see http://www.thecarneyeffect.co.uk/creating-custom-content-type-adding-fields-programmatically-drupal-7
*/
function _faq_add_custom_fields() {
foreach (_faq_installed_fields() as $field) {
field_create_field($field);
}
foreach (_faq_installed_instances() as $fieldinstance) {
$fieldinstance['entity_type'] = 'node';
$fieldinstance['bundle'] = 'faq';
field_create_instance($fieldinstance);
}
}
/**
* Return the detailed question field definition.
*/
function _faq_installed_fields() {
$t = get_t();
return array(
'detailed_question' => array(
'translatable' => '0',
'entity_types' => array(),
'settings' => array(),
'storage' => array(
'type' => 'field_sql_storage',
'settings' => array(),
'module' => 'field_sql_storage',
'active' => '1',
'details' => array(
'sql' => array(
'FIELD_LOAD_CURRENT' => array(
'field_data_field_detailed_question' => array(
'value' => 'field_detailed_question_value',
'format' => 'field_detailed_question_format',
),
),
'FIELD_LOAD_REVISION' => array(
'field_revision_field_detailed_question' => array(
'value' => 'field_detailed_question_value',
'format' => 'field_detailed_question_format',
),
),
),
),
),
'foreign keys' => array(
'format' => array(
'table' => 'filter_format',
'columns' => array(
'format' => 'format',
),
),
),
'indexes' => array(
'format' => array(
'format',
),
),
'field_name' => 'field_detailed_question',
'type' => 'text_long',
'module' => 'text',
'active' => '1',
'locked' => '0',
'cardinality' => '1',
'deleted' => '0',
'columns' => array(
'value' => array(
'type' => 'text',
'size' => 'big',
'not null' => FALSE,
),
'format' => array(
'type' => 'varchar',
'length' => 255,
'not null' => FALSE,
),
),
'bundles' => array(
'node' => array(
'page',
),
),
),
);
}
/**
* Returns the detailed question field instance.
*/
function _faq_installed_instances() {
$t = get_t();
// Position in the question and answer page. Body (Answer) is 0 and Question (Title) is -4.
return array(
'detailed_question' => array(
'label' => 'Detailed question',
'widget' => array(
'weight' => '-3',
'type' => 'text_textarea',
'module' => 'text',
'active' => 1,
'settings' => array(
'rows' => '5',
),
),
'settings' => array(
'text_processing' => '1',
'user_register_form' => FALSE,
),
'display' => array(
'default' => array(
'label' => 'hidden',
'type' => 'text_default',
'weight' => '-3',
'settings' => array(),
'module' => 'text',
),
'teaser' => array(
'label' => 'hidden',
'type' => 'text_default',
'weight' => '-3',
'settings' => array(),
'module' => 'text',
),
),
'required' => 0,
'description' => 'Enter the detailed question text',
'default_value' => NULL,
'field_name' => 'field_detailed_question',
'entity_type' => 'node',
'bundle' => 'page',
'deleted' => '0',
),
);
}
/**
* Cleanup custom fields on uninstall.
*/
function _faq_delete_custom_fields() {
foreach (array_keys(_faq_installed_fields()) as $field) {
if (field_info_field($field)) {
field_delete_field($field);
}
}
$instances = field_info_instances('node', 'faq');
foreach ($instances as $instance_name => $fieldinstance) {
field_delete_instance($fieldinstance);
}
}
/**
* Shift fields down.
*/
function _faq_shift_fields_down() {
// Adjust the weight of the field so that it is above the answer (body).
$instance = field_read_instance('node', 'field_detailed_question', 'faq');
// Get all bundle instances.
$instances = field_info_instances('node', 'faq');
$body_widget_weight = $instances['body']['widget']['weight'];
$body_default_weight = $instances['body']['display']['default']['weight'];
$body_teaser_weight = $instances['body']['display']['teaser']['weight'];
// Move all of them one down so that.
foreach ($instances as $field => $settings) {
if ($settings['widget']['weight'] >= $body_widget_weight) {
$settings['widget']['weight']++;
}
if ($settings['display']['default']['weight'] >= $body_default_weight) {
$settings['display']['default']['weight']++;
}
if ($settings['display']['teaser']['weight'] >= $body_teaser_weight) {
$settings['display']['teaser']['weight']++;
}
field_update_instance($settings);
}
$instance['widget']['weight'] = $body_widget_weight;
$instance['display']['default']['weight'] = $body_default_weight;
$instance['display']['teaser']['weight'] = $body_teaser_weight;
field_update_instance($instance);
}
/**
* Implements hook_update_last_removed().
*/
function faq_update_last_removed() {
return 7003;
}
/**
* Migrate faq variables to config.
*/
function faq_update_1000() {
$config = config('faq.settings');
$config->set('faq_title', update_variable_get('faq_title', 'Frequently Asked Questions'));
$config->set('faq_description', update_variable_get('faq_description', array('value' => '', 'format' => filter_fallback_format())));
$config->set('faq_custom_breadcrumbs', update_variable_get('faq_custom_breadcrumbs', TRUE));
$config->set('faq_category_display', update_variable_get('faq_category_display', 'categories_inline'));
$config->set('faq_use_categories', update_variable_get('faq_use_categories', FALSE));
$config->set('faq_display', update_variable_get('faq_display', 'questions_top'));
$config->set('faq_question_listing', update_variable_get('faq_question_listing', 'ul'));
$config->set('faq_qa_mark', update_variable_get('faq_qa_mark', array()));
$config->set('faq_question_label', update_variable_get('faq_question_label', 'Q:'));
$config->set('faq_answer_label', update_variable_get('faq_answer_label', 'A:'));
$config->set('faq_question_long_form', update_variable_get('faq_question_long_form', '1'));
$config->set('faq_question_length', update_variable_get('faq_question_length', 'short'));
$config->set('faq_hide_qa_accordion', update_variable_get('faq_hide_qa_accordion', array()));
$config->set('faq_show_expand_all', update_variable_get('faq_show_expand_all', array()));
$config->set('faq_use_teaser', update_variable_get('faq_use_teaser', array()));
$config->set('faq_show_node_links', update_variable_get('faq_show_node_links', array()));
$config->set('faq_back_to_top', update_variable_get('faq_back_to_top', 'Back to Top'));
$config->set('faq_disable_node_links', update_variable_get('faq_disable_node_links', array()));
$config->set('faq_default_sorting', update_variable_get('faq_default_sorting', 'DESC'));
$config->set('faq_omit_vocabulary', update_variable_get('faq_omit_vocabulary', array()));
$config->set('faq_category_listing', update_variable_get('faq_category_listing', 'ul'));
$config->set('faq_category_hide_qa_accordion', update_variable_get('faq_category_hide_qa_accordion', array()));
$config->set('faq_count', update_variable_get('faq_count', array()));
$config->set('faq_answer_category_name', update_variable_get('faq_answer_category_name', array()));
$config->set('faq_group_questions_top', update_variable_get('faq_group_questions_top', array()));
$config->set('faq_hide_child_terms', update_variable_get('faq_hide_child_terms', array()));
$config->set('faq_show_term_page_children', update_variable_get('faq_show_term_page_children', array()));
$config->set('faq_description_format', update_variable_get('faq_description_format', filter_fallback_format()));
$config->set('faq_auto_generate_alias', update_variable_get('faq_auto_generate_alias', TRUE));
$config->set('faq_path', update_variable_get('faq_path', 'faq-page'));
$config->save();
update_variable_del('faq_title');
update_variable_del('faq_description');
update_variable_del('faq_custom_breadcrumbs');
update_variable_del('faq_category_display');
update_variable_del('faq_use_categories');
update_variable_del('faq_display');
update_variable_del('faq_question_listing');
update_variable_del('faq_qa_mark');
update_variable_del('faq_question_label');
update_variable_del('faq_answer_label');
update_variable_del('faq_question_long_form');
update_variable_del('faq_question_length');
update_variable_del('faq_hide_qa_accordion');
update_variable_del('faq_show_expand_all');
update_variable_del('faq_use_teaser');
update_variable_del('faq_show_node_links');
update_variable_del('faq_back_to_top');
update_variable_del('faq_disable_node_links');
update_variable_del('faq_default_sorting');
update_variable_del('faq_omit_vocabulary');
update_variable_del('faq_category_listing');
update_variable_del('faq_category_hide_qa_accordion');
update_variable_del('faq_count');
update_variable_del('faq_answer_category_name');
update_variable_del('faq_group_questions_top');
update_variable_del('faq_hide_child_terms');
update_variable_del('faq_show_term_page_children');
update_variable_del('faq_description_format');
update_variable_del('faq_auto_generate_alias');
update_variable_del('faq_path');
}