forked from backdrop-contrib/entity_token
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentity_token.module
199 lines (186 loc) · 7.51 KB
/
entity_token.module
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
<?php
/**
* @file
* Module file for the entity tokens module.
*/
/**
* Implements hook_field_info_alter().
*/
function entity_token_field_info_alter(&$info) {
foreach (array('date', 'datetime', 'datestamp') as $date_type) {
if (isset($info[$date_type])) {
$info[$date_type]['property_type'] = 'date';
$info[$date_type]['property_callbacks'][] = 'date_entity_metadata_property_info_alter';
}
}
}
/**
* Callback to alter the property info of date fields.
*
* @see entity_token_field_info_alter()
*/
function date_entity_metadata_property_info_alter(&$info, $entity_type, $field, $instance, $field_type) {
$name = $field['field_name'];
$property = &$info[$entity_type]['bundles'][$instance['bundle']]['properties'][$name];
if ($field['type'] != 'datestamp' || $field['settings']['timezone_db'] != 'UTC') {
// Add a getter callback to convert the date into the right format.
$property['getter callback'] = 'date_entity_metadata_field_getter';
$property['setter callback'] = 'date_entity_metadata_field_setter';
unset($property['query callback']);
}
if (!empty($field['settings']['todate'])) {
// Define a simple data structure containing both dates.
$property['type'] = ($field['cardinality'] != 1) ? 'list<struct>' : 'struct';
$property['auto creation'] = 'date_entity_metadata_struct_create';
$property['getter callback'] = 'entity_metadata_field_verbatim_get';
$property['setter callback'] = 'entity_metadata_field_verbatim_set';
$property['property info'] = array(
'value' => array(
'type' => 'date',
'label' => t('Start date'),
'getter callback' => 'date_entity_metadata_struct_getter',
'setter callback' => 'date_entity_metadata_struct_setter',
// The getter and setter callbacks for 'value' and 'value2'
// will not provide the field name as $name, we'll add it to $info.
'field_name' => $field['field_name'],
// Alert Microdata module that this value can be exposed in microdata.
'microdata' => TRUE,
),
'value2' => array(
'type' => 'date',
'label' => t('End date'),
'getter callback' => 'date_entity_metadata_struct_getter',
'setter callback' => 'date_entity_metadata_struct_setter',
// The getter and setter callbacks for 'value' and 'value2'
// will not provide the field name as $name, we'll add it to $info.
'field_name' => $field['field_name'],
// Alert Microdata module that this value can be exposed in microdata.
'microdata' => TRUE,
),
'duration' => array(
'type' => 'duration',
'label' => t('Duration'),
'desription' => t('The duration of the time period given by the dates.'),
'getter callback' => 'date_entity_metadata_duration_getter',
// No setter callback for duration.
// The getter callback for duration will not provide the field name
// as $name, we'll add it to $info.
'field_name' => $field['field_name'],
),
);
unset($property['query callback']);
}
else {
// If this doesn't have a todate, it is handled as a date rather than a
// struct. Enable microdata on the field itself rather than the properties.
$property['microdata'] = TRUE;
}
}
/**
* Getter callback to return date values as datestamp in UTC from the field.
*/
function date_entity_metadata_field_getter($entity, array $options, $name, $entity_type, &$context) {
$return = entity_plus_metadata_field_verbatim_get($entity, $options, $name, $entity_type, $context);
$items = ($context['field']['cardinality'] == 1) ? array($return) : $return;
foreach ($items as $key => $item) {
$items[$key] = date_entity_metadata_struct_getter($item, $options, 'value', 'struct', $context);
}
return ($context['field']['cardinality'] == 1) ? $items[0] : $items;
}
/**
* Getter callback to return date values as datestamp in UTC.
*/
function date_entity_metadata_struct_getter($item, array $options, $name, $type, $info) {
$value = trim($item[$name]);
if (empty($value)) {
return NULL;
}
$timezone_db = !empty($item['timezone_db']) ? $item['timezone_db'] : 'UTC';
$date = new BackdropDateTime($value, $timezone_db);
return !empty($date) ? date_format_date($date, 'custom', 'U') : NULL;
}
/**
* Getter callback to return the duration of the time period given by the dates.
*/
function date_entity_metadata_duration_getter($item, array $options, $name, $type, $info) {
$value = date_entity_metadata_struct_getter($item, $options, 'value', 'struct', $info);
$value2 = date_entity_metadata_struct_getter($item, $options, 'value2', 'struct', $info);
if ($value && $value2) {
return $value2 - $value;
}
}
/**
* Callback for setting field property values.
*
* Based on entity_metadata_field_property_set(), the original property setter,
* adapted to transform non-timestamp date values to timestamps.
*/
function date_entity_metadata_field_setter(&$entity, $name, $value, $langcode, $entity_type, $info) {
$field = field_info_field($name);
if (!isset($langcode)) {
// Try to figure out the default language used by the entity.
// @todo: Update once http://backdrop.org/node/1260640 has been fixed.
$langcode = isset($entity->language) ? $entity->language : LANGUAGE_NONE;
}
$values = $field['cardinality'] == 1 ? array($value) : (array) $value;
$items = array();
foreach ($values as $delta => $value) {
// Make use of the struct setter to convert the date back to a timestamp.
$info['field_name'] = $name;
date_entity_metadata_struct_setter($items[$delta], 'value', $value, $langcode, 'struct', $info);
}
$entity->{$name}[$langcode] = $items;
// Empty the static field language cache, so the field system picks up any
// possible new languages.
backdrop_static_reset('field_language');
}
/**
* Auto creation callback for fields which contain two date values in one.
*/
function date_entity_metadata_struct_create($name, $property_info) {
return array(
'date_type' => $property_info['field']['columns'][$name]['type'],
'timezone_db' => $property_info['field']['settings']['timezone_db'],
);
}
/**
* Callback for setting an individual field value if a to-date may be there too.
*
* Based on entity_property_verbatim_set().
*
* The passed in unix timestamp (UTC) is converted to the right value and format dependent on the field.
*
* $name is either 'value' or 'value2'.
*/
function date_entity_metadata_struct_setter(&$item, $name, $value, $langcode, $type, $info) {
if (!isset($value)) {
$item[$name] = NULL;
}
else {
$field = field_info_field($info['field_name']);
$format = date_type_format($field['type']);
$timezone_db = date_get_timezone_db($field['settings']['tz_handling']);
$date = new BackdropDateTime($value, 'UTC');
if ($timezone_db != 'UTC') {
date_timezone_set($date, timezone_open($timezone_db));
}
$item[$name] = $date->format($format);
}
}
/**
* Returns a metadata wrapper for accessing site-wide properties.
*
* Although there is no 'site' entity or such, modules may provide info about
* site-wide properties using hook_entity_property_info(). This function returns
* a wrapper for making use of this properties.
*
* @return EntityMetadataWrapper
* A wrapper for accessing site-wide properties.
*
* @see entity_metadata_system_entity_property_info()
*/
function entity_token_metadata_site_wrapper() {
$site_info = entity_plus_get_property_info('site');
$info['property info'] = $site_info['properties'];
return entity_metadata_wrapper('site', FALSE, $info);
}