-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathding_event.install
More file actions
78 lines (70 loc) · 2.14 KB
/
ding_event.install
File metadata and controls
78 lines (70 loc) · 2.14 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
<?php
/**
* @file
* Update functions for the ding_event module.
*/
/**
* Implements hook_update_N().
*
* Add end date to the field.
*/
function ding_event_update_7001(&$sandbox) {
return date_field_update_add_end_date('field_event_date');
}
/**
* Helper function.
*
* Allows to update a date field (without deleting existing content) to allow
* input of an end date.
*
* @param string $field_name
* machine name of the field to be updated.
*
* @return string
* Message that will be show when update is finished.
*/
function date_field_update_add_end_date($field_name) {
$field_info = field_info_field($field_name);
if (!$field_info) {
return t('Field "@fld" doesn\'t exists. Update aborted.', array('@fld' => $field_name));
}
// Remove non-db settings.
$non_db_settings = array(
'field_name',
'type',
'module',
'active',
'locked',
'cardinality',
'deleted',
'columns',
'bundles',
);
$to_column = $field_info['columns']['value'];
foreach ($non_db_settings as $setting) {
unset($field_info[$setting]);
}
unset($field_info['storage']['details']);
// Add end date setting.
$field_info['settings']['todate'] = 'optional';
$update_ok = db_update('field_config')
->fields(array('data' => serialize($field_info)))
->condition('field_name', $field_name)
->execute();
// Alter field value storage tables (data and revisions).
$field_data_table = 'field_data_' . $field_name;
$field_rev_table = 'field_revision_' . $field_name;
$field = $field_name . '_value2';
db_add_field($field_data_table, $field, $to_column);
db_add_field($field_rev_table, $field, $to_column);
if ($update_ok && db_field_exists($field_data_table, $field) && db_field_exists($field_rev_table, $field)) {
// Set default value for "to" date that equals "from".
db_update($field_data_table)->expression($field, $field_name . '_value')->execute();
db_update($field_rev_table)->expression($field, $field_name . '_value')->execute();
cache_clear_all();
return t('Field "@fld" updated successfully', array('@fld' => $field_name));
}
else {
return t('Update failed!');
}
}