-
Notifications
You must be signed in to change notification settings - Fork 11
/
efq_example.install
126 lines (120 loc) · 3.58 KB
/
efq_example.install
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
<?php
/**
* @file
* Install file for efq_example
*/
/**
* Implements hook_install().
* Install all ctypes handled by this module.
*/
function efq_example_install() {
// An array of content type machine names to install.
$ctypes = efq_example_ctypes();
efq_example_install_fields($ctypes);
efq_example_place_example_block();
}
/**
* Implements hook_uninstall().
*/
function efq_example_uninstall() {
$ctypes = efq_example_ctypes();
efq_example_uninstall_ctypes($ctypes);
}
/**
* List of ctypes handled by this module;
* this list should be complete for all ctypes handled by this module
*/
function efq_example_ctypes() {
$ctypes = array(
'efq_page',
'efq_article',
'efq_photo',
);
return $ctypes;
}
/**
* Install fields for a given ctype
*
* @param array $ctypes
* an array of ctypes
*/
function efq_example_install_fields($ctypes, $module = 'efq_example') {
// Make sure the system is aware of new ctypes.
node_types_rebuild();
foreach ($ctypes as $ctype) {
module_load_include('inc', $module, "fields/${module}.fields." . $ctype);
$fields_function = "${module}_${ctype}_fields";
foreach ($fields_function() as $field) {
// Create the field if it doesn't exist.
if (!field_info_field($field['field_config']['field_name'])) {
field_create_field($field['field_config']);
}
// Create the field instance if it doesn't exist.
if (!field_info_instance('node', $field['field_instance']['field_name'], $ctype)) {
field_create_instance($field['field_instance']);
}
}
}
}
/**
* Uninstall a set of ctypes
* This is stolen shamelessly from Pro Drupal Development 7.
*
* @param array $ctypes
* an array of ctypes
* @param string $module
* module whose types to uninstall
*/
function efq_example_uninstall_ctypes($ctypes, $module = 'efq_example') {
foreach ($ctypes as $ctype) {
// Gather nids of all nodes of this type.
$nids = db_query("SELECT n.nid FROM {node} n WHERE n.type = :type", array(':type' => $ctype))->fetchCol();
// Delete all nodes of this type.
node_delete_multiple($nids);
// Delete any fields defined by this module for this ctype;
// this is tricky; we need to leave behind fields that are used elsewhere
// relying on our convention of field_$ctype_* for ctype-specific
// fields here; anything else is something like body or group_audience.
module_load_include('inc', $module, "fields/${module}.fields." . $ctype);
$fields_function = "${module}_${ctype}_fields";
foreach ($fields_function() as $field) {
if (preg_match("/^field_${ctype}/", $field['field_config']['field_name'])) {
field_delete_field($field['field_config']['field_name']);
}
}
// Delete any remaining instances associated with this ctype.
$instances = field_info_instances('node', $ctype);
foreach ($instances as $instance) {
field_delete_instance($instance);
}
// Delete the ctype.
node_type_delete($ctype);
// Purge field info.
field_purge_batch(1000);
}
}
/**
* example block
*/
function efq_example_place_example_block() {
$values = array(
array(
'module' => 'efq_example',
'delta' => 'content_by_state',
'theme' => variable_get('theme_default', 'bartik'),
'status' => 1,
'weight' => 0,
'region' => 'sidebar_first',
'pages' => '',
'cache' => -1,
),
);
$query = db_insert('block')->fields(array(
'module', 'delta', 'theme',
'status', 'weight', 'region',
'pages', 'cache'));
foreach ($values as $record) {
$query->values($record);
}
$query->execute();
}