Prefixed entity_type to avoid conflicts with other modules
[project/fieldx.git] / fieldx.module
1 <?php
2
3 /**
4 * Implements hook_entity_load().
5 */
6 function fieldx_entity_load($entities, $type) {
7 foreach ($entities as $entity) {
8 $entity->fieldx_entity_type = $type;
9 }
10 }
11
12 /**
13 * Implements hook_entity_presave().
14 */
15 function fieldx_entity_presave($entity, $type) {
16 $entity->fieldx_entity_type = $type;
17 }
18
19 /**
20 * Implements hook_form_alter().
21 */
22 function fieldx_form_alter(&$form, &$form_state) {
23 if (isset($form['#entity_type'])) {
24 $form['fieldx_entity_type'] = array(
25 '#type' => 'value',
26 '#value' => $form['#entity_type'],
27 );
28 }
29 }
30
31
32 /**
33 * Public Field Extended API
34 *
35 * These are the functions that should be used until they are deprecated in
36 * favor of official or better ones. See the wrapped functions for the full
37 * documentation.
38 */
39
40 /**
41 * Returns a single field item value in the current display language.
42 */
43 function fieldx_get_display_item($entity, $field_name, $column = 'value', $delta = 0) {
44 $entity = (object) $entity;
45 $items = field_get_display_items($entity->fieldx_entity_type, $entity, $field_name);
46 return isset($items[$delta][$column]) ? $items[$delta][$column] : FALSE;
47 }
48
49 /**
50 * Returns a single field item value in the current working language.
51 */
52 function fieldx_get_raw_item($entity, $field_name, $column = 'value', $delta = 0) {
53 $entity = (object) $entity;
54 $items = field_get_raw_items($entity->fieldx_entity_type, $entity, $field_name);
55 return isset($items[$delta][$column]) ? $items[$delta][$column] : FALSE;
56 }
57
58 /**
59 * Returns the given field items to be manipulated in the specified language.
60 */
61 function fieldx_get_raw_items($entity, $field_name, $langcode = NULL) {
62 return field_get_raw_items($entity->fieldx_entity_type, $entity, $field_name, $langcode);
63 }
64
65 /**
66 * Sets a single field item value in the current working language.
67 */
68 function fieldx_set_raw_item($entity, $field_name, $value, $column = 'value', $delta = 0) {
69 $entity = (object) $entity;
70 $items = field_get_raw_items($entity->fieldx_entity_type, $entity, $field_name);
71 $items[$delta][$column] = $value;
72 field_set_raw_items($entity->fieldx_entity_type, $entity, $field_name, $items);
73 }
74
75 /**
76 * Sets the given field items manipulated in the specified language.
77 */
78 function fieldx_set_raw_items($entity, $field_name, $items, $langcode = NULL) {
79 field_set_raw_items($entity->fieldx_entity_type, $entity, $field_name, $items);
80 }
81
82 /**
83 * Returns the submitted field item value.
84 */
85 function fieldx_get_form_item($field_name, $form, $form_state, $column = 'value', $delta = 0) {
86 $items = field_get_form_items($field_name, $form, $form_state);
87 return isset($items[$delta][$column]) ? $items[$delta][$column] : FALSE;
88 }
89
90 /**
91 * Returns the submitted field items for the given form data.
92 */
93 function fieldx_get_form_items($field_name, $form, $form_state) {
94 return field_get_form_items($field_name, $form, $form_state);
95 }
96
97 /**
98 * Returns the language code to be used to manipulate a field items array.
99 */
100 function fieldx_working_language($entity, $field_name, $langcode = NULL) {
101 return field_working_language($entity->fieldx_entity_type, $entity, $field_name, $langcode = NULL);
102 }
103
104
105 /**
106 * Future Field API
107 *
108 * These functions should be included into the core Field API. You may want to
109 * use the FieldX wrappers until they do.
110 */
111
112 if (!function_exists('field_get_raw_items')) {
113
114 /**
115 * Returns the given field items to be manipulated in the specified language.
116 *
117 * This function should be used to retrieve the items for the specified field in
118 * the current working language. Specifying a language is useful only when there
119 * is a known language to work with. In most scenarios this is not required as
120 * using the entity language is generally the intended behavior.
121 *
122 * @param $entity_type
123 * The type of $entity; e.g., 'node' or 'user'.
124 * @param $entity
125 * The entity containing the data to be returned.
126 * @param $field_name
127 * The name of the field whose items are to be retrieved.
128 * @param $langcode
129 * (optional) The language code to be used if the field is translatable.
130 * Defaults to the entity language.
131 *
132 * @return
133 * An array of field items keyed by delta if available, FALSE otherwise.
134 */
135 function field_get_raw_items($entity_type, $entity, $field_name, $langcode = NULL) {
136 $langcode = field_working_language($entity_type, $entity, $field_name, $langcode);
137 return isset($entity->{$field_name}[$langcode]) ? $entity->{$field_name}[$langcode] : FALSE;
138 }
139
140
141 } if (!function_exists('field_set_raw_items')) {
142
143 /**
144 * Sets the given field items manipulated in the specified language.
145 *
146 * This function should be used to set the items for the specified field in the
147 * current working language. Specifying a language is useful only when there is
148 * a known language to work with. In most scenarios this is not required as
149 * using the entity language is generally the intended behavior.
150 *
151 * @param $entity_type
152 * The type of $entity; e.g., 'node' or 'user'.
153 * @param $entity
154 * The entity containing the data to be returned.
155 * @param $field_name
156 * The name of the field whose items are to be set.
157 * @param $items
158 * The items to be set for the given field.
159 * @param $langcode
160 * (optional) The language code to be used if the field is translatable.
161 * Defaults to the entity language.
162 */
163 function field_set_raw_items($entity_type, $entity, $field_name, $items, $langcode = NULL) {
164 $langcode = field_working_language($entity_type, $entity, $field_name, $langcode);
165 $entity->{$field_name}[$langcode] = $items;
166 }
167
168
169 } if (!function_exists('field_get_form_items')) {
170
171 /**
172 * Returns the submitted field items for the given form data.
173 *
174 * @param $field_name
175 * The name of the field whose items have to be retrieved.
176 * @param $form
177 * The complete form array structure.
178 * @param $form_state
179 * An associative array containing the current state of the form.
180 */
181 function field_get_form_items($field_name, $form, $form_state) {
182 $entity = (object) $form_state['values'];
183 $path = array_merge($form['#parents'], array($field_name));
184 $element = $form;
185 $element = drupal_array_get_nested_value($element, $path);
186 $langcode = $element['#language'];
187 return isset($entity->{$field_name}[$langcode]) ? $entity->{$field_name}[$langcode] : FALSE;
188 }
189
190
191 } if (!function_exists('field_working_language')) {
192
193 /**
194 * Returns the language code to be used to manipulate a field items array.
195 *
196 * This function should be used to retrieve the current working language for the
197 * given field. Specifying a language is useful only when there is a known
198 * language to work with. In most scenarios this is not required as using the
199 * entity language is generally the intended behavior.
200 *
201 * @param $entity_type
202 * The type of $entity; e.g., 'node' or 'user'.
203 * @param $entity
204 * The entity containing the data to be manipulated.
205 * @param $field_name
206 * The name of the field whose items are to be manipulated.
207 * @param $langcode
208 * (optional) The language code to be used if the field is translatable.
209 * Defaults to the entity language.
210 */
211 function field_working_language($entity_type, $entity, $field_name, $langcode = NULL) {
212 $working_language = NULL;
213 $field = field_info_field($field_name);
214 if (field_is_translatable($entity_type, $field)) {
215 $working_language = !empty($langcode) ? $langcode : entity_language($entity_type, $entity);
216 }
217 // If entity_language() returns an empty value, we fall back to no language.
218 return !empty($working_language) ? $working_language : LANGUAGE_NONE;
219 }
220
221
222 } if (!function_exists('field_get_display_items')) {
223
224 /**
225 * Returns the field items in the language they currently would be displayed.
226 *
227 * This function should be used only in the rendering phase. The returned values
228 * should only be displayed or used to determine a displayed value. Setting back
229 * the items onto the entity is highly discouraged as it may cause unexpected
230 * results. As a matter of fact fallback rules might have been applied and the
231 * returned items might not exist in the current language.
232 *
233 * @param $entity_type
234 * The type of $entity; e.g., 'node' or 'user'.
235 * @param $entity
236 * The entity containing the data to be displayed.
237 * @param $field_name
238 * The field to be displayed.
239 * @param $langcode
240 * (optional) The language code $entity->{$field_name} has to be displayed in.
241 * Defaults to the current language.
242 *
243 * @return
244 * An array of field items keyed by delta if available, FALSE otherwise.
245 */
246 function field_get_display_items($entity_type, $entity, $field_name, $langcode = NULL) {
247 $langcode = field_language($entity_type, $entity, $field_name, $langcode);
248 return isset($entity->{$field_name}[$langcode]) ? $entity->{$field_name}[$langcode] : FALSE;
249 }
250
251
252 }
253
254 /**
255 * Deprecated API
256 *
257 * These functions have been deprecated in favor of the core API or other Field
258 * Extended API functions.
259 */
260
261 /**
262 * @deprecated
263 * @see fieldx_working_language()
264 */
265 function fieldx_entity_language($entity, $field_name) {
266 return fieldx_working_language($entity, $field_name);
267 }
268
269 /**
270 * @deprecated
271 * @see fieldx_get_display_item()
272 */
273 function fieldx_get_item($entity, $field_name, $column = 'value', $delta = 0) {
274 return fieldx_get_display_item($entity, $field_name, $column, $delta);
275 }