/
farm_equipment.install
171 lines (150 loc) · 5.19 KB
/
farm_equipment.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
<?php
/**
* @file
* Farm equipment install file.
*/
/**
* Implements hook_update_dependencies().
*/
function farm_equipment_update_dependencies() {
// Update 7002 (add new movement field to maintenance logs) depends on
// farm_movement_update_7000().
$dependencies['farm_equipment'][7002] = array('farm_movement' => 7000);
return $dependencies;
}
/**
* Migrate Equipment Description field to plain Description field (from farm_fields).
*/
function farm_equipment_update_7000(&$sandbox) {
// Revert this module's field_instance Features component.
features_revert(array('farm_equipment' => array('field_instance')));
// If the new description field database tables exist...
if (db_table_exists('field_data_field_farm_description') && db_table_exists('field_revision_field_farm_description')) {
// Copy all descriptions from the old database tables to the new ones.
db_query('INSERT INTO {field_data_field_farm_description} SELECT * FROM {field_data_field_farm_equipment_description}');
db_query('INSERT INTO {field_revision_field_farm_description} SELECT * FROM {field_revision_field_farm_equipment_description}');
// Delete the old field.
$field = field_info_instance('farm_asset', 'field_farm_equipment_description', 'equipment');
field_delete_instance($field);
}
}
/**
* Migrate equipment reference field to new asset reference field.
*/
function farm_equipment_update_7001(&$sandbox) {
// Revert this module's field_instance Features component.
features_revert(array('farm_equipment' => array('field_instance')));
// Copy all field data from the old database tables to the new ones.
db_query("INSERT INTO {field_data_field_farm_asset} (SELECT * FROM {field_data_field_farm_equipment} WHERE entity_type = 'log' AND bundle = 'farm_maintenance')");
db_query("INSERT INTO {field_revision_field_farm_asset} (SELECT * FROM {field_revision_field_farm_equipment} WHERE entity_type = 'log' AND bundle = 'farm_maintenance')");
// Delete the old equipment reference field.
$field = field_info_instance('log', 'field_farm_equipment', 'farm_maintenance');
field_delete_instance($field);
}
/**
* Add new movement field to maintenance logs.
*/
function farm_equipment_update_7002(&$sandbox) {
features_revert(array('farm_equipment' => array('field_instance')));
}
/**
* Add new equipment fields: manufacturer, model, serial number.
*/
function farm_equipment_update_7003(&$sandbox) {
features_revert(array('farm_equipment' => array('field_base', 'field_instance')));
}
/**
* Add new "Equipment used" field to logs.
*/
function farm_equipment_update_7004(&$sandbox) {
// Load field types.
$field_types = field_info_fields();
// Create the equipment field base, if it doesn't already exist.
if (empty($field_types['field_farm_equipment'])) {
$field_base = array(
'active' => 1,
'cardinality' => -1,
'deleted' => 0,
'entity_types' => array(),
'field_name' => 'field_farm_equipment',
'indexes' => array(
'target_id' => array(
0 => 'target_id',
),
),
'locked' => 0,
'module' => 'entityreference',
'settings' => array(
'handler' => 'base',
'handler_settings' => array(
'behaviors' => array(
'views-select-list' => array(
'status' => 0,
),
),
'sort' => array(
'type' => 'none',
),
'target_bundles' => array(
'equipment'
),
),
'target_type' => 'farm_asset',
),
'translatable' => 0,
'type' => 'entityreference',
);
field_create_field($field_base);
}
// Load field instances.
$field_instances = field_info_instances();
// Create new field instances on log types.
$log_types = log_type_get_names();
foreach ($log_types as $bundle => $bundle_info) {
// If the instance already exists, skip it.
if (!empty($field_instances['log'][$bundle]['field_farm_equipment'])) {
continue;
}
// Create a field instance.
$field_instance = array(
'bundle' => $bundle,
'deleted' => 0,
'description' => 'What equipment was used?',
'display' => array(
'default' => array(
'label' => 'inline',
'module' => 'entityreference',
'settings' => array(
'bypass_access' => FALSE,
'link' => 1,
),
'type' => 'entityreference_label',
'weight' => 0,
),
),
'entity_type' => 'log',
'field_name' => 'field_farm_equipment',
'label' => t('Equipment used'),
'required' => 0,
'settings' => array(
'user_register_form' => FALSE,
),
'widget' => array(
'active' => 1,
'module' => 'entityreference_view_widget',
'settings' => array(
'allow_duplicates' => 0,
'close_modal' => 1,
'pass_argument' => 1,
'pass_arguments' => '',
'rendered_entity' => 0,
'view' => 'farm_asset_entityreference_view|entityreference_view_widget',
'view_mode' => 'full',
),
'type' => 'entityreference_view_widget',
'weight' => 2,
),
);
field_create_instance($field_instance);
}
}