summaryrefslogtreecommitdiffstats
path: root/fieldx.module
blob: 20a01d9a305bde162cd83549399a830b1332110a (plain)
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
<?php

/**
 * Implements hook_entity_load().
 */
function fieldx_entity_load($entities, $type) {
  foreach ($entities as $entity) {
    $entity->fieldx_entity_type = $type;
  }
}

/**
 * Implements hook_entity_presave().
 */
function fieldx_entity_presave($entity, $type) {
  $entity->fieldx_entity_type = $type;
}

/**
 * Implements hook_form_alter().
 */
function fieldx_form_alter(&$form, &$form_state) {
  if (isset($form['#entity_type'])) {
    $form['fieldx_entity_type'] = array(
      '#type' => 'value',
      '#value' => $form['#entity_type'],
    );
  }
}


/**
 * Public Field Extended API
 *
 * These are the functions that should be used until they are deprecated in
 * favor of official or better ones. See the wrapped functions for the full
 * documentation.
 */

/**
 * Returns a single field item value in the current display language.
 */
function fieldx_get_display_item($entity, $field_name, $column = 'value', $delta = 0) {
  $entity = (object) $entity;
  $items = field_get_display_items($entity->fieldx_entity_type, $entity, $field_name);
  return isset($items[$delta][$column]) ? $items[$delta][$column] : FALSE;
}

/**
 * Returns a single field item value in the current working language.
 */
function fieldx_get_raw_item($entity, $field_name, $column = 'value', $delta = 0) {
  $entity = (object) $entity;
  $items = field_get_raw_items($entity->fieldx_entity_type, $entity, $field_name);
  return isset($items[$delta][$column]) ? $items[$delta][$column] : FALSE;
}

/**
 * Returns the given field items to be manipulated in the specified language.
 */
function fieldx_get_raw_items($entity, $field_name, $langcode = NULL) {
  return field_get_raw_items($entity->fieldx_entity_type, $entity, $field_name, $langcode);
}

/**
 * Sets a single field item value in the current working language.
 */
function fieldx_set_raw_item($entity, $field_name, $value, $column = 'value', $delta = 0) {
  $entity = (object) $entity;
  $items = field_get_raw_items($entity->fieldx_entity_type, $entity, $field_name);
  $items[$delta][$column] = $value;
  field_set_raw_items($entity->fieldx_entity_type, $entity, $field_name, $items);
}

/**
 * Sets the given field items manipulated in the specified language.
 */
function fieldx_set_raw_items($entity, $field_name, $items, $langcode = NULL) {
  field_set_raw_items($entity->fieldx_entity_type, $entity, $field_name, $items);
}

/**
 * Returns the submitted field item value.
 */
function fieldx_get_form_item($field_name, $form, $form_state, $column = 'value', $delta = 0) {
  $items = field_get_form_items($field_name, $form, $form_state);
  return isset($items[$delta][$column]) ? $items[$delta][$column] : FALSE;
}

/**
 * Returns the submitted field items for the given form data.
 */
function fieldx_get_form_items($field_name, $form, $form_state) {
  return field_get_form_items($field_name, $form, $form_state);
}

/**
 * Returns the language code to be used to manipulate a field items array.
 */
function fieldx_working_language($entity, $field_name, $langcode = NULL) {
  return field_working_language($entity->fieldx_entity_type, $entity, $field_name, $langcode = NULL);
}


/**
 * Future Field API
 *
 * These functions should be included into the core Field API. You may want to
 * use the FieldX wrappers until they do.
 */

if (!function_exists('field_get_raw_items')) {

/**
 * Returns the given field items to be manipulated in the specified language.
 *
 * This function should be used to retrieve the items for the specified field in
 * the current working language. Specifying a language is useful only when there
 * is a known language to work with. In most scenarios this is not required as
 * using the entity language is generally the intended behavior.
 *
 * @param $entity_type
 *   The type of $entity; e.g., 'node' or 'user'.
 * @param $entity
 *   The entity containing the data to be returned.
 * @param $field_name
 *   The name of the field whose items are to be retrieved.
 * @param $langcode
 *   (optional) The language code to be used if the field is translatable.
 *   Defaults to the entity language.
 *
 * @return
 *   An array of field items keyed by delta if available, FALSE otherwise.
 */
function field_get_raw_items($entity_type, $entity, $field_name, $langcode = NULL) {
  $langcode = field_working_language($entity_type, $entity, $field_name, $langcode);
  return isset($entity->{$field_name}[$langcode]) ? $entity->{$field_name}[$langcode] : FALSE;
}


} if (!function_exists('field_set_raw_items')) {

/**
 * Sets the given field items manipulated in the specified language.
 *
 * This function should be used to set the items for the specified field in the
 * current working language. Specifying a language is useful only when there is
 * a known language to work with. In most scenarios this is not required as
 * using the entity language is generally the intended behavior.
 *
 * @param $entity_type
 *   The type of $entity; e.g., 'node' or 'user'.
 * @param $entity
 *   The entity containing the data to be returned.
 * @param $field_name
 *   The name of the field whose items are to be set.
 * @param $items
 *   The items to be set for the given field.
 * @param $langcode
 *   (optional) The language code to be used if the field is translatable.
 *   Defaults to the entity language.
 */
function field_set_raw_items($entity_type, $entity, $field_name, $items, $langcode = NULL) {
  $langcode = field_working_language($entity_type, $entity, $field_name, $langcode);
  $entity->{$field_name}[$langcode] = $items;
}


} if (!function_exists('field_get_form_items')) {

/**
 * Returns the submitted field items for the given form data.
 *
 * @param $field_name
 *   The name of the field whose items have to be retrieved.
 * @param $form
 *   The complete form array structure.
 * @param $form_state
 *   An associative array containing the current state of the form.
 */
function field_get_form_items($field_name, $form, $form_state) {
  $entity = (object) $form_state['values'];
  $path = array_merge($form['#parents'], array($field_name));
  $element = $form;
  $element = drupal_array_get_nested_value($element, $path);
  $langcode = $element['#language'];
  return isset($entity->{$field_name}[$langcode]) ? $entity->{$field_name}[$langcode] : FALSE;
}


} if (!function_exists('field_working_language')) {

/**
 * Returns the language code to be used to manipulate a field items array.
 *
 * This function should be used to retrieve the current working language for the
 * given field. Specifying a language is useful only when there is a known
 * language to work with. In most scenarios this is not required as using the
 * entity language is generally the intended behavior.
 *
 * @param $entity_type
 *   The type of $entity; e.g., 'node' or 'user'.
 * @param $entity
 *   The entity containing the data to be manipulated.
 * @param $field_name
 *   The name of the field whose items are to be manipulated.
 * @param $langcode
 *   (optional) The language code to be used if the field is translatable.
 *   Defaults to the entity language.
 */
function field_working_language($entity_type, $entity, $field_name, $langcode = NULL) {
  $working_language = NULL;
  $field = field_info_field($field_name);
  if (field_is_translatable($entity_type, $field)) {
    $working_language = !empty($langcode) ? $langcode : entity_language($entity_type, $entity);
  }
  // If entity_language() returns an empty value, we fall back to no language.
  return !empty($working_language) ? $working_language : LANGUAGE_NONE;
}


} if (!function_exists('field_get_display_items')) {

/**
 * Returns the field items in the language they currently would be displayed.
 *
 * This function should be used only in the rendering phase. The returned values
 * should only be displayed or used to determine a displayed value. Setting back
 * the items onto the entity is highly discouraged as it may cause unexpected
 * results. As a matter of fact fallback rules might have been applied and the
 * returned items might not exist in the current language.
 *
 * @param $entity_type
 *   The type of $entity; e.g., 'node' or 'user'.
 * @param $entity
 *   The entity containing the data to be displayed.
 * @param $field_name
 *   The field to be displayed.
 * @param $langcode
 *   (optional) The language code $entity->{$field_name} has to be displayed in.
 *   Defaults to the current language.
 *
 * @return
 *   An array of field items keyed by delta if available, FALSE otherwise.
 */
function field_get_display_items($entity_type, $entity, $field_name, $langcode = NULL) {
  $langcode = field_language($entity_type, $entity, $field_name, $langcode);
  return isset($entity->{$field_name}[$langcode]) ? $entity->{$field_name}[$langcode] : FALSE;
}


}

/**
 * Deprecated API
 *
 * These functions have been deprecated in favor of the core API or other Field
 * Extended API functions.
 */

/**
 * @deprecated
 * @see fieldx_working_language()
 */
function fieldx_entity_language($entity, $field_name) {
  return fieldx_working_language($entity, $field_name);
}

/**
 * @deprecated
 * @see fieldx_get_display_item()
 */
function fieldx_get_item($entity, $field_name, $column = 'value', $delta = 0) {
  return fieldx_get_display_item($entity, $field_name, $column, $delta);
}