-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom_module.install
200 lines (171 loc) · 4.88 KB
/
custom_module.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
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
<?php
/**
* @file
* Custom Module installation
*/
/**
* Implements hook_install().
*/
function custom_module_install() {
// Set default variables.
variable_set('custom_module_gmap', 1);
variable_set('custom_module_default_center_lat', 42.91455);
variable_set('custom_module_default_center_long', -75.569851);
variable_set('custom_module_default_gmap_zoom', 8);
// Get localization function for installation as t() may be unavailable.
$t = get_t();
// Give user feedback.
drupal_set_message($t('Custom Module variables created.'));
}
// Content type definition.
$content_type = array(
'type' => 'custom_module',
'name' => $t('Custom Module'),
'description' => $t('A Custom Module, including Fields.'),
'title_label' => $t('Title or Name'),
'base' => 'node_content',
'custom' => TRUE,
);
// Set remaining definitions with defaults.
$node_type = node_type_set_defaults($content_type);
// Save the content type.
node_type_save($node_type);
}
// Add a field for the body.
node_add_body_field($node_type, $t('Description'));
// Create fields.
$fields = array();
$fields['custom_module_unit_count'] = array(
'field_name' => 'custom_module_unit_count',
'type' => 'number_integer',
// Optional.
'cardinality' => 1,
'settings' => array(
'max_length' => 5,
),
);
$fields['custom_module_latitude'] = array(
'field_name' => 'custom_module_latitude',
'type' => 'number_float',
'settings' => array(
'max_length' => 20,
),
);
$fields['custom_module_longitude'] = array(
'field_name' => 'custom_module_longitude',
'type' => 'number_float',
'settings' => array(
'max_length' => 20,
),
);
$fields['custom_module_city'] = array(
'field_name' => 'custom_module_city',
'type' => 'text',
'settings' => array(
'max_length' => 60,
),
);
foreach ($fields as $field) {
field_create_field($field);
}
// Create Field Instances.
$instances = array();
$instances['custom_module_unit_count'] = array(
'field_name' => 'custom_module_unit_count',
'label' => $t('Number of Units'),
'description' => $t('Number of individual units.'),
'widget' => array(
'type' => 'text_textfield',
),
'required' => TRUE,
'settings' => array(
'text_processing' => 0,
),
);
$instances['custom_module_latitude'] = array(
'field_name' => 'custom_module_latitude',
'label' => $t('Latitude'),
'description' => $t('Signed degrees format (DDD.dddd)'),
'widget' => array(
'type' => 'text_textfield',
),
'settings' => array(
'text_processing' => 0,
),
'display' => array(
'default' => array(
'type' => 'hidden',
),
),
);
$instances['custom_module_longitude'] = array(
'field_name' => 'custom_module_longitude',
'label' => $t('Longitude'),
'description' => $t('Signed degrees format (DDD.dddd)'),
'widget' => array(
'type' => 'text_textfield',
),
'settings' => array(
'text_processing' => 0,
),
'display' => array(
'default' => array(
'type' => 'hidden',
),
),
);
$instances['custom_module_city'] = array(
'field_name' => 'custom_module_city',
'label' => $t('The City'),
'description' => $t('The name of the City'),
'widget' => array(
'type' => 'text_textfield',
),
'display' => array(
'default' => array(
'label' => 'inline',
),
),
);
foreach ($instances as $instance) {
$instance['entity_type'] = 'node';
$instance['bundle'] = 'custom_module';
field_create_instance($instance);
}
}
/**
* Implements hook_uninstall().
*/
function custom_module_uninstall() {
// Delete variables.
variable_del('custom_module_gmap');
variable_del('custom_module_default_center_lat');
variable_del('custom_module_default_center_long');
variable_del('custom_module_default_gmap_zoom');
// Inform the user of the removal.
$t = get_t();
drupal_set_message($t('Custom Module variables removed.'));
//Get all node IDs with "custom module" content type.
$sql_query = 'SELECT nid ';
$sql_query .= 'FROM {node} ';
$sql_query .= 'WHERE {node}.type = :type ';
$result = db_query($sql_query, array(':type' => 'custom_module'))
$nids = array();
foreach ($result as $row) {
$nids[] = $row->nid;
}
//Delete all custom_module content
node_delete_multiple($nids)
drupal_set_message($t('Custom Codule content removed.'))
// Remove all fields and field instances.
foreach (field_info_instances('node', 'custom_module') as $field_name => $instance) {
field_delete_field($field_name);
field_delete_instance($instance);
}
drupal_set_message($t('Custom Module field and field instances removed.'));
// Delete the content type.
node_type_delete('custom_module');
drupal_set_message($t('Custom Module Content Type removed.'));
// Clean up deleted fields.
field_purge_batch(1000);
}