Mercurial > hg > rr-repo
comparison modules/field/field.api.php @ 0:ff03f76ab3fe
initial version
author | danieleb <danielebarchiesi@me.com> |
---|---|
date | Wed, 21 Aug 2013 18:51:11 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:ff03f76ab3fe |
---|---|
1 <?php | |
2 | |
3 /** | |
4 * @addtogroup hooks | |
5 * @{ | |
6 */ | |
7 | |
8 /** | |
9 * Exposes "pseudo-field" components on fieldable entities. | |
10 * | |
11 * Field UI's "Manage fields" and "Manage display" pages let users re-order | |
12 * fields, but also non-field components. For nodes, these include the title, | |
13 * poll choices, and other elements exposed by modules through hook_form() or | |
14 * hook_form_alter(). | |
15 * | |
16 * Fieldable entities or modules that want to have their components supported | |
17 * should expose them using this hook. The user-defined settings (weight, | |
18 * visible) are automatically applied on rendered forms and displayed | |
19 * entities in a #pre_render callback added by field_attach_form() and | |
20 * field_attach_view(). | |
21 * | |
22 * @see _field_extra_fields_pre_render() | |
23 * @see hook_field_extra_fields_alter() | |
24 * | |
25 * @return | |
26 * A nested array of 'pseudo-field' elements. Each list is nested within the | |
27 * following keys: entity type, bundle name, context (either 'form' or | |
28 * 'display'). The keys are the name of the elements as appearing in the | |
29 * renderable array (either the entity form or the displayed entity). The | |
30 * value is an associative array: | |
31 * - label: The human readable name of the element. | |
32 * - description: A short description of the element contents. | |
33 * - weight: The default weight of the element. | |
34 * - edit: (optional) String containing markup (normally a link) used as the | |
35 * element's 'edit' operation in the administration interface. Only for | |
36 * 'form' context. | |
37 * - delete: (optional) String containing markup (normally a link) used as the | |
38 * element's 'delete' operation in the administration interface. Only for | |
39 * 'form' context. | |
40 */ | |
41 function hook_field_extra_fields() { | |
42 $extra['node']['poll'] = array( | |
43 'form' => array( | |
44 'choice_wrapper' => array( | |
45 'label' => t('Poll choices'), | |
46 'description' => t('Poll choices'), | |
47 'weight' => -4, | |
48 ), | |
49 'settings' => array( | |
50 'label' => t('Poll settings'), | |
51 'description' => t('Poll module settings'), | |
52 'weight' => -3, | |
53 ), | |
54 ), | |
55 'display' => array( | |
56 'poll_view_voting' => array( | |
57 'label' => t('Poll vote'), | |
58 'description' => t('Poll vote'), | |
59 'weight' => 0, | |
60 ), | |
61 'poll_view_results' => array( | |
62 'label' => t('Poll results'), | |
63 'description' => t('Poll results'), | |
64 'weight' => 0, | |
65 ), | |
66 ) | |
67 ); | |
68 | |
69 return $extra; | |
70 } | |
71 | |
72 /** | |
73 * Alter "pseudo-field" components on fieldable entities. | |
74 * | |
75 * @param $info | |
76 * The associative array of 'pseudo-field' components. | |
77 * | |
78 * @see hook_field_extra_fields() | |
79 */ | |
80 function hook_field_extra_fields_alter(&$info) { | |
81 // Force node title to always be at the top of the list by default. | |
82 foreach (node_type_get_types() as $bundle) { | |
83 if (isset($info['node'][$bundle->type]['form']['title'])) { | |
84 $info['node'][$bundle->type]['form']['title']['weight'] = -20; | |
85 } | |
86 } | |
87 } | |
88 | |
89 /** | |
90 * @defgroup field_types Field Types API | |
91 * @{ | |
92 * Define field types. | |
93 * | |
94 * In the Field API, each field has a type, which determines what kind of data | |
95 * (integer, string, date, etc.) the field can hold, which settings it provides, | |
96 * and so on. The data type(s) accepted by a field are defined in | |
97 * hook_field_schema(); other basic properties of a field are defined in | |
98 * hook_field_info(). The other hooks below are called by the Field Attach API | |
99 * to perform field-type-specific actions. | |
100 * | |
101 * The Field Types API also defines two kinds of pluggable handlers: widgets | |
102 * and formatters. @link field_widget Widgets @endlink specify how the field | |
103 * appears in edit forms, while @link field_formatter formatters @endlink | |
104 * specify how the field appears in displayed entities. | |
105 * | |
106 * A third kind of pluggable handlers, storage backends, is defined by the | |
107 * @link field_storage Field Storage API @endlink. | |
108 * | |
109 * See @link field Field API @endlink for information about the other parts of | |
110 * the Field API. | |
111 */ | |
112 | |
113 /** | |
114 * Define Field API field types. | |
115 * | |
116 * @return | |
117 * An array whose keys are field type names and whose values are arrays | |
118 * describing the field type, with the following key/value pairs: | |
119 * - label: The human-readable name of the field type. | |
120 * - description: A short description for the field type. | |
121 * - settings: An array whose keys are the names of the settings available | |
122 * for the field type, and whose values are the default values for those | |
123 * settings. | |
124 * - instance_settings: An array whose keys are the names of the settings | |
125 * available for instances of the field type, and whose values are the | |
126 * default values for those settings. Instance-level settings can have | |
127 * different values on each field instance, and thus allow greater | |
128 * flexibility than field-level settings. It is recommended to put settings | |
129 * at the instance level whenever possible. Notable exceptions: settings | |
130 * acting on the schema definition, or settings that Views needs to use | |
131 * across field instances (for example, the list of allowed values). | |
132 * - default_widget: The machine name of the default widget to be used by | |
133 * instances of this field type, when no widget is specified in the | |
134 * instance definition. This widget must be available whenever the field | |
135 * type is available (i.e. provided by the field type module, or by a module | |
136 * the field type module depends on). | |
137 * - default_formatter: The machine name of the default formatter to be used | |
138 * by instances of this field type, when no formatter is specified in the | |
139 * instance definition. This formatter must be available whenever the field | |
140 * type is available (i.e. provided by the field type module, or by a module | |
141 * the field type module depends on). | |
142 * - no_ui: (optional) A boolean specifying that users should not be allowed | |
143 * to create fields and instances of this field type through the UI. Such | |
144 * fields can only be created programmatically with field_create_field() | |
145 * and field_create_instance(). Defaults to FALSE. | |
146 * | |
147 * @see hook_field_info_alter() | |
148 */ | |
149 function hook_field_info() { | |
150 return array( | |
151 'text' => array( | |
152 'label' => t('Text'), | |
153 'description' => t('This field stores varchar text in the database.'), | |
154 'settings' => array('max_length' => 255), | |
155 'instance_settings' => array('text_processing' => 0), | |
156 'default_widget' => 'text_textfield', | |
157 'default_formatter' => 'text_default', | |
158 ), | |
159 'text_long' => array( | |
160 'label' => t('Long text'), | |
161 'description' => t('This field stores long text in the database.'), | |
162 'settings' => array('max_length' => ''), | |
163 'instance_settings' => array('text_processing' => 0), | |
164 'default_widget' => 'text_textarea', | |
165 'default_formatter' => 'text_default', | |
166 ), | |
167 'text_with_summary' => array( | |
168 'label' => t('Long text and summary'), | |
169 'description' => t('This field stores long text in the database along with optional summary text.'), | |
170 'settings' => array('max_length' => ''), | |
171 'instance_settings' => array('text_processing' => 1, 'display_summary' => 0), | |
172 'default_widget' => 'text_textarea_with_summary', | |
173 'default_formatter' => 'text_summary_or_trimmed', | |
174 ), | |
175 ); | |
176 } | |
177 | |
178 /** | |
179 * Perform alterations on Field API field types. | |
180 * | |
181 * @param $info | |
182 * Array of information on field types exposed by hook_field_info() | |
183 * implementations. | |
184 */ | |
185 function hook_field_info_alter(&$info) { | |
186 // Add a setting to all field types. | |
187 foreach ($info as $field_type => $field_type_info) { | |
188 $info[$field_type]['settings'] += array( | |
189 'mymodule_additional_setting' => 'default value', | |
190 ); | |
191 } | |
192 | |
193 // Change the default widget for fields of type 'foo'. | |
194 if (isset($info['foo'])) { | |
195 $info['foo']['default widget'] = 'mymodule_widget'; | |
196 } | |
197 } | |
198 | |
199 /** | |
200 * Define the Field API schema for a field structure. | |
201 * | |
202 * This hook MUST be defined in .install for it to be detected during | |
203 * installation and upgrade. | |
204 * | |
205 * @param $field | |
206 * A field structure. | |
207 * | |
208 * @return | |
209 * An associative array with the following keys: | |
210 * - columns: An array of Schema API column specifications, keyed by column | |
211 * name. This specifies what comprises a value for a given field. For | |
212 * example, a value for a number field is simply 'value', while a value for | |
213 * a formatted text field is the combination of 'value' and 'format'. It is | |
214 * recommended to avoid having the column definitions depend on field | |
215 * settings when possible. No assumptions should be made on how storage | |
216 * engines internally use the original column name to structure their | |
217 * storage. | |
218 * - indexes: (optional) An array of Schema API indexes definitions. Only | |
219 * columns that appear in the 'columns' array are allowed. Those indexes | |
220 * will be used as default indexes. Callers of field_create_field() can | |
221 * specify additional indexes, or, at their own risk, modify the default | |
222 * indexes specified by the field-type module. Some storage engines might | |
223 * not support indexes. | |
224 * - foreign keys: (optional) An array of Schema API foreign keys | |
225 * definitions. | |
226 */ | |
227 function hook_field_schema($field) { | |
228 if ($field['type'] == 'text_long') { | |
229 $columns = array( | |
230 'value' => array( | |
231 'type' => 'text', | |
232 'size' => 'big', | |
233 'not null' => FALSE, | |
234 ), | |
235 ); | |
236 } | |
237 else { | |
238 $columns = array( | |
239 'value' => array( | |
240 'type' => 'varchar', | |
241 'length' => $field['settings']['max_length'], | |
242 'not null' => FALSE, | |
243 ), | |
244 ); | |
245 } | |
246 $columns += array( | |
247 'format' => array( | |
248 'type' => 'varchar', | |
249 'length' => 255, | |
250 'not null' => FALSE, | |
251 ), | |
252 ); | |
253 return array( | |
254 'columns' => $columns, | |
255 'indexes' => array( | |
256 'format' => array('format'), | |
257 ), | |
258 'foreign keys' => array( | |
259 'format' => array( | |
260 'table' => 'filter_format', | |
261 'columns' => array('format' => 'format'), | |
262 ), | |
263 ), | |
264 ); | |
265 } | |
266 | |
267 /** | |
268 * Define custom load behavior for this module's field types. | |
269 * | |
270 * Unlike most other field hooks, this hook operates on multiple entities. The | |
271 * $entities, $instances and $items parameters are arrays keyed by entity ID. | |
272 * For performance reasons, information for all available entity should be | |
273 * loaded in a single query where possible. | |
274 * | |
275 * Note that the changes made to the field values get cached by the field cache | |
276 * for subsequent loads. You should never use this hook to load fieldable | |
277 * entities, since this is likely to cause infinite recursions when | |
278 * hook_field_load() is run on those as well. Use | |
279 * hook_field_formatter_prepare_view() instead. | |
280 * | |
281 * Make changes or additions to field values by altering the $items parameter by | |
282 * reference. There is no return value. | |
283 * | |
284 * @param $entity_type | |
285 * The type of $entity. | |
286 * @param $entities | |
287 * Array of entities being loaded, keyed by entity ID. | |
288 * @param $field | |
289 * The field structure for the operation. | |
290 * @param $instances | |
291 * Array of instance structures for $field for each entity, keyed by entity | |
292 * ID. | |
293 * @param $langcode | |
294 * The language code associated with $items. | |
295 * @param $items | |
296 * Array of field values already loaded for the entities, keyed by entity ID. | |
297 * Store your changes in this parameter (passed by reference). | |
298 * @param $age | |
299 * FIELD_LOAD_CURRENT to load the most recent revision for all fields, or | |
300 * FIELD_LOAD_REVISION to load the version indicated by each entity. | |
301 */ | |
302 function hook_field_load($entity_type, $entities, $field, $instances, $langcode, &$items, $age) { | |
303 // Sample code from text.module: precompute sanitized strings so they are | |
304 // stored in the field cache. | |
305 foreach ($entities as $id => $entity) { | |
306 foreach ($items[$id] as $delta => $item) { | |
307 // Only process items with a cacheable format, the rest will be handled | |
308 // by formatters if needed. | |
309 if (empty($instances[$id]['settings']['text_processing']) || filter_format_allowcache($item['format'])) { | |
310 $items[$id][$delta]['safe_value'] = isset($item['value']) ? _text_sanitize($instances[$id], $langcode, $item, 'value') : ''; | |
311 if ($field['type'] == 'text_with_summary') { | |
312 $items[$id][$delta]['safe_summary'] = isset($item['summary']) ? _text_sanitize($instances[$id], $langcode, $item, 'summary') : ''; | |
313 } | |
314 } | |
315 } | |
316 } | |
317 } | |
318 | |
319 /** | |
320 * Prepare field values prior to display. | |
321 * | |
322 * This hook is invoked before the field values are handed to formatters | |
323 * for display, and runs before the formatters' own | |
324 * hook_field_formatter_prepare_view(). | |
325 * | |
326 * Unlike most other field hooks, this hook operates on multiple entities. The | |
327 * $entities, $instances and $items parameters are arrays keyed by entity ID. | |
328 * For performance reasons, information for all available entities should be | |
329 * loaded in a single query where possible. | |
330 * | |
331 * Make changes or additions to field values by altering the $items parameter by | |
332 * reference. There is no return value. | |
333 * | |
334 * @param $entity_type | |
335 * The type of $entity. | |
336 * @param $entities | |
337 * Array of entities being displayed, keyed by entity ID. | |
338 * @param $field | |
339 * The field structure for the operation. | |
340 * @param $instances | |
341 * Array of instance structures for $field for each entity, keyed by entity | |
342 * ID. | |
343 * @param $langcode | |
344 * The language associated to $items. | |
345 * @param $items | |
346 * $entity->{$field['field_name']}, or an empty array if unset. | |
347 */ | |
348 function hook_field_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items) { | |
349 // Sample code from image.module: if there are no images specified at all, | |
350 // use the default image. | |
351 foreach ($entities as $id => $entity) { | |
352 if (empty($items[$id]) && $field['settings']['default_image']) { | |
353 if ($file = file_load($field['settings']['default_image'])) { | |
354 $items[$id][0] = (array) $file + array( | |
355 'is_default' => TRUE, | |
356 'alt' => '', | |
357 'title' => '', | |
358 ); | |
359 } | |
360 } | |
361 } | |
362 } | |
363 | |
364 /** | |
365 * Validate this module's field data. | |
366 * | |
367 * If there are validation problems, add to the $errors array (passed by | |
368 * reference). There is no return value. | |
369 * | |
370 * @param $entity_type | |
371 * The type of $entity. | |
372 * @param $entity | |
373 * The entity for the operation. | |
374 * @param $field | |
375 * The field structure for the operation. | |
376 * @param $instance | |
377 * The instance structure for $field on $entity's bundle. | |
378 * @param $langcode | |
379 * The language associated with $items. | |
380 * @param $items | |
381 * $entity->{$field['field_name']}[$langcode], or an empty array if unset. | |
382 * @param $errors | |
383 * The array of errors (keyed by field name, language code, and delta) that | |
384 * have already been reported for the entity. The function should add its | |
385 * errors to this array. Each error is an associative array with the following | |
386 * keys and values: | |
387 * - error: An error code (should be a string prefixed with the module name). | |
388 * - message: The human readable message to be displayed. | |
389 */ | |
390 function hook_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) { | |
391 foreach ($items as $delta => $item) { | |
392 if (!empty($item['value'])) { | |
393 if (!empty($field['settings']['max_length']) && drupal_strlen($item['value']) > $field['settings']['max_length']) { | |
394 $errors[$field['field_name']][$langcode][$delta][] = array( | |
395 'error' => 'text_max_length', | |
396 'message' => t('%name: the value may not be longer than %max characters.', array('%name' => $instance['label'], '%max' => $field['settings']['max_length'])), | |
397 ); | |
398 } | |
399 } | |
400 } | |
401 } | |
402 | |
403 /** | |
404 * Define custom presave behavior for this module's field types. | |
405 * | |
406 * Make changes or additions to field values by altering the $items parameter by | |
407 * reference. There is no return value. | |
408 * | |
409 * @param $entity_type | |
410 * The type of $entity. | |
411 * @param $entity | |
412 * The entity for the operation. | |
413 * @param $field | |
414 * The field structure for the operation. | |
415 * @param $instance | |
416 * The instance structure for $field on $entity's bundle. | |
417 * @param $langcode | |
418 * The language associated with $items. | |
419 * @param $items | |
420 * $entity->{$field['field_name']}[$langcode], or an empty array if unset. | |
421 */ | |
422 function hook_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) { | |
423 if ($field['type'] == 'number_decimal') { | |
424 // Let PHP round the value to ensure consistent behavior across storage | |
425 // backends. | |
426 foreach ($items as $delta => $item) { | |
427 if (isset($item['value'])) { | |
428 $items[$delta]['value'] = round($item['value'], $field['settings']['scale']); | |
429 } | |
430 } | |
431 } | |
432 } | |
433 | |
434 /** | |
435 * Define custom insert behavior for this module's field data. | |
436 * | |
437 * This hook is invoked from field_attach_insert() on the module that defines a | |
438 * field, during the process of inserting an entity object (node, taxonomy term, | |
439 * etc.). It is invoked just before the data for this field on the particular | |
440 * entity object is inserted into field storage. Only field modules that are | |
441 * storing or tracking information outside the standard field storage mechanism | |
442 * need to implement this hook. | |
443 * | |
444 * @param $entity_type | |
445 * The type of $entity. | |
446 * @param $entity | |
447 * The entity for the operation. | |
448 * @param $field | |
449 * The field structure for the operation. | |
450 * @param $instance | |
451 * The instance structure for $field on $entity's bundle. | |
452 * @param $langcode | |
453 * The language associated with $items. | |
454 * @param $items | |
455 * $entity->{$field['field_name']}[$langcode], or an empty array if unset. | |
456 * | |
457 * @see hook_field_update() | |
458 * @see hook_field_delete() | |
459 */ | |
460 function hook_field_insert($entity_type, $entity, $field, $instance, $langcode, &$items) { | |
461 if (variable_get('taxonomy_maintain_index_table', TRUE) && $field['storage']['type'] == 'field_sql_storage' && $entity_type == 'node' && $entity->status) { | |
462 $query = db_insert('taxonomy_index')->fields(array('nid', 'tid', 'sticky', 'created', )); | |
463 foreach ($items as $item) { | |
464 $query->values(array( | |
465 'nid' => $entity->nid, | |
466 'tid' => $item['tid'], | |
467 'sticky' => $entity->sticky, | |
468 'created' => $entity->created, | |
469 )); | |
470 } | |
471 $query->execute(); | |
472 } | |
473 } | |
474 | |
475 /** | |
476 * Define custom update behavior for this module's field data. | |
477 * | |
478 * This hook is invoked from field_attach_update() on the module that defines a | |
479 * field, during the process of updating an entity object (node, taxonomy term, | |
480 * etc.). It is invoked just before the data for this field on the particular | |
481 * entity object is updated into field storage. Only field modules that are | |
482 * storing or tracking information outside the standard field storage mechanism | |
483 * need to implement this hook. | |
484 * | |
485 * @param $entity_type | |
486 * The type of $entity. | |
487 * @param $entity | |
488 * The entity for the operation. | |
489 * @param $field | |
490 * The field structure for the operation. | |
491 * @param $instance | |
492 * The instance structure for $field on $entity's bundle. | |
493 * @param $langcode | |
494 * The language associated with $items. | |
495 * @param $items | |
496 * $entity->{$field['field_name']}[$langcode], or an empty array if unset. | |
497 * | |
498 * @see hook_field_insert() | |
499 * @see hook_field_delete() | |
500 */ | |
501 function hook_field_update($entity_type, $entity, $field, $instance, $langcode, &$items) { | |
502 if (variable_get('taxonomy_maintain_index_table', TRUE) && $field['storage']['type'] == 'field_sql_storage' && $entity_type == 'node') { | |
503 $first_call = &drupal_static(__FUNCTION__, array()); | |
504 | |
505 // We don't maintain data for old revisions, so clear all previous values | |
506 // from the table. Since this hook runs once per field, per object, make | |
507 // sure we only wipe values once. | |
508 if (!isset($first_call[$entity->nid])) { | |
509 $first_call[$entity->nid] = FALSE; | |
510 db_delete('taxonomy_index')->condition('nid', $entity->nid)->execute(); | |
511 } | |
512 // Only save data to the table if the node is published. | |
513 if ($entity->status) { | |
514 $query = db_insert('taxonomy_index')->fields(array('nid', 'tid', 'sticky', 'created')); | |
515 foreach ($items as $item) { | |
516 $query->values(array( | |
517 'nid' => $entity->nid, | |
518 'tid' => $item['tid'], | |
519 'sticky' => $entity->sticky, | |
520 'created' => $entity->created, | |
521 )); | |
522 } | |
523 $query->execute(); | |
524 } | |
525 } | |
526 } | |
527 | |
528 /** | |
529 * Update the storage information for a field. | |
530 * | |
531 * This is invoked on the field's storage module from field_update_field(), | |
532 * before the new field information is saved to the database. The field storage | |
533 * module should update its storage tables to agree with the new field | |
534 * information. If there is a problem, the field storage module should throw an | |
535 * exception. | |
536 * | |
537 * @param $field | |
538 * The updated field structure to be saved. | |
539 * @param $prior_field | |
540 * The previously-saved field structure. | |
541 * @param $has_data | |
542 * TRUE if the field has data in storage currently. | |
543 */ | |
544 function hook_field_storage_update_field($field, $prior_field, $has_data) { | |
545 if (!$has_data) { | |
546 // There is no data. Re-create the tables completely. | |
547 $prior_schema = _field_sql_storage_schema($prior_field); | |
548 foreach ($prior_schema as $name => $table) { | |
549 db_drop_table($name, $table); | |
550 } | |
551 $schema = _field_sql_storage_schema($field); | |
552 foreach ($schema as $name => $table) { | |
553 db_create_table($name, $table); | |
554 } | |
555 } | |
556 else { | |
557 // There is data. See field_sql_storage_field_storage_update_field() for | |
558 // an example of what to do to modify the schema in place, preserving the | |
559 // old data as much as possible. | |
560 } | |
561 drupal_get_schema(NULL, TRUE); | |
562 } | |
563 | |
564 /** | |
565 * Define custom delete behavior for this module's field data. | |
566 * | |
567 * This hook is invoked from field_attach_delete() on the module that defines a | |
568 * field, during the process of deleting an entity object (node, taxonomy term, | |
569 * etc.). It is invoked just before the data for this field on the particular | |
570 * entity object is deleted from field storage. Only field modules that are | |
571 * storing or tracking information outside the standard field storage mechanism | |
572 * need to implement this hook. | |
573 * | |
574 * @param $entity_type | |
575 * The type of $entity. | |
576 * @param $entity | |
577 * The entity for the operation. | |
578 * @param $field | |
579 * The field structure for the operation. | |
580 * @param $instance | |
581 * The instance structure for $field on $entity's bundle. | |
582 * @param $langcode | |
583 * The language associated with $items. | |
584 * @param $items | |
585 * $entity->{$field['field_name']}[$langcode], or an empty array if unset. | |
586 * | |
587 * @see hook_field_insert() | |
588 * @see hook_field_update() | |
589 */ | |
590 function hook_field_delete($entity_type, $entity, $field, $instance, $langcode, &$items) { | |
591 list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity); | |
592 foreach ($items as $delta => $item) { | |
593 // For hook_file_references(), remember that this is being deleted. | |
594 $item['file_field_name'] = $field['field_name']; | |
595 // Pass in the ID of the object that is being removed so all references can | |
596 // be counted in hook_file_references(). | |
597 $item['file_field_type'] = $entity_type; | |
598 $item['file_field_id'] = $id; | |
599 file_field_delete_file($item, $field, $entity_type, $id); | |
600 } | |
601 } | |
602 | |
603 /** | |
604 * Define custom revision delete behavior for this module's field types. | |
605 * | |
606 * This hook is invoked just before the data is deleted from field storage | |
607 * in field_attach_delete_revision(), and will only be called for fieldable | |
608 * types that are versioned. | |
609 * | |
610 * @param $entity_type | |
611 * The type of $entity. | |
612 * @param $entity | |
613 * The entity for the operation. | |
614 * @param $field | |
615 * The field structure for the operation. | |
616 * @param $instance | |
617 * The instance structure for $field on $entity's bundle. | |
618 * @param $langcode | |
619 * The language associated with $items. | |
620 * @param $items | |
621 * $entity->{$field['field_name']}[$langcode], or an empty array if unset. | |
622 */ | |
623 function hook_field_delete_revision($entity_type, $entity, $field, $instance, $langcode, &$items) { | |
624 list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity); | |
625 foreach ($items as $delta => $item) { | |
626 // For hook_file_references, remember that this file is being deleted. | |
627 $item['file_field_name'] = $field['field_name']; | |
628 if (file_field_delete_file($item, $field, $entity_type, $id)) { | |
629 $items[$delta] = NULL; | |
630 } | |
631 } | |
632 } | |
633 | |
634 /** | |
635 * Define custom prepare_translation behavior for this module's field types. | |
636 * | |
637 * @param $entity_type | |
638 * The type of $entity. | |
639 * @param $entity | |
640 * The entity for the operation. | |
641 * @param $field | |
642 * The field structure for the operation. | |
643 * @param $instance | |
644 * The instance structure for $field on $entity's bundle. | |
645 * @param $langcode | |
646 * The language associated to $items. | |
647 * @param $items | |
648 * $entity->{$field['field_name']}[$langcode], or an empty array if unset. | |
649 * @param $source_entity | |
650 * The source entity from which field values are being copied. | |
651 * @param $source_langcode | |
652 * The source language from which field values are being copied. | |
653 */ | |
654 function hook_field_prepare_translation($entity_type, $entity, $field, $instance, $langcode, &$items, $source_entity, $source_langcode) { | |
655 // If the translating user is not permitted to use the assigned text format, | |
656 // we must not expose the source values. | |
657 $field_name = $field['field_name']; | |
658 $formats = filter_formats(); | |
659 $format_id = $source_entity->{$field_name}[$source_langcode][0]['format']; | |
660 if (!filter_access($formats[$format_id])) { | |
661 $items = array(); | |
662 } | |
663 } | |
664 | |
665 /** | |
666 * Define what constitutes an empty item for a field type. | |
667 * | |
668 * @param $item | |
669 * An item that may or may not be empty. | |
670 * @param $field | |
671 * The field to which $item belongs. | |
672 * | |
673 * @return | |
674 * TRUE if $field's type considers $item not to contain any data; | |
675 * FALSE otherwise. | |
676 */ | |
677 function hook_field_is_empty($item, $field) { | |
678 if (empty($item['value']) && (string) $item['value'] !== '0') { | |
679 return TRUE; | |
680 } | |
681 return FALSE; | |
682 } | |
683 | |
684 /** | |
685 * @} End of "defgroup field_types". | |
686 */ | |
687 | |
688 /** | |
689 * @defgroup field_widget Field Widget API | |
690 * @{ | |
691 * Define Field API widget types. | |
692 * | |
693 * Field API widgets specify how fields are displayed in edit forms. Fields of a | |
694 * given @link field_types field type @endlink may be edited using more than one | |
695 * widget. In this case, the Field UI module allows the site builder to choose | |
696 * which widget to use. Widget types are defined by implementing | |
697 * hook_field_widget_info(). | |
698 * | |
699 * Widgets are @link forms_api_reference.html Form API @endlink elements with | |
700 * additional processing capabilities. Widget hooks are typically called by the | |
701 * Field Attach API during the creation of the field form structure with | |
702 * field_attach_form(). | |
703 * | |
704 * @see field | |
705 * @see field_types | |
706 * @see field_formatter | |
707 */ | |
708 | |
709 /** | |
710 * Expose Field API widget types. | |
711 * | |
712 * @return | |
713 * An array describing the widget types implemented by the module. | |
714 * The keys are widget type names. To avoid name clashes, widget type | |
715 * names should be prefixed with the name of the module that exposes them. | |
716 * The values are arrays describing the widget type, with the following | |
717 * key/value pairs: | |
718 * - label: The human-readable name of the widget type. | |
719 * - description: A short description for the widget type. | |
720 * - field types: An array of field types the widget supports. | |
721 * - settings: An array whose keys are the names of the settings available | |
722 * for the widget type, and whose values are the default values for those | |
723 * settings. | |
724 * - behaviors: (optional) An array describing behaviors of the widget, with | |
725 * the following elements: | |
726 * - multiple values: One of the following constants: | |
727 * - FIELD_BEHAVIOR_DEFAULT: (default) If the widget allows the input of | |
728 * one single field value (most common case). The widget will be | |
729 * repeated for each value input. | |
730 * - FIELD_BEHAVIOR_CUSTOM: If one single copy of the widget can receive | |
731 * several field values. Examples: checkboxes, multiple select, | |
732 * comma-separated textfield. | |
733 * - default value: One of the following constants: | |
734 * - FIELD_BEHAVIOR_DEFAULT: (default) If the widget accepts default | |
735 * values. | |
736 * - FIELD_BEHAVIOR_NONE: if the widget does not support default values. | |
737 * - weight: (optional) An integer to determine the weight of this widget | |
738 * relative to other widgets in the Field UI when selecting a widget for a | |
739 * given field instance. | |
740 * | |
741 * @see hook_field_widget_info_alter() | |
742 * @see hook_field_widget_form() | |
743 * @see hook_field_widget_form_alter() | |
744 * @see hook_field_widget_WIDGET_TYPE_form_alter() | |
745 * @see hook_field_widget_error() | |
746 * @see hook_field_widget_settings_form() | |
747 */ | |
748 function hook_field_widget_info() { | |
749 return array( | |
750 'text_textfield' => array( | |
751 'label' => t('Text field'), | |
752 'field types' => array('text'), | |
753 'settings' => array('size' => 60), | |
754 'behaviors' => array( | |
755 'multiple values' => FIELD_BEHAVIOR_DEFAULT, | |
756 'default value' => FIELD_BEHAVIOR_DEFAULT, | |
757 ), | |
758 ), | |
759 'text_textarea' => array( | |
760 'label' => t('Text area (multiple rows)'), | |
761 'field types' => array('text_long'), | |
762 'settings' => array('rows' => 5), | |
763 'behaviors' => array( | |
764 'multiple values' => FIELD_BEHAVIOR_DEFAULT, | |
765 'default value' => FIELD_BEHAVIOR_DEFAULT, | |
766 ), | |
767 ), | |
768 'text_textarea_with_summary' => array( | |
769 'label' => t('Text area with a summary'), | |
770 'field types' => array('text_with_summary'), | |
771 'settings' => array('rows' => 20, 'summary_rows' => 5), | |
772 'behaviors' => array( | |
773 'multiple values' => FIELD_BEHAVIOR_DEFAULT, | |
774 'default value' => FIELD_BEHAVIOR_DEFAULT, | |
775 ), | |
776 // As an advanced widget, force it to sink to the bottom of the choices. | |
777 'weight' => 2, | |
778 ), | |
779 ); | |
780 } | |
781 | |
782 /** | |
783 * Perform alterations on Field API widget types. | |
784 * | |
785 * @param $info | |
786 * Array of informations on widget types exposed by hook_field_widget_info() | |
787 * implementations. | |
788 */ | |
789 function hook_field_widget_info_alter(&$info) { | |
790 // Add a setting to a widget type. | |
791 $info['text_textfield']['settings'] += array( | |
792 'mymodule_additional_setting' => 'default value', | |
793 ); | |
794 | |
795 // Let a new field type re-use an existing widget. | |
796 $info['options_select']['field types'][] = 'my_field_type'; | |
797 } | |
798 | |
799 /** | |
800 * Return the form for a single field widget. | |
801 * | |
802 * Field widget form elements should be based on the passed-in $element, which | |
803 * contains the base form element properties derived from the field | |
804 * configuration. | |
805 * | |
806 * Field API will set the weight, field name and delta values for each form | |
807 * element. If there are multiple values for this field, the Field API will | |
808 * invoke this hook as many times as needed. | |
809 * | |
810 * Note that, depending on the context in which the widget is being included | |
811 * (regular entity form, field configuration form, advanced search form...), | |
812 * the values for $field and $instance might be different from the "official" | |
813 * definitions returned by field_info_field() and field_info_instance(). | |
814 * Examples: mono-value widget even if the field is multi-valued, non-required | |
815 * widget even if the field is 'required'... | |
816 * | |
817 * Therefore, the FAPI element callbacks (such as #process, #element_validate, | |
818 * #value_callback...) used by the widget cannot use the field_info_field() | |
819 * or field_info_instance() functions to retrieve the $field or $instance | |
820 * definitions they should operate on. The field_widget_field() and | |
821 * field_widget_instance() functions should be used instead to fetch the | |
822 * current working definitions from $form_state, where Field API stores them. | |
823 * | |
824 * Alternatively, hook_field_widget_form() can extract the needed specific | |
825 * properties from $field and $instance and set them as ad-hoc | |
826 * $element['#custom'] properties, for later use by its element callbacks. | |
827 * | |
828 * Other modules may alter the form element provided by this function using | |
829 * hook_field_widget_form_alter(). | |
830 * | |
831 * @param $form | |
832 * The form structure where widgets are being attached to. This might be a | |
833 * full form structure, or a sub-element of a larger form. | |
834 * @param $form_state | |
835 * An associative array containing the current state of the form. | |
836 * @param $field | |
837 * The field structure. | |
838 * @param $instance | |
839 * The field instance. | |
840 * @param $langcode | |
841 * The language associated with $items. | |
842 * @param $items | |
843 * Array of default values for this field. | |
844 * @param $delta | |
845 * The order of this item in the array of subelements (0, 1, 2, etc). | |
846 * @param $element | |
847 * A form element array containing basic properties for the widget: | |
848 * - #entity_type: The name of the entity the field is attached to. | |
849 * - #bundle: The name of the field bundle the field is contained in. | |
850 * - #field_name: The name of the field. | |
851 * - #language: The language the field is being edited in. | |
852 * - #field_parents: The 'parents' space for the field in the form. Most | |
853 * widgets can simply overlook this property. This identifies the | |
854 * location where the field values are placed within | |
855 * $form_state['values'], and is used to access processing information | |
856 * for the field through the field_form_get_state() and | |
857 * field_form_set_state() functions. | |
858 * - #columns: A list of field storage columns of the field. | |
859 * - #title: The sanitized element label for the field instance, ready for | |
860 * output. | |
861 * - #description: The sanitized element description for the field instance, | |
862 * ready for output. | |
863 * - #required: A Boolean indicating whether the element value is required; | |
864 * for required multiple value fields, only the first widget's values are | |
865 * required. | |
866 * - #delta: The order of this item in the array of subelements; see $delta | |
867 * above. | |
868 * | |
869 * @return | |
870 * The form elements for a single widget for this field. | |
871 * | |
872 * @see field_widget_field() | |
873 * @see field_widget_instance() | |
874 * @see hook_field_widget_form_alter() | |
875 * @see hook_field_widget_WIDGET_TYPE_form_alter() | |
876 */ | |
877 function hook_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) { | |
878 $element += array( | |
879 '#type' => $instance['widget']['type'], | |
880 '#default_value' => isset($items[$delta]) ? $items[$delta] : '', | |
881 ); | |
882 return array('value' => $element); | |
883 } | |
884 | |
885 /** | |
886 * Alter forms for field widgets provided by other modules. | |
887 * | |
888 * @param $element | |
889 * The field widget form element as constructed by hook_field_widget_form(). | |
890 * @param $form_state | |
891 * An associative array containing the current state of the form. | |
892 * @param $context | |
893 * An associative array containing the following key-value pairs, matching the | |
894 * arguments received by hook_field_widget_form(): | |
895 * - form: The form structure to which widgets are being attached. This may be | |
896 * a full form structure, or a sub-element of a larger form. | |
897 * - field: The field structure. | |
898 * - instance: The field instance structure. | |
899 * - langcode: The language associated with $items. | |
900 * - items: Array of default values for this field. | |
901 * - delta: The order of this item in the array of subelements (0, 1, 2, etc). | |
902 * | |
903 * @see hook_field_widget_form() | |
904 * @see hook_field_widget_WIDGET_TYPE_form_alter() | |
905 */ | |
906 function hook_field_widget_form_alter(&$element, &$form_state, $context) { | |
907 // Add a css class to widget form elements for all fields of type mytype. | |
908 if ($context['field']['type'] == 'mytype') { | |
909 // Be sure not to overwrite existing attributes. | |
910 $element['#attributes']['class'][] = 'myclass'; | |
911 } | |
912 } | |
913 | |
914 /** | |
915 * Alter widget forms for a specific widget provided by another module. | |
916 * | |
917 * Modules can implement hook_field_widget_WIDGET_TYPE_form_alter() to modify a | |
918 * specific widget form, rather than using hook_field_widget_form_alter() and | |
919 * checking the widget type. | |
920 * | |
921 * @param $element | |
922 * The field widget form element as constructed by hook_field_widget_form(). | |
923 * @param $form_state | |
924 * An associative array containing the current state of the form. | |
925 * @param $context | |
926 * An associative array containing the following key-value pairs, matching the | |
927 * arguments received by hook_field_widget_form(): | |
928 * - "form": The form structure where widgets are being attached to. This | |
929 * might be a full form structure, or a sub-element of a larger form. | |
930 * - "field": The field structure. | |
931 * - "instance": The field instance structure. | |
932 * - "langcode": The language associated with $items. | |
933 * - "items": Array of default values for this field. | |
934 * - "delta": The order of this item in the array of subelements (0, 1, 2, | |
935 * etc). | |
936 * | |
937 * @see hook_field_widget_form() | |
938 * @see hook_field_widget_form_alter() | |
939 */ | |
940 function hook_field_widget_WIDGET_TYPE_form_alter(&$element, &$form_state, $context) { | |
941 // Code here will only act on widgets of type WIDGET_TYPE. For example, | |
942 // hook_field_widget_mymodule_autocomplete_form_alter() will only act on | |
943 // widgets of type 'mymodule_autocomplete'. | |
944 $element['#autocomplete_path'] = 'mymodule/autocomplete_path'; | |
945 } | |
946 | |
947 /** | |
948 * Alters the widget properties of a field instance before it gets displayed. | |
949 * | |
950 * Note that instead of hook_field_widget_properties_alter(), which is called | |
951 * for all fields on all entity types, | |
952 * hook_field_widget_properties_ENTITY_TYPE_alter() may be used to alter widget | |
953 * properties for fields on a specific entity type only. | |
954 * | |
955 * This hook is called once per field per added or edit entity. If the result | |
956 * of the hook involves reading from the database, it is highly recommended to | |
957 * statically cache the information. | |
958 * | |
959 * @param $widget | |
960 * The instance's widget properties. | |
961 * @param $context | |
962 * An associative array containing: | |
963 * - entity_type: The entity type; e.g., 'node' or 'user'. | |
964 * - entity: The entity object. | |
965 * - field: The field that the widget belongs to. | |
966 * - instance: The instance of the field. | |
967 * | |
968 * @see hook_field_widget_properties_ENTITY_TYPE_alter() | |
969 */ | |
970 function hook_field_widget_properties_alter(&$widget, $context) { | |
971 // Change a widget's type according to the time of day. | |
972 $field = $context['field']; | |
973 if ($context['entity_type'] == 'node' && $field['field_name'] == 'field_foo') { | |
974 $time = date('H'); | |
975 $widget['type'] = $time < 12 ? 'widget_am' : 'widget_pm'; | |
976 } | |
977 } | |
978 | |
979 /** | |
980 * Flag a field-level validation error. | |
981 * | |
982 * @param $element | |
983 * An array containing the form element for the widget. The error needs to be | |
984 * flagged on the right sub-element, according to the widget's internal | |
985 * structure. | |
986 * @param $error | |
987 * An associative array with the following key-value pairs, as returned by | |
988 * hook_field_validate(): | |
989 * - error: the error code. Complex widgets might need to report different | |
990 * errors to different form elements inside the widget. | |
991 * - message: the human readable message to be displayed. | |
992 * @param $form | |
993 * The form structure where field elements are attached to. This might be a | |
994 * full form structure, or a sub-element of a larger form. | |
995 * @param $form_state | |
996 * An associative array containing the current state of the form. | |
997 */ | |
998 function hook_field_widget_error($element, $error, $form, &$form_state) { | |
999 form_error($element, $error['message']); | |
1000 } | |
1001 | |
1002 | |
1003 /** | |
1004 * @} End of "defgroup field_widget". | |
1005 */ | |
1006 | |
1007 | |
1008 /** | |
1009 * @defgroup field_formatter Field Formatter API | |
1010 * @{ | |
1011 * Define Field API formatter types. | |
1012 * | |
1013 * Field API formatters specify how fields are displayed when the entity to | |
1014 * which the field is attached is displayed. Fields of a given | |
1015 * @link field_types field type @endlink may be displayed using more than one | |
1016 * formatter. In this case, the Field UI module allows the site builder to | |
1017 * choose which formatter to use. Field formatters are defined by implementing | |
1018 * hook_field_formatter_info(). | |
1019 * | |
1020 * @see field | |
1021 * @see field_types | |
1022 * @see field_widget | |
1023 */ | |
1024 | |
1025 /** | |
1026 * Expose Field API formatter types. | |
1027 * | |
1028 * Formatters handle the display of field values. Formatter hooks are typically | |
1029 * called by the Field Attach API field_attach_prepare_view() and | |
1030 * field_attach_view() functions. | |
1031 * | |
1032 * @return | |
1033 * An array describing the formatter types implemented by the module. | |
1034 * The keys are formatter type names. To avoid name clashes, formatter type | |
1035 * names should be prefixed with the name of the module that exposes them. | |
1036 * The values are arrays describing the formatter type, with the following | |
1037 * key/value pairs: | |
1038 * - label: The human-readable name of the formatter type. | |
1039 * - description: A short description for the formatter type. | |
1040 * - field types: An array of field types the formatter supports. | |
1041 * - settings: An array whose keys are the names of the settings available | |
1042 * for the formatter type, and whose values are the default values for | |
1043 * those settings. | |
1044 * | |
1045 * @see hook_field_formatter_info_alter() | |
1046 * @see hook_field_formatter_view() | |
1047 * @see hook_field_formatter_prepare_view() | |
1048 */ | |
1049 function hook_field_formatter_info() { | |
1050 return array( | |
1051 'text_default' => array( | |
1052 'label' => t('Default'), | |
1053 'field types' => array('text', 'text_long', 'text_with_summary'), | |
1054 ), | |
1055 'text_plain' => array( | |
1056 'label' => t('Plain text'), | |
1057 'field types' => array('text', 'text_long', 'text_with_summary'), | |
1058 ), | |
1059 | |
1060 // The text_trimmed formatter displays the trimmed version of the | |
1061 // full element of the field. It is intended to be used with text | |
1062 // and text_long fields. It also works with text_with_summary | |
1063 // fields though the text_summary_or_trimmed formatter makes more | |
1064 // sense for that field type. | |
1065 'text_trimmed' => array( | |
1066 'label' => t('Trimmed'), | |
1067 'field types' => array('text', 'text_long', 'text_with_summary'), | |
1068 ), | |
1069 | |
1070 // The 'summary or trimmed' field formatter for text_with_summary | |
1071 // fields displays returns the summary element of the field or, if | |
1072 // the summary is empty, the trimmed version of the full element | |
1073 // of the field. | |
1074 'text_summary_or_trimmed' => array( | |
1075 'label' => t('Summary or trimmed'), | |
1076 'field types' => array('text_with_summary'), | |
1077 ), | |
1078 ); | |
1079 } | |
1080 | |
1081 /** | |
1082 * Perform alterations on Field API formatter types. | |
1083 * | |
1084 * @param $info | |
1085 * An array of information on formatter types exposed by | |
1086 * hook_field_formatter_info() implementations. | |
1087 */ | |
1088 function hook_field_formatter_info_alter(&$info) { | |
1089 // Add a setting to a formatter type. | |
1090 $info['text_default']['settings'] += array( | |
1091 'mymodule_additional_setting' => 'default value', | |
1092 ); | |
1093 | |
1094 // Let a new field type re-use an existing formatter. | |
1095 $info['text_default']['field types'][] = 'my_field_type'; | |
1096 } | |
1097 | |
1098 /** | |
1099 * Allow formatters to load information for field values being displayed. | |
1100 * | |
1101 * This should be used when a formatter needs to load additional information | |
1102 * from the database in order to render a field, for example a reference field | |
1103 * which displays properties of the referenced entities such as name or type. | |
1104 * | |
1105 * This hook is called after the field type's own hook_field_prepare_view(). | |
1106 * | |
1107 * Unlike most other field hooks, this hook operates on multiple entities. The | |
1108 * $entities, $instances and $items parameters are arrays keyed by entity ID. | |
1109 * For performance reasons, information for all available entities should be | |
1110 * loaded in a single query where possible. | |
1111 * | |
1112 * @param $entity_type | |
1113 * The type of $entity. | |
1114 * @param $entities | |
1115 * Array of entities being displayed, keyed by entity ID. | |
1116 * @param $field | |
1117 * The field structure for the operation. | |
1118 * @param $instances | |
1119 * Array of instance structures for $field for each entity, keyed by entity | |
1120 * ID. | |
1121 * @param $langcode | |
1122 * The language the field values are to be shown in. If no language is | |
1123 * provided the current language is used. | |
1124 * @param $items | |
1125 * Array of field values for the entities, keyed by entity ID. | |
1126 * @param $displays | |
1127 * Array of display settings to use for each entity, keyed by entity ID. | |
1128 * | |
1129 * @return | |
1130 * Changes or additions to field values are done by altering the $items | |
1131 * parameter by reference. | |
1132 */ | |
1133 function hook_field_formatter_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items, $displays) { | |
1134 $tids = array(); | |
1135 | |
1136 // Collect every possible term attached to any of the fieldable entities. | |
1137 foreach ($entities as $id => $entity) { | |
1138 foreach ($items[$id] as $delta => $item) { | |
1139 // Force the array key to prevent duplicates. | |
1140 $tids[$item['tid']] = $item['tid']; | |
1141 } | |
1142 } | |
1143 | |
1144 if ($tids) { | |
1145 $terms = taxonomy_term_load_multiple($tids); | |
1146 | |
1147 // Iterate through the fieldable entities again to attach the loaded term | |
1148 // data. | |
1149 foreach ($entities as $id => $entity) { | |
1150 $rekey = FALSE; | |
1151 | |
1152 foreach ($items[$id] as $delta => $item) { | |
1153 // Check whether the taxonomy term field instance value could be loaded. | |
1154 if (isset($terms[$item['tid']])) { | |
1155 // Replace the instance value with the term data. | |
1156 $items[$id][$delta]['taxonomy_term'] = $terms[$item['tid']]; | |
1157 } | |
1158 // Otherwise, unset the instance value, since the term does not exist. | |
1159 else { | |
1160 unset($items[$id][$delta]); | |
1161 $rekey = TRUE; | |
1162 } | |
1163 } | |
1164 | |
1165 if ($rekey) { | |
1166 // Rekey the items array. | |
1167 $items[$id] = array_values($items[$id]); | |
1168 } | |
1169 } | |
1170 } | |
1171 } | |
1172 | |
1173 /** | |
1174 * Build a renderable array for a field value. | |
1175 * | |
1176 * @param $entity_type | |
1177 * The type of $entity. | |
1178 * @param $entity | |
1179 * The entity being displayed. | |
1180 * @param $field | |
1181 * The field structure. | |
1182 * @param $instance | |
1183 * The field instance. | |
1184 * @param $langcode | |
1185 * The language associated with $items. | |
1186 * @param $items | |
1187 * Array of values for this field. | |
1188 * @param $display | |
1189 * The display settings to use, as found in the 'display' entry of instance | |
1190 * definitions. The array notably contains the following keys and values; | |
1191 * - type: The name of the formatter to use. | |
1192 * - settings: The array of formatter settings. | |
1193 * | |
1194 * @return | |
1195 * A renderable array for the $items, as an array of child elements keyed | |
1196 * by numeric indexes starting from 0. | |
1197 */ | |
1198 function hook_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) { | |
1199 $element = array(); | |
1200 $settings = $display['settings']; | |
1201 | |
1202 switch ($display['type']) { | |
1203 case 'sample_field_formatter_simple': | |
1204 // Common case: each value is displayed individually in a sub-element | |
1205 // keyed by delta. The field.tpl.php template specifies the markup | |
1206 // wrapping each value. | |
1207 foreach ($items as $delta => $item) { | |
1208 $element[$delta] = array('#markup' => $settings['some_setting'] . $item['value']); | |
1209 } | |
1210 break; | |
1211 | |
1212 case 'sample_field_formatter_themeable': | |
1213 // More elaborate formatters can defer to a theme function for easier | |
1214 // customization. | |
1215 foreach ($items as $delta => $item) { | |
1216 $element[$delta] = array( | |
1217 '#theme' => 'mymodule_theme_sample_field_formatter_themeable', | |
1218 '#data' => $item['value'], | |
1219 '#some_setting' => $settings['some_setting'], | |
1220 ); | |
1221 } | |
1222 break; | |
1223 | |
1224 case 'sample_field_formatter_combined': | |
1225 // Some formatters might need to display all values within a single piece | |
1226 // of markup. | |
1227 $rows = array(); | |
1228 foreach ($items as $delta => $item) { | |
1229 $rows[] = array($delta, $item['value']); | |
1230 } | |
1231 $element[0] = array( | |
1232 '#theme' => 'table', | |
1233 '#header' => array(t('Delta'), t('Value')), | |
1234 '#rows' => $rows, | |
1235 ); | |
1236 break; | |
1237 } | |
1238 | |
1239 return $element; | |
1240 } | |
1241 | |
1242 /** | |
1243 * @} End of "defgroup field_formatter". | |
1244 */ | |
1245 | |
1246 /** | |
1247 * @ingroup field_attach | |
1248 * @{ | |
1249 */ | |
1250 | |
1251 /** | |
1252 * Act on field_attach_form(). | |
1253 * | |
1254 * This hook is invoked after the field module has performed the operation. | |
1255 * Implementing modules should alter the $form or $form_state parameters. | |
1256 * | |
1257 * @param $entity_type | |
1258 * The type of $entity; for example, 'node' or 'user'. | |
1259 * @param $entity | |
1260 * The entity for which an edit form is being built. | |
1261 * @param $form | |
1262 * The form structure where field elements are attached to. This might be a | |
1263 * full form structure, or a sub-element of a larger form. The | |
1264 * $form['#parents'] property can be used to identify the corresponding part | |
1265 * of $form_state['values']. Hook implementations that need to act on the | |
1266 * top-level properties of the global form (like #submit, #validate...) can | |
1267 * add a #process callback to the array received in the $form parameter, and | |
1268 * act on the $complete_form parameter in the process callback. | |
1269 * @param $form_state | |
1270 * An associative array containing the current state of the form. | |
1271 * @param $langcode | |
1272 * The language the field values are going to be entered in. If no language | |
1273 * is provided the default site language will be used. | |
1274 */ | |
1275 function hook_field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode) { | |
1276 // Add a checkbox allowing a given field to be emptied. | |
1277 // See hook_field_attach_submit() for the corresponding processing code. | |
1278 $form['empty_field_foo'] = array( | |
1279 '#type' => 'checkbox', | |
1280 '#title' => t("Empty the 'field_foo' field"), | |
1281 ); | |
1282 } | |
1283 | |
1284 /** | |
1285 * Act on field_attach_load(). | |
1286 * | |
1287 * This hook is invoked after the field module has performed the operation. | |
1288 * | |
1289 * Unlike other field_attach hooks, this hook accounts for 'multiple loads'. | |
1290 * Instead of the usual $entity parameter, it accepts an array of entities, | |
1291 * indexed by entity ID. For performance reasons, information for all available | |
1292 * entities should be loaded in a single query where possible. | |
1293 * | |
1294 * The changes made to the entities' field values get cached by the field cache | |
1295 * for subsequent loads. | |
1296 * | |
1297 * See field_attach_load() for details and arguments. | |
1298 */ | |
1299 function hook_field_attach_load($entity_type, $entities, $age, $options) { | |
1300 // @todo Needs function body. | |
1301 } | |
1302 | |
1303 /** | |
1304 * Act on field_attach_validate(). | |
1305 * | |
1306 * This hook is invoked after the field module has performed the operation. | |
1307 * | |
1308 * See field_attach_validate() for details and arguments. | |
1309 */ | |
1310 function hook_field_attach_validate($entity_type, $entity, &$errors) { | |
1311 // @todo Needs function body. | |
1312 } | |
1313 | |
1314 /** | |
1315 * Act on field_attach_submit(). | |
1316 * | |
1317 * This hook is invoked after the field module has performed the operation. | |
1318 * | |
1319 * @param $entity_type | |
1320 * The type of $entity; for example, 'node' or 'user'. | |
1321 * @param $entity | |
1322 * The entity for which an edit form is being submitted. The incoming form | |
1323 * values have been extracted as field values of the $entity object. | |
1324 * @param $form | |
1325 * The form structure where field elements are attached to. This might be a | |
1326 * full form structure, or a sub-part of a larger form. The $form['#parents'] | |
1327 * property can be used to identify the corresponding part of | |
1328 * $form_state['values']. | |
1329 * @param $form_state | |
1330 * An associative array containing the current state of the form. | |
1331 */ | |
1332 function hook_field_attach_submit($entity_type, $entity, $form, &$form_state) { | |
1333 // Sample case of an 'Empty the field' checkbox added on the form, allowing | |
1334 // a given field to be emptied. | |
1335 $values = drupal_array_get_nested_value($form_state['values'], $form['#parents']); | |
1336 if (!empty($values['empty_field_foo'])) { | |
1337 unset($entity->field_foo); | |
1338 } | |
1339 } | |
1340 | |
1341 /** | |
1342 * Act on field_attach_presave(). | |
1343 * | |
1344 * This hook is invoked after the field module has performed the operation. | |
1345 * | |
1346 * See field_attach_presave() for details and arguments. | |
1347 */ | |
1348 function hook_field_attach_presave($entity_type, $entity) { | |
1349 // @todo Needs function body. | |
1350 } | |
1351 | |
1352 /** | |
1353 * Act on field_attach_insert(). | |
1354 * | |
1355 * This hook is invoked after the field module has performed the operation. | |
1356 * | |
1357 * See field_attach_insert() for details and arguments. | |
1358 */ | |
1359 function hook_field_attach_insert($entity_type, $entity) { | |
1360 // @todo Needs function body. | |
1361 } | |
1362 | |
1363 /** | |
1364 * Act on field_attach_update(). | |
1365 * | |
1366 * This hook is invoked after the field module has performed the operation. | |
1367 * | |
1368 * See field_attach_update() for details and arguments. | |
1369 */ | |
1370 function hook_field_attach_update($entity_type, $entity) { | |
1371 // @todo Needs function body. | |
1372 } | |
1373 | |
1374 /** | |
1375 * Alter field_attach_preprocess() variables. | |
1376 * | |
1377 * This hook is invoked while preprocessing the field.tpl.php template file | |
1378 * in field_attach_preprocess(). | |
1379 * | |
1380 * @param $variables | |
1381 * The variables array is passed by reference and will be populated with field | |
1382 * values. | |
1383 * @param $context | |
1384 * An associative array containing: | |
1385 * - entity_type: The type of $entity; for example, 'node' or 'user'. | |
1386 * - entity: The entity with fields to render. | |
1387 * - element: The structured array containing the values ready for rendering. | |
1388 */ | |
1389 function hook_field_attach_preprocess_alter(&$variables, $context) { | |
1390 // @todo Needs function body. | |
1391 } | |
1392 | |
1393 /** | |
1394 * Act on field_attach_delete(). | |
1395 * | |
1396 * This hook is invoked after the field module has performed the operation. | |
1397 * | |
1398 * See field_attach_delete() for details and arguments. | |
1399 */ | |
1400 function hook_field_attach_delete($entity_type, $entity) { | |
1401 // @todo Needs function body. | |
1402 } | |
1403 | |
1404 /** | |
1405 * Act on field_attach_delete_revision(). | |
1406 * | |
1407 * This hook is invoked after the field module has performed the operation. | |
1408 * | |
1409 * See field_attach_delete_revision() for details and arguments. | |
1410 */ | |
1411 function hook_field_attach_delete_revision($entity_type, $entity) { | |
1412 // @todo Needs function body. | |
1413 } | |
1414 | |
1415 /** | |
1416 * Act on field_purge_data(). | |
1417 * | |
1418 * This hook is invoked in field_purge_data() and allows modules to act on | |
1419 * purging data from a single field pseudo-entity. For example, if a module | |
1420 * relates data in the field with its own data, it may purge its own data | |
1421 * during this process as well. | |
1422 * | |
1423 * @param $entity_type | |
1424 * The type of $entity; for example, 'node' or 'user'. | |
1425 * @param $entity | |
1426 * The pseudo-entity whose field data is being purged. | |
1427 * @param $field | |
1428 * The (possibly deleted) field whose data is being purged. | |
1429 * @param $instance | |
1430 * The deleted field instance whose data is being purged. | |
1431 * | |
1432 * @see @link field_purge Field API bulk data deletion @endlink | |
1433 * @see field_purge_data() | |
1434 */ | |
1435 function hook_field_attach_purge($entity_type, $entity, $field, $instance) { | |
1436 // find the corresponding data in mymodule and purge it | |
1437 if ($entity_type == 'node' && $field->field_name == 'my_field_name') { | |
1438 mymodule_remove_mydata($entity->nid); | |
1439 } | |
1440 } | |
1441 | |
1442 /** | |
1443 * Perform alterations on field_attach_view() or field_view_field(). | |
1444 * | |
1445 * This hook is invoked after the field module has performed the operation. | |
1446 * | |
1447 * @param $output | |
1448 * The structured content array tree for all of the entity's fields. | |
1449 * @param $context | |
1450 * An associative array containing: | |
1451 * - entity_type: The type of $entity; for example, 'node' or 'user'. | |
1452 * - entity: The entity with fields to render. | |
1453 * - view_mode: View mode; for example, 'full' or 'teaser'. | |
1454 * - display: Either a view mode string or an array of display settings. If | |
1455 * this hook is being invoked from field_attach_view(), the 'display' | |
1456 * element is set to the view mode string. If this hook is being invoked | |
1457 * from field_view_field(), this element is set to the $display argument | |
1458 * and the view_mode element is set to '_custom'. See field_view_field() | |
1459 * for more information on what its $display argument contains. | |
1460 * - language: The language code used for rendering. | |
1461 */ | |
1462 function hook_field_attach_view_alter(&$output, $context) { | |
1463 // Append RDF term mappings on displayed taxonomy links. | |
1464 foreach (element_children($output) as $field_name) { | |
1465 $element = &$output[$field_name]; | |
1466 if ($element['#field_type'] == 'taxonomy_term_reference' && $element['#formatter'] == 'taxonomy_term_reference_link') { | |
1467 foreach ($element['#items'] as $delta => $item) { | |
1468 $term = $item['taxonomy_term']; | |
1469 if (!empty($term->rdf_mapping['rdftype'])) { | |
1470 $element[$delta]['#options']['attributes']['typeof'] = $term->rdf_mapping['rdftype']; | |
1471 } | |
1472 if (!empty($term->rdf_mapping['name']['predicates'])) { | |
1473 $element[$delta]['#options']['attributes']['property'] = $term->rdf_mapping['name']['predicates']; | |
1474 } | |
1475 } | |
1476 } | |
1477 } | |
1478 } | |
1479 | |
1480 /** | |
1481 * Perform alterations on field_attach_prepare_translation(). | |
1482 * | |
1483 * This hook is invoked after the field module has performed the operation. | |
1484 * | |
1485 * @param $entity | |
1486 * The entity being prepared for translation. | |
1487 * @param $context | |
1488 * An associative array containing: | |
1489 * - entity_type: The type of $entity; e.g. 'node' or 'user'. | |
1490 * - langcode: The language the entity has to be translated in. | |
1491 * - source_entity: The entity holding the field values to be translated. | |
1492 * - source_langcode: The source language from which translate. | |
1493 */ | |
1494 function hook_field_attach_prepare_translation_alter(&$entity, $context) { | |
1495 if ($context['entity_type'] == 'custom_entity_type') { | |
1496 $entity->custom_field = $context['source_entity']->custom_field; | |
1497 } | |
1498 } | |
1499 | |
1500 /** | |
1501 * Perform alterations on field_language() values. | |
1502 * | |
1503 * This hook is invoked to alter the array of display languages for the given | |
1504 * entity. | |
1505 * | |
1506 * @param $display_language | |
1507 * A reference to an array of language codes keyed by field name. | |
1508 * @param $context | |
1509 * An associative array containing: | |
1510 * - entity_type: The type of the entity to be displayed. | |
1511 * - entity: The entity with fields to render. | |
1512 * - langcode: The language code $entity has to be displayed in. | |
1513 */ | |
1514 function hook_field_language_alter(&$display_language, $context) { | |
1515 // Do not apply core language fallback rules if they are disabled or if Locale | |
1516 // is not registered as a translation handler. | |
1517 if (variable_get('locale_field_language_fallback', TRUE) && field_has_translation_handler($context['entity_type'], 'locale')) { | |
1518 locale_field_language_fallback($display_language, $context['entity'], $context['language']); | |
1519 } | |
1520 } | |
1521 | |
1522 /** | |
1523 * Alter field_available_languages() values. | |
1524 * | |
1525 * This hook is invoked from field_available_languages() to allow modules to | |
1526 * alter the array of available languages for the given field. | |
1527 * | |
1528 * @param $languages | |
1529 * A reference to an array of language codes to be made available. | |
1530 * @param $context | |
1531 * An associative array containing: | |
1532 * - entity_type: The type of the entity the field is attached to. | |
1533 * - field: A field data structure. | |
1534 */ | |
1535 function hook_field_available_languages_alter(&$languages, $context) { | |
1536 // Add an unavailable language. | |
1537 $languages[] = 'xx'; | |
1538 | |
1539 // Remove an available language. | |
1540 $index = array_search('yy', $languages); | |
1541 unset($languages[$index]); | |
1542 } | |
1543 | |
1544 /** | |
1545 * Act on field_attach_create_bundle(). | |
1546 * | |
1547 * This hook is invoked after the field module has performed the operation. | |
1548 * | |
1549 * See field_attach_create_bundle() for details and arguments. | |
1550 */ | |
1551 function hook_field_attach_create_bundle($entity_type, $bundle) { | |
1552 // When a new bundle is created, the menu needs to be rebuilt to add the | |
1553 // Field UI menu item tabs. | |
1554 variable_set('menu_rebuild_needed', TRUE); | |
1555 } | |
1556 | |
1557 /** | |
1558 * Act on field_attach_rename_bundle(). | |
1559 * | |
1560 * This hook is invoked after the field module has performed the operation. | |
1561 * | |
1562 * See field_attach_rename_bundle() for details and arguments. | |
1563 */ | |
1564 function hook_field_attach_rename_bundle($entity_type, $bundle_old, $bundle_new) { | |
1565 // Update the extra weights variable with new information. | |
1566 if ($bundle_old !== $bundle_new) { | |
1567 $extra_weights = variable_get('field_extra_weights', array()); | |
1568 if (isset($info[$entity_type][$bundle_old])) { | |
1569 $extra_weights[$entity_type][$bundle_new] = $extra_weights[$entity_type][$bundle_old]; | |
1570 unset($extra_weights[$entity_type][$bundle_old]); | |
1571 variable_set('field_extra_weights', $extra_weights); | |
1572 } | |
1573 } | |
1574 } | |
1575 | |
1576 /** | |
1577 * Act on field_attach_delete_bundle. | |
1578 * | |
1579 * This hook is invoked after the field module has performed the operation. | |
1580 * | |
1581 * @param $entity_type | |
1582 * The type of entity; for example, 'node' or 'user'. | |
1583 * @param $bundle | |
1584 * The bundle that was just deleted. | |
1585 * @param $instances | |
1586 * An array of all instances that existed for the bundle before it was | |
1587 * deleted. | |
1588 */ | |
1589 function hook_field_attach_delete_bundle($entity_type, $bundle, $instances) { | |
1590 // Remove the extra weights variable information for this bundle. | |
1591 $extra_weights = variable_get('field_extra_weights', array()); | |
1592 if (isset($extra_weights[$entity_type][$bundle])) { | |
1593 unset($extra_weights[$entity_type][$bundle]); | |
1594 variable_set('field_extra_weights', $extra_weights); | |
1595 } | |
1596 } | |
1597 | |
1598 /** | |
1599 * @} End of "defgroup field_attach". | |
1600 */ | |
1601 | |
1602 /** | |
1603 * @addtogroup field_storage | |
1604 * @{ | |
1605 */ | |
1606 | |
1607 /** | |
1608 * Expose Field API storage backends. | |
1609 * | |
1610 * @return | |
1611 * An array describing the storage backends implemented by the module. | |
1612 * The keys are storage backend names. To avoid name clashes, storage backend | |
1613 * names should be prefixed with the name of the module that exposes them. | |
1614 * The values are arrays describing the storage backend, with the following | |
1615 * key/value pairs: | |
1616 * - label: The human-readable name of the storage backend. | |
1617 * - description: A short description for the storage backend. | |
1618 * - settings: An array whose keys are the names of the settings available | |
1619 * for the storage backend, and whose values are the default values for | |
1620 * those settings. | |
1621 */ | |
1622 function hook_field_storage_info() { | |
1623 return array( | |
1624 'field_sql_storage' => array( | |
1625 'label' => t('Default SQL storage'), | |
1626 'description' => t('Stores fields in the local SQL database, using per-field tables.'), | |
1627 'settings' => array(), | |
1628 ), | |
1629 ); | |
1630 } | |
1631 | |
1632 /** | |
1633 * Perform alterations on Field API storage types. | |
1634 * | |
1635 * @param $info | |
1636 * Array of informations on storage types exposed by | |
1637 * hook_field_field_storage_info() implementations. | |
1638 */ | |
1639 function hook_field_storage_info_alter(&$info) { | |
1640 // Add a setting to a storage type. | |
1641 $info['field_sql_storage']['settings'] += array( | |
1642 'mymodule_additional_setting' => 'default value', | |
1643 ); | |
1644 } | |
1645 | |
1646 /** | |
1647 * Reveal the internal details about the storage for a field. | |
1648 * | |
1649 * For example, an SQL storage module might return the Schema API structure for | |
1650 * the table. A key/value storage module might return the server name, | |
1651 * authentication credentials, and bin name. | |
1652 * | |
1653 * Field storage modules are not obligated to implement this hook. Modules | |
1654 * that rely on these details must only use them for read operations. | |
1655 * | |
1656 * @param $field | |
1657 * A field structure. | |
1658 * | |
1659 * @return | |
1660 * An array of details. | |
1661 * - The first dimension is a store type (sql, solr, etc). | |
1662 * - The second dimension indicates the age of the values in the store | |
1663 * FIELD_LOAD_CURRENT or FIELD_LOAD_REVISION. | |
1664 * - Other dimensions are specific to the field storage module. | |
1665 * | |
1666 * @see hook_field_storage_details_alter() | |
1667 */ | |
1668 function hook_field_storage_details($field) { | |
1669 $details = array(); | |
1670 | |
1671 // Add field columns. | |
1672 foreach ((array) $field['columns'] as $column_name => $attributes) { | |
1673 $real_name = _field_sql_storage_columnname($field['field_name'], $column_name); | |
1674 $columns[$column_name] = $real_name; | |
1675 } | |
1676 return array( | |
1677 'sql' => array( | |
1678 FIELD_LOAD_CURRENT => array( | |
1679 _field_sql_storage_tablename($field) => $columns, | |
1680 ), | |
1681 FIELD_LOAD_REVISION => array( | |
1682 _field_sql_storage_revision_tablename($field) => $columns, | |
1683 ), | |
1684 ), | |
1685 ); | |
1686 } | |
1687 | |
1688 /** | |
1689 * Perform alterations on Field API storage details. | |
1690 * | |
1691 * @param $details | |
1692 * An array of storage details for fields as exposed by | |
1693 * hook_field_storage_details() implementations. | |
1694 * @param $field | |
1695 * A field structure. | |
1696 * | |
1697 * @see hook_field_storage_details() | |
1698 */ | |
1699 function hook_field_storage_details_alter(&$details, $field) { | |
1700 if ($field['field_name'] == 'field_of_interest') { | |
1701 $columns = array(); | |
1702 foreach ((array) $field['columns'] as $column_name => $attributes) { | |
1703 $columns[$column_name] = $column_name; | |
1704 } | |
1705 $details['drupal_variables'] = array( | |
1706 FIELD_LOAD_CURRENT => array( | |
1707 'moon' => $columns, | |
1708 ), | |
1709 FIELD_LOAD_REVISION => array( | |
1710 'mars' => $columns, | |
1711 ), | |
1712 ); | |
1713 } | |
1714 } | |
1715 | |
1716 /** | |
1717 * Load field data for a set of entities. | |
1718 * | |
1719 * This hook is invoked from field_attach_load() to ask the field storage | |
1720 * module to load field data. | |
1721 * | |
1722 * Modules implementing this hook should load field values and add them to | |
1723 * objects in $entities. Fields with no values should be added as empty | |
1724 * arrays. | |
1725 * | |
1726 * @param $entity_type | |
1727 * The type of entity, such as 'node' or 'user'. | |
1728 * @param $entities | |
1729 * The array of entity objects to add fields to, keyed by entity ID. | |
1730 * @param $age | |
1731 * FIELD_LOAD_CURRENT to load the most recent revision for all fields, or | |
1732 * FIELD_LOAD_REVISION to load the version indicated by each entity. | |
1733 * @param $fields | |
1734 * An array listing the fields to be loaded. The keys of the array are field | |
1735 * IDs, and the values of the array are the entity IDs (or revision IDs, | |
1736 * depending on the $age parameter) to add each field to. | |
1737 * @param $options | |
1738 * An associative array of additional options, with the following keys: | |
1739 * - deleted: If TRUE, deleted fields should be loaded as well as | |
1740 * non-deleted fields. If unset or FALSE, only non-deleted fields should be | |
1741 * loaded. | |
1742 */ | |
1743 function hook_field_storage_load($entity_type, $entities, $age, $fields, $options) { | |
1744 $load_current = $age == FIELD_LOAD_CURRENT; | |
1745 | |
1746 foreach ($fields as $field_id => $ids) { | |
1747 // By the time this hook runs, the relevant field definitions have been | |
1748 // populated and cached in FieldInfo, so calling field_info_field_by_id() | |
1749 // on each field individually is more efficient than loading all fields in | |
1750 // memory upfront with field_info_field_by_ids(). | |
1751 $field = field_info_field_by_id($field_id); | |
1752 $field_name = $field['field_name']; | |
1753 $table = $load_current ? _field_sql_storage_tablename($field) : _field_sql_storage_revision_tablename($field); | |
1754 | |
1755 $query = db_select($table, 't') | |
1756 ->fields('t') | |
1757 ->condition('entity_type', $entity_type) | |
1758 ->condition($load_current ? 'entity_id' : 'revision_id', $ids, 'IN') | |
1759 ->condition('language', field_available_languages($entity_type, $field), 'IN') | |
1760 ->orderBy('delta'); | |
1761 | |
1762 if (empty($options['deleted'])) { | |
1763 $query->condition('deleted', 0); | |
1764 } | |
1765 | |
1766 $results = $query->execute(); | |
1767 | |
1768 $delta_count = array(); | |
1769 foreach ($results as $row) { | |
1770 if (!isset($delta_count[$row->entity_id][$row->language])) { | |
1771 $delta_count[$row->entity_id][$row->language] = 0; | |
1772 } | |
1773 | |
1774 if ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED || $delta_count[$row->entity_id][$row->language] < $field['cardinality']) { | |
1775 $item = array(); | |
1776 // For each column declared by the field, populate the item | |
1777 // from the prefixed database column. | |
1778 foreach ($field['columns'] as $column => $attributes) { | |
1779 $column_name = _field_sql_storage_columnname($field_name, $column); | |
1780 $item[$column] = $row->$column_name; | |
1781 } | |
1782 | |
1783 // Add the item to the field values for the entity. | |
1784 $entities[$row->entity_id]->{$field_name}[$row->language][] = $item; | |
1785 $delta_count[$row->entity_id][$row->language]++; | |
1786 } | |
1787 } | |
1788 } | |
1789 } | |
1790 | |
1791 /** | |
1792 * Write field data for an entity. | |
1793 * | |
1794 * This hook is invoked from field_attach_insert() and field_attach_update(), | |
1795 * to ask the field storage module to save field data. | |
1796 * | |
1797 * @param $entity_type | |
1798 * The entity type of entity, such as 'node' or 'user'. | |
1799 * @param $entity | |
1800 * The entity on which to operate. | |
1801 * @param $op | |
1802 * FIELD_STORAGE_UPDATE when updating an existing entity, | |
1803 * FIELD_STORAGE_INSERT when inserting a new entity. | |
1804 * @param $fields | |
1805 * An array listing the fields to be written. The keys and values of the | |
1806 * array are field IDs. | |
1807 */ | |
1808 function hook_field_storage_write($entity_type, $entity, $op, $fields) { | |
1809 list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity); | |
1810 if (!isset($vid)) { | |
1811 $vid = $id; | |
1812 } | |
1813 | |
1814 foreach ($fields as $field_id) { | |
1815 $field = field_info_field_by_id($field_id); | |
1816 $field_name = $field['field_name']; | |
1817 $table_name = _field_sql_storage_tablename($field); | |
1818 $revision_name = _field_sql_storage_revision_tablename($field); | |
1819 | |
1820 $all_languages = field_available_languages($entity_type, $field); | |
1821 $field_languages = array_intersect($all_languages, array_keys((array) $entity->$field_name)); | |
1822 | |
1823 // Delete and insert, rather than update, in case a value was added. | |
1824 if ($op == FIELD_STORAGE_UPDATE) { | |
1825 // Delete languages present in the incoming $entity->$field_name. | |
1826 // Delete all languages if $entity->$field_name is empty. | |
1827 $languages = !empty($entity->$field_name) ? $field_languages : $all_languages; | |
1828 if ($languages) { | |
1829 db_delete($table_name) | |
1830 ->condition('entity_type', $entity_type) | |
1831 ->condition('entity_id', $id) | |
1832 ->condition('language', $languages, 'IN') | |
1833 ->execute(); | |
1834 db_delete($revision_name) | |
1835 ->condition('entity_type', $entity_type) | |
1836 ->condition('entity_id', $id) | |
1837 ->condition('revision_id', $vid) | |
1838 ->condition('language', $languages, 'IN') | |
1839 ->execute(); | |
1840 } | |
1841 } | |
1842 | |
1843 // Prepare the multi-insert query. | |
1844 $do_insert = FALSE; | |
1845 $columns = array('entity_type', 'entity_id', 'revision_id', 'bundle', 'delta', 'language'); | |
1846 foreach ($field['columns'] as $column => $attributes) { | |
1847 $columns[] = _field_sql_storage_columnname($field_name, $column); | |
1848 } | |
1849 $query = db_insert($table_name)->fields($columns); | |
1850 $revision_query = db_insert($revision_name)->fields($columns); | |
1851 | |
1852 foreach ($field_languages as $langcode) { | |
1853 $items = (array) $entity->{$field_name}[$langcode]; | |
1854 $delta_count = 0; | |
1855 foreach ($items as $delta => $item) { | |
1856 // We now know we have someting to insert. | |
1857 $do_insert = TRUE; | |
1858 $record = array( | |
1859 'entity_type' => $entity_type, | |
1860 'entity_id' => $id, | |
1861 'revision_id' => $vid, | |
1862 'bundle' => $bundle, | |
1863 'delta' => $delta, | |
1864 'language' => $langcode, | |
1865 ); | |
1866 foreach ($field['columns'] as $column => $attributes) { | |
1867 $record[_field_sql_storage_columnname($field_name, $column)] = isset($item[$column]) ? $item[$column] : NULL; | |
1868 } | |
1869 $query->values($record); | |
1870 if (isset($vid)) { | |
1871 $revision_query->values($record); | |
1872 } | |
1873 | |
1874 if ($field['cardinality'] != FIELD_CARDINALITY_UNLIMITED && ++$delta_count == $field['cardinality']) { | |
1875 break; | |
1876 } | |
1877 } | |
1878 } | |
1879 | |
1880 // Execute the query if we have values to insert. | |
1881 if ($do_insert) { | |
1882 $query->execute(); | |
1883 $revision_query->execute(); | |
1884 } | |
1885 } | |
1886 } | |
1887 | |
1888 /** | |
1889 * Delete all field data for an entity. | |
1890 * | |
1891 * This hook is invoked from field_attach_delete() to ask the field storage | |
1892 * module to delete field data. | |
1893 * | |
1894 * @param $entity_type | |
1895 * The entity type of entity, such as 'node' or 'user'. | |
1896 * @param $entity | |
1897 * The entity on which to operate. | |
1898 * @param $fields | |
1899 * An array listing the fields to delete. The keys and values of the | |
1900 * array are field IDs. | |
1901 */ | |
1902 function hook_field_storage_delete($entity_type, $entity, $fields) { | |
1903 list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity); | |
1904 | |
1905 foreach (field_info_instances($entity_type, $bundle) as $instance) { | |
1906 if (isset($fields[$instance['field_id']])) { | |
1907 $field = field_info_field_by_id($instance['field_id']); | |
1908 field_sql_storage_field_storage_purge($entity_type, $entity, $field, $instance); | |
1909 } | |
1910 } | |
1911 } | |
1912 | |
1913 /** | |
1914 * Delete a single revision of field data for an entity. | |
1915 * | |
1916 * This hook is invoked from field_attach_delete_revision() to ask the field | |
1917 * storage module to delete field revision data. | |
1918 * | |
1919 * Deleting the current (most recently written) revision is not | |
1920 * allowed as has undefined results. | |
1921 * | |
1922 * @param $entity_type | |
1923 * The entity type of entity, such as 'node' or 'user'. | |
1924 * @param $entity | |
1925 * The entity on which to operate. The revision to delete is | |
1926 * indicated by the entity's revision ID property, as identified by | |
1927 * hook_fieldable_info() for $entity_type. | |
1928 * @param $fields | |
1929 * An array listing the fields to delete. The keys and values of the | |
1930 * array are field IDs. | |
1931 */ | |
1932 function hook_field_storage_delete_revision($entity_type, $entity, $fields) { | |
1933 list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity); | |
1934 | |
1935 if (isset($vid)) { | |
1936 foreach ($fields as $field_id) { | |
1937 $field = field_info_field_by_id($field_id); | |
1938 $revision_name = _field_sql_storage_revision_tablename($field); | |
1939 db_delete($revision_name) | |
1940 ->condition('entity_type', $entity_type) | |
1941 ->condition('entity_id', $id) | |
1942 ->condition('revision_id', $vid) | |
1943 ->execute(); | |
1944 } | |
1945 } | |
1946 } | |
1947 | |
1948 /** | |
1949 * Execute an EntityFieldQuery. | |
1950 * | |
1951 * This hook is called to find the entities having certain entity and field | |
1952 * conditions and sort them in the given field order. If the field storage | |
1953 * engine also handles property sorts and orders, it should unset those | |
1954 * properties in the called object to signal that those have been handled. | |
1955 * | |
1956 * @param EntityFieldQuery $query | |
1957 * An EntityFieldQuery. | |
1958 * | |
1959 * @return | |
1960 * See EntityFieldQuery::execute() for the return values. | |
1961 */ | |
1962 function hook_field_storage_query($query) { | |
1963 $groups = array(); | |
1964 if ($query->age == FIELD_LOAD_CURRENT) { | |
1965 $tablename_function = '_field_sql_storage_tablename'; | |
1966 $id_key = 'entity_id'; | |
1967 } | |
1968 else { | |
1969 $tablename_function = '_field_sql_storage_revision_tablename'; | |
1970 $id_key = 'revision_id'; | |
1971 } | |
1972 $table_aliases = array(); | |
1973 // Add tables for the fields used. | |
1974 foreach ($query->fields as $key => $field) { | |
1975 $tablename = $tablename_function($field); | |
1976 // Every field needs a new table. | |
1977 $table_alias = $tablename . $key; | |
1978 $table_aliases[$key] = $table_alias; | |
1979 if ($key) { | |
1980 $select_query->join($tablename, $table_alias, "$table_alias.entity_type = $field_base_table.entity_type AND $table_alias.$id_key = $field_base_table.$id_key"); | |
1981 } | |
1982 else { | |
1983 $select_query = db_select($tablename, $table_alias); | |
1984 $select_query->addTag('entity_field_access'); | |
1985 $select_query->addMetaData('base_table', $tablename); | |
1986 $select_query->fields($table_alias, array('entity_type', 'entity_id', 'revision_id', 'bundle')); | |
1987 $field_base_table = $table_alias; | |
1988 } | |
1989 if ($field['cardinality'] != 1) { | |
1990 $select_query->distinct(); | |
1991 } | |
1992 } | |
1993 | |
1994 // Add field conditions. | |
1995 foreach ($query->fieldConditions as $key => $condition) { | |
1996 $table_alias = $table_aliases[$key]; | |
1997 $field = $condition['field']; | |
1998 // Add the specified condition. | |
1999 $sql_field = "$table_alias." . _field_sql_storage_columnname($field['field_name'], $condition['column']); | |
2000 $query->addCondition($select_query, $sql_field, $condition); | |
2001 // Add delta / language group conditions. | |
2002 foreach (array('delta', 'language') as $column) { | |
2003 if (isset($condition[$column . '_group'])) { | |
2004 $group_name = $condition[$column . '_group']; | |
2005 if (!isset($groups[$column][$group_name])) { | |
2006 $groups[$column][$group_name] = $table_alias; | |
2007 } | |
2008 else { | |
2009 $select_query->where("$table_alias.$column = " . $groups[$column][$group_name] . ".$column"); | |
2010 } | |
2011 } | |
2012 } | |
2013 } | |
2014 | |
2015 if (isset($query->deleted)) { | |
2016 $select_query->condition("$field_base_table.deleted", (int) $query->deleted); | |
2017 } | |
2018 | |
2019 // Is there a need to sort the query by property? | |
2020 $has_property_order = FALSE; | |
2021 foreach ($query->order as $order) { | |
2022 if ($order['type'] == 'property') { | |
2023 $has_property_order = TRUE; | |
2024 } | |
2025 } | |
2026 | |
2027 if ($query->propertyConditions || $has_property_order) { | |
2028 if (empty($query->entityConditions['entity_type']['value'])) { | |
2029 throw new EntityFieldQueryException('Property conditions and orders must have an entity type defined.'); | |
2030 } | |
2031 $entity_type = $query->entityConditions['entity_type']['value']; | |
2032 $entity_base_table = _field_sql_storage_query_join_entity($select_query, $entity_type, $field_base_table); | |
2033 $query->entityConditions['entity_type']['operator'] = '='; | |
2034 foreach ($query->propertyConditions as $property_condition) { | |
2035 $query->addCondition($select_query, "$entity_base_table." . $property_condition['column'], $property_condition); | |
2036 } | |
2037 } | |
2038 foreach ($query->entityConditions as $key => $condition) { | |
2039 $query->addCondition($select_query, "$field_base_table.$key", $condition); | |
2040 } | |
2041 | |
2042 // Order the query. | |
2043 foreach ($query->order as $order) { | |
2044 if ($order['type'] == 'entity') { | |
2045 $key = $order['specifier']; | |
2046 $select_query->orderBy("$field_base_table.$key", $order['direction']); | |
2047 } | |
2048 elseif ($order['type'] == 'field') { | |
2049 $specifier = $order['specifier']; | |
2050 $field = $specifier['field']; | |
2051 $table_alias = $table_aliases[$specifier['index']]; | |
2052 $sql_field = "$table_alias." . _field_sql_storage_columnname($field['field_name'], $specifier['column']); | |
2053 $select_query->orderBy($sql_field, $order['direction']); | |
2054 } | |
2055 elseif ($order['type'] == 'property') { | |
2056 $select_query->orderBy("$entity_base_table." . $order['specifier'], $order['direction']); | |
2057 } | |
2058 } | |
2059 | |
2060 return $query->finishQuery($select_query, $id_key); | |
2061 } | |
2062 | |
2063 /** | |
2064 * Act on creation of a new field. | |
2065 * | |
2066 * This hook is invoked from field_create_field() to ask the field storage | |
2067 * module to save field information and prepare for storing field instances. | |
2068 * If there is a problem, the field storage module should throw an exception. | |
2069 * | |
2070 * @param $field | |
2071 * The field structure being created. | |
2072 */ | |
2073 function hook_field_storage_create_field($field) { | |
2074 $schema = _field_sql_storage_schema($field); | |
2075 foreach ($schema as $name => $table) { | |
2076 db_create_table($name, $table); | |
2077 } | |
2078 drupal_get_schema(NULL, TRUE); | |
2079 } | |
2080 | |
2081 /** | |
2082 * Act on deletion of a field. | |
2083 * | |
2084 * This hook is invoked from field_delete_field() to ask the field storage | |
2085 * module to mark all information stored in the field for deletion. | |
2086 * | |
2087 * @param $field | |
2088 * The field being deleted. | |
2089 */ | |
2090 function hook_field_storage_delete_field($field) { | |
2091 // Mark all data associated with the field for deletion. | |
2092 $field['deleted'] = 0; | |
2093 $table = _field_sql_storage_tablename($field); | |
2094 $revision_table = _field_sql_storage_revision_tablename($field); | |
2095 db_update($table) | |
2096 ->fields(array('deleted' => 1)) | |
2097 ->execute(); | |
2098 | |
2099 // Move the table to a unique name while the table contents are being deleted. | |
2100 $field['deleted'] = 1; | |
2101 $new_table = _field_sql_storage_tablename($field); | |
2102 $revision_new_table = _field_sql_storage_revision_tablename($field); | |
2103 db_rename_table($table, $new_table); | |
2104 db_rename_table($revision_table, $revision_new_table); | |
2105 drupal_get_schema(NULL, TRUE); | |
2106 } | |
2107 | |
2108 /** | |
2109 * Act on deletion of a field instance. | |
2110 * | |
2111 * This hook is invoked from field_delete_instance() to ask the field storage | |
2112 * module to mark all information stored for the field instance for deletion. | |
2113 * | |
2114 * @param $instance | |
2115 * The instance being deleted. | |
2116 */ | |
2117 function hook_field_storage_delete_instance($instance) { | |
2118 $field = field_info_field($instance['field_name']); | |
2119 $table_name = _field_sql_storage_tablename($field); | |
2120 $revision_name = _field_sql_storage_revision_tablename($field); | |
2121 db_update($table_name) | |
2122 ->fields(array('deleted' => 1)) | |
2123 ->condition('entity_type', $instance['entity_type']) | |
2124 ->condition('bundle', $instance['bundle']) | |
2125 ->execute(); | |
2126 db_update($revision_name) | |
2127 ->fields(array('deleted' => 1)) | |
2128 ->condition('entity_type', $instance['entity_type']) | |
2129 ->condition('bundle', $instance['bundle']) | |
2130 ->execute(); | |
2131 } | |
2132 | |
2133 /** | |
2134 * Act before the storage backends load field data. | |
2135 * | |
2136 * This hook allows modules to load data before the Field Storage API, | |
2137 * optionally preventing the field storage module from doing so. | |
2138 * | |
2139 * This lets 3rd party modules override, mirror, shard, or otherwise store a | |
2140 * subset of fields in a different way than the current storage engine. | |
2141 * Possible use cases include per-bundle storage, per-combo-field storage, etc. | |
2142 * | |
2143 * Modules implementing this hook should load field values and add them to | |
2144 * objects in $entities. Fields with no values should be added as empty | |
2145 * arrays. In addition, fields loaded should be added as keys to $skip_fields. | |
2146 * | |
2147 * @param $entity_type | |
2148 * The type of entity, such as 'node' or 'user'. | |
2149 * @param $entities | |
2150 * The array of entity objects to add fields to, keyed by entity ID. | |
2151 * @param $age | |
2152 * FIELD_LOAD_CURRENT to load the most recent revision for all fields, or | |
2153 * FIELD_LOAD_REVISION to load the version indicated by each entity. | |
2154 * @param $skip_fields | |
2155 * An array keyed by field IDs whose data has already been loaded and | |
2156 * therefore should not be loaded again. Add a key to this array to indicate | |
2157 * that your module has already loaded a field. | |
2158 * @param $options | |
2159 * An associative array of additional options, with the following keys: | |
2160 * - field_id: The field ID that should be loaded. If unset, all fields | |
2161 * should be loaded. | |
2162 * - deleted: If TRUE, deleted fields should be loaded as well as | |
2163 * non-deleted fields. If unset or FALSE, only non-deleted fields should be | |
2164 * loaded. | |
2165 */ | |
2166 function hook_field_storage_pre_load($entity_type, $entities, $age, &$skip_fields, $options) { | |
2167 // @todo Needs function body. | |
2168 } | |
2169 | |
2170 /** | |
2171 * Act before the storage backends insert field data. | |
2172 * | |
2173 * This hook allows modules to store data before the Field Storage API, | |
2174 * optionally preventing the field storage module from doing so. | |
2175 * | |
2176 * @param $entity_type | |
2177 * The type of $entity; for example, 'node' or 'user'. | |
2178 * @param $entity | |
2179 * The entity with fields to save. | |
2180 * @param $skip_fields | |
2181 * An array keyed by field IDs whose data has already been written and | |
2182 * therefore should not be written again. The values associated with these | |
2183 * keys are not specified. | |
2184 * @return | |
2185 * Saved field IDs are set set as keys in $skip_fields. | |
2186 */ | |
2187 function hook_field_storage_pre_insert($entity_type, $entity, &$skip_fields) { | |
2188 if ($entity_type == 'node' && $entity->status && _forum_node_check_node_type($entity)) { | |
2189 $query = db_insert('forum_index')->fields(array('nid', 'title', 'tid', 'sticky', 'created', 'comment_count', 'last_comment_timestamp')); | |
2190 foreach ($entity->taxonomy_forums as $language) { | |
2191 foreach ($language as $delta) { | |
2192 $query->values(array( | |
2193 'nid' => $entity->nid, | |
2194 'title' => $entity->title, | |
2195 'tid' => $delta['value'], | |
2196 'sticky' => $entity->sticky, | |
2197 'created' => $entity->created, | |
2198 'comment_count' => 0, | |
2199 'last_comment_timestamp' => $entity->created, | |
2200 )); | |
2201 } | |
2202 } | |
2203 $query->execute(); | |
2204 } | |
2205 } | |
2206 | |
2207 /** | |
2208 * Act before the storage backends update field data. | |
2209 * | |
2210 * This hook allows modules to store data before the Field Storage API, | |
2211 * optionally preventing the field storage module from doing so. | |
2212 * | |
2213 * @param $entity_type | |
2214 * The type of $entity; for example, 'node' or 'user'. | |
2215 * @param $entity | |
2216 * The entity with fields to save. | |
2217 * @param $skip_fields | |
2218 * An array keyed by field IDs whose data has already been written and | |
2219 * therefore should not be written again. The values associated with these | |
2220 * keys are not specified. | |
2221 * @return | |
2222 * Saved field IDs are set set as keys in $skip_fields. | |
2223 */ | |
2224 function hook_field_storage_pre_update($entity_type, $entity, &$skip_fields) { | |
2225 $first_call = &drupal_static(__FUNCTION__, array()); | |
2226 | |
2227 if ($entity_type == 'node' && $entity->status && _forum_node_check_node_type($entity)) { | |
2228 // We don't maintain data for old revisions, so clear all previous values | |
2229 // from the table. Since this hook runs once per field, per entity, make | |
2230 // sure we only wipe values once. | |
2231 if (!isset($first_call[$entity->nid])) { | |
2232 $first_call[$entity->nid] = FALSE; | |
2233 db_delete('forum_index')->condition('nid', $entity->nid)->execute(); | |
2234 } | |
2235 // Only save data to the table if the node is published. | |
2236 if ($entity->status) { | |
2237 $query = db_insert('forum_index')->fields(array('nid', 'title', 'tid', 'sticky', 'created', 'comment_count', 'last_comment_timestamp')); | |
2238 foreach ($entity->taxonomy_forums as $language) { | |
2239 foreach ($language as $delta) { | |
2240 $query->values(array( | |
2241 'nid' => $entity->nid, | |
2242 'title' => $entity->title, | |
2243 'tid' => $delta['value'], | |
2244 'sticky' => $entity->sticky, | |
2245 'created' => $entity->created, | |
2246 'comment_count' => 0, | |
2247 'last_comment_timestamp' => $entity->created, | |
2248 )); | |
2249 } | |
2250 } | |
2251 $query->execute(); | |
2252 // The logic for determining last_comment_count is fairly complex, so | |
2253 // call _forum_update_forum_index() too. | |
2254 _forum_update_forum_index($entity->nid); | |
2255 } | |
2256 } | |
2257 } | |
2258 | |
2259 /** | |
2260 * Returns the maximum weight for the entity components handled by the module. | |
2261 * | |
2262 * Field API takes care of fields and 'extra_fields'. This hook is intended for | |
2263 * third-party modules adding other entity components (e.g. field_group). | |
2264 * | |
2265 * @param $entity_type | |
2266 * The type of entity; e.g. 'node' or 'user'. | |
2267 * @param $bundle | |
2268 * The bundle name. | |
2269 * @param $context | |
2270 * The context for which the maximum weight is requested. Either 'form', or | |
2271 * the name of a view mode. | |
2272 * @return | |
2273 * The maximum weight of the entity's components, or NULL if no components | |
2274 * were found. | |
2275 */ | |
2276 function hook_field_info_max_weight($entity_type, $bundle, $context) { | |
2277 $weights = array(); | |
2278 | |
2279 foreach (my_module_entity_additions($entity_type, $bundle, $context) as $addition) { | |
2280 $weights[] = $addition['weight']; | |
2281 } | |
2282 | |
2283 return $weights ? max($weights) : NULL; | |
2284 } | |
2285 | |
2286 /** | |
2287 * Alters the display settings of a field before it gets displayed. | |
2288 * | |
2289 * Note that instead of hook_field_display_alter(), which is called for all | |
2290 * fields on all entity types, hook_field_display_ENTITY_TYPE_alter() may be | |
2291 * used to alter display settings for fields on a specific entity type only. | |
2292 * | |
2293 * This hook is called once per field per displayed entity. If the result of the | |
2294 * hook involves reading from the database, it is highly recommended to | |
2295 * statically cache the information. | |
2296 * | |
2297 * @param $display | |
2298 * The display settings that will be used to display the field values, as | |
2299 * found in the 'display' key of $instance definitions. | |
2300 * @param $context | |
2301 * An associative array containing: | |
2302 * - entity_type: The entity type; e.g., 'node' or 'user'. | |
2303 * - field: The field being rendered. | |
2304 * - instance: The instance being rendered. | |
2305 * - entity: The entity being rendered. | |
2306 * - view_mode: The view mode, e.g. 'full', 'teaser'... | |
2307 * | |
2308 * @see hook_field_display_ENTITY_TYPE_alter() | |
2309 */ | |
2310 function hook_field_display_alter(&$display, $context) { | |
2311 // Leave field labels out of the search index. | |
2312 // Note: The check against $context['entity_type'] == 'node' could be avoided | |
2313 // by using hook_field_display_node_alter() instead of | |
2314 // hook_field_display_alter(), resulting in less function calls when | |
2315 // rendering non-node entities. | |
2316 if ($context['entity_type'] == 'node' && $context['view_mode'] == 'search_index') { | |
2317 $display['label'] = 'hidden'; | |
2318 } | |
2319 } | |
2320 | |
2321 /** | |
2322 * Alters the display settings of a field on a given entity type before it gets displayed. | |
2323 * | |
2324 * Modules can implement hook_field_display_ENTITY_TYPE_alter() to alter display | |
2325 * settings for fields on a specific entity type, rather than implementing | |
2326 * hook_field_display_alter(). | |
2327 * | |
2328 * This hook is called once per field per displayed entity. If the result of the | |
2329 * hook involves reading from the database, it is highly recommended to | |
2330 * statically cache the information. | |
2331 * | |
2332 * @param $display | |
2333 * The display settings that will be used to display the field values, as | |
2334 * found in the 'display' key of $instance definitions. | |
2335 * @param $context | |
2336 * An associative array containing: | |
2337 * - entity_type: The entity type; e.g., 'node' or 'user'. | |
2338 * - field: The field being rendered. | |
2339 * - instance: The instance being rendered. | |
2340 * - entity: The entity being rendered. | |
2341 * - view_mode: The view mode, e.g. 'full', 'teaser'... | |
2342 * | |
2343 * @see hook_field_display_alter() | |
2344 */ | |
2345 function hook_field_display_ENTITY_TYPE_alter(&$display, $context) { | |
2346 // Leave field labels out of the search index. | |
2347 if ($context['view_mode'] == 'search_index') { | |
2348 $display['label'] = 'hidden'; | |
2349 } | |
2350 } | |
2351 | |
2352 /** | |
2353 * Alters the display settings of pseudo-fields before an entity is displayed. | |
2354 * | |
2355 * This hook is called once per displayed entity. If the result of the hook | |
2356 * involves reading from the database, it is highly recommended to statically | |
2357 * cache the information. | |
2358 * | |
2359 * @param $displays | |
2360 * An array of display settings for the pseudo-fields in the entity, keyed | |
2361 * by pseudo-field names. | |
2362 * @param $context | |
2363 * An associative array containing: | |
2364 * - entity_type: The entity type; e.g., 'node' or 'user'. | |
2365 * - bundle: The bundle name. | |
2366 * - view_mode: The view mode, e.g. 'full', 'teaser'... | |
2367 */ | |
2368 function hook_field_extra_fields_display_alter(&$displays, $context) { | |
2369 if ($context['entity_type'] == 'taxonomy_term' && $context['view_mode'] == 'full') { | |
2370 $displays['description']['visible'] = FALSE; | |
2371 } | |
2372 } | |
2373 | |
2374 /** | |
2375 * Alters the widget properties of a field instance on a given entity type | |
2376 * before it gets displayed. | |
2377 * | |
2378 * Modules can implement hook_field_widget_properties_ENTITY_TYPE_alter() to | |
2379 * alter the widget properties for fields on a specific entity type, rather than | |
2380 * implementing hook_field_widget_properties_alter(). | |
2381 * | |
2382 * This hook is called once per field per displayed widget entity. If the result | |
2383 * of the hook involves reading from the database, it is highly recommended to | |
2384 * statically cache the information. | |
2385 * | |
2386 * @param $widget | |
2387 * The instance's widget properties. | |
2388 * @param $context | |
2389 * An associative array containing: | |
2390 * - entity_type: The entity type; e.g., 'node' or 'user'. | |
2391 * - entity: The entity object. | |
2392 * - field: The field that the widget belongs to. | |
2393 * - instance: The instance of the field. | |
2394 * | |
2395 * @see hook_field_widget_properties_alter() | |
2396 */ | |
2397 function hook_field_widget_properties_ENTITY_TYPE_alter(&$widget, $context) { | |
2398 // Change a widget's type according to the time of day. | |
2399 $field = $context['field']; | |
2400 if ($field['field_name'] == 'field_foo') { | |
2401 $time = date('H'); | |
2402 $widget['type'] = $time < 12 ? 'widget_am' : 'widget_pm'; | |
2403 } | |
2404 } | |
2405 | |
2406 /** | |
2407 * @} End of "addtogroup field_storage". | |
2408 */ | |
2409 | |
2410 /** | |
2411 * @addtogroup field_crud | |
2412 * @{ | |
2413 */ | |
2414 | |
2415 /** | |
2416 * Act on a field being created. | |
2417 * | |
2418 * This hook is invoked from field_create_field() after the field is created, to | |
2419 * allow modules to act on field creation. | |
2420 * | |
2421 * @param $field | |
2422 * The field just created. | |
2423 */ | |
2424 function hook_field_create_field($field) { | |
2425 // @todo Needs function body. | |
2426 } | |
2427 | |
2428 /** | |
2429 * Act on a field instance being created. | |
2430 * | |
2431 * This hook is invoked from field_create_instance() after the instance record | |
2432 * is saved, so it cannot be used to modify the instance itself. | |
2433 * | |
2434 * @param $instance | |
2435 * The instance just created. | |
2436 */ | |
2437 function hook_field_create_instance($instance) { | |
2438 // @todo Needs function body. | |
2439 } | |
2440 | |
2441 /** | |
2442 * Forbid a field update from occurring. | |
2443 * | |
2444 * Any module may forbid any update for any reason. For example, the | |
2445 * field's storage module might forbid an update if it would change | |
2446 * the storage schema while data for the field exists. A field type | |
2447 * module might forbid an update if it would change existing data's | |
2448 * semantics, or if there are external dependencies on field settings | |
2449 * that cannot be updated. | |
2450 * | |
2451 * To forbid the update from occurring, throw a FieldUpdateForbiddenException. | |
2452 * | |
2453 * @param $field | |
2454 * The field as it will be post-update. | |
2455 * @param $prior_field | |
2456 * The field as it is pre-update. | |
2457 * @param $has_data | |
2458 * Whether any data already exists for this field. | |
2459 */ | |
2460 function hook_field_update_forbid($field, $prior_field, $has_data) { | |
2461 // A 'list' field stores integer keys mapped to display values. If | |
2462 // the new field will have fewer values, and any data exists for the | |
2463 // abandoned keys, the field will have no way to display them. So, | |
2464 // forbid such an update. | |
2465 if ($has_data && count($field['settings']['allowed_values']) < count($prior_field['settings']['allowed_values'])) { | |
2466 // Identify the keys that will be lost. | |
2467 $lost_keys = array_diff(array_keys($field['settings']['allowed_values']), array_keys($prior_field['settings']['allowed_values'])); | |
2468 // If any data exist for those keys, forbid the update. | |
2469 $query = new EntityFieldQuery(); | |
2470 $found = $query | |
2471 ->fieldCondition($prior_field['field_name'], 'value', $lost_keys) | |
2472 ->range(0, 1) | |
2473 ->execute(); | |
2474 if ($found) { | |
2475 throw new FieldUpdateForbiddenException("Cannot update a list field not to include keys with existing data"); | |
2476 } | |
2477 } | |
2478 } | |
2479 | |
2480 /** | |
2481 * Act on a field being updated. | |
2482 * | |
2483 * This hook is invoked just after field is updated in field_update_field(). | |
2484 * | |
2485 * @param $field | |
2486 * The field as it is post-update. | |
2487 * @param $prior_field | |
2488 * The field as it was pre-update. | |
2489 * @param $has_data | |
2490 * Whether any data already exists for this field. | |
2491 */ | |
2492 function hook_field_update_field($field, $prior_field, $has_data) { | |
2493 // Reset the static value that keeps track of allowed values for list fields. | |
2494 drupal_static_reset('list_allowed_values'); | |
2495 } | |
2496 | |
2497 /** | |
2498 * Act on a field being deleted. | |
2499 * | |
2500 * This hook is invoked just after a field is deleted by field_delete_field(). | |
2501 * | |
2502 * @param $field | |
2503 * The field just deleted. | |
2504 */ | |
2505 function hook_field_delete_field($field) { | |
2506 // @todo Needs function body. | |
2507 } | |
2508 | |
2509 /** | |
2510 * Act on a field instance being updated. | |
2511 * | |
2512 * This hook is invoked from field_update_instance() after the instance record | |
2513 * is saved, so it cannot be used by a module to modify the instance itself. | |
2514 * | |
2515 * @param $instance | |
2516 * The instance as it is post-update. | |
2517 * @param $prior_instance | |
2518 * The instance as it was pre-update. | |
2519 */ | |
2520 function hook_field_update_instance($instance, $prior_instance) { | |
2521 // @todo Needs function body. | |
2522 } | |
2523 | |
2524 /** | |
2525 * Act on a field instance being deleted. | |
2526 * | |
2527 * This hook is invoked from field_delete_instance() after the instance is | |
2528 * deleted. | |
2529 * | |
2530 * @param $instance | |
2531 * The instance just deleted. | |
2532 */ | |
2533 function hook_field_delete_instance($instance) { | |
2534 // @todo Needs function body. | |
2535 } | |
2536 | |
2537 /** | |
2538 * Act on field records being read from the database. | |
2539 * | |
2540 * This hook is invoked from field_read_fields() on each field being read. | |
2541 * | |
2542 * @param $field | |
2543 * The field record just read from the database. | |
2544 */ | |
2545 function hook_field_read_field($field) { | |
2546 // @todo Needs function body. | |
2547 } | |
2548 | |
2549 /** | |
2550 * Act on a field record being read from the database. | |
2551 * | |
2552 * This hook is invoked from field_read_instances() on each instance being read. | |
2553 * | |
2554 * @param $instance | |
2555 * The instance record just read from the database. | |
2556 */ | |
2557 function hook_field_read_instance($instance) { | |
2558 // @todo Needs function body. | |
2559 } | |
2560 | |
2561 /** | |
2562 * Acts when a field record is being purged. | |
2563 * | |
2564 * In field_purge_field(), after the field configuration has been | |
2565 * removed from the database, the field storage module has had a chance to | |
2566 * run its hook_field_storage_purge_field(), and the field info cache | |
2567 * has been cleared, this hook is invoked on all modules to allow them to | |
2568 * respond to the field being purged. | |
2569 * | |
2570 * @param $field | |
2571 * The field being purged. | |
2572 */ | |
2573 function hook_field_purge_field($field) { | |
2574 db_delete('my_module_field_info') | |
2575 ->condition('id', $field['id']) | |
2576 ->execute(); | |
2577 } | |
2578 | |
2579 /** | |
2580 * Acts when a field instance is being purged. | |
2581 * | |
2582 * In field_purge_instance(), after the field instance has been | |
2583 * removed from the database, the field storage module has had a chance to | |
2584 * run its hook_field_storage_purge_instance(), and the field info cache | |
2585 * has been cleared, this hook is invoked on all modules to allow them to | |
2586 * respond to the field instance being purged. | |
2587 * | |
2588 * @param $instance | |
2589 * The instance being purged. | |
2590 */ | |
2591 function hook_field_purge_instance($instance) { | |
2592 db_delete('my_module_field_instance_info') | |
2593 ->condition('id', $instance['id']) | |
2594 ->execute(); | |
2595 } | |
2596 | |
2597 /** | |
2598 * Remove field storage information when a field record is purged. | |
2599 * | |
2600 * Called from field_purge_field() to allow the field storage module | |
2601 * to remove field information when a field is being purged. | |
2602 * | |
2603 * @param $field | |
2604 * The field being purged. | |
2605 */ | |
2606 function hook_field_storage_purge_field($field) { | |
2607 $table_name = _field_sql_storage_tablename($field); | |
2608 $revision_name = _field_sql_storage_revision_tablename($field); | |
2609 db_drop_table($table_name); | |
2610 db_drop_table($revision_name); | |
2611 } | |
2612 | |
2613 /** | |
2614 * Remove field storage information when a field instance is purged. | |
2615 * | |
2616 * Called from field_purge_instance() to allow the field storage module | |
2617 * to remove field instance information when a field instance is being | |
2618 * purged. | |
2619 * | |
2620 * @param $instance | |
2621 * The instance being purged. | |
2622 */ | |
2623 function hook_field_storage_purge_field_instance($instance) { | |
2624 db_delete('my_module_field_instance_info') | |
2625 ->condition('id', $instance['id']) | |
2626 ->execute(); | |
2627 } | |
2628 | |
2629 /** | |
2630 * Remove field storage information when field data is purged. | |
2631 * | |
2632 * Called from field_purge_data() to allow the field storage | |
2633 * module to delete field data information. | |
2634 * | |
2635 * @param $entity_type | |
2636 * The type of $entity; for example, 'node' or 'user'. | |
2637 * @param $entity | |
2638 * The pseudo-entity whose field data to delete. | |
2639 * @param $field | |
2640 * The (possibly deleted) field whose data is being purged. | |
2641 * @param $instance | |
2642 * The deleted field instance whose data is being purged. | |
2643 */ | |
2644 function hook_field_storage_purge($entity_type, $entity, $field, $instance) { | |
2645 list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity); | |
2646 | |
2647 $table_name = _field_sql_storage_tablename($field); | |
2648 $revision_name = _field_sql_storage_revision_tablename($field); | |
2649 db_delete($table_name) | |
2650 ->condition('entity_type', $entity_type) | |
2651 ->condition('entity_id', $id) | |
2652 ->execute(); | |
2653 db_delete($revision_name) | |
2654 ->condition('entity_type', $entity_type) | |
2655 ->condition('entity_id', $id) | |
2656 ->execute(); | |
2657 } | |
2658 | |
2659 /** | |
2660 * @} End of "addtogroup field_crud". | |
2661 */ | |
2662 | |
2663 /** | |
2664 * Determine whether the user has access to a given field. | |
2665 * | |
2666 * This hook is invoked from field_access() to let modules block access to | |
2667 * operations on fields. If no module returns FALSE, the operation is allowed. | |
2668 * | |
2669 * @param $op | |
2670 * The operation to be performed. Possible values: 'edit', 'view'. | |
2671 * @param $field | |
2672 * The field on which the operation is to be performed. | |
2673 * @param $entity_type | |
2674 * The type of $entity; for example, 'node' or 'user'. | |
2675 * @param $entity | |
2676 * (optional) The entity for the operation. | |
2677 * @param $account | |
2678 * (optional) The account to check; if not given use currently logged in user. | |
2679 * | |
2680 * @return | |
2681 * TRUE if the operation is allowed, and FALSE if the operation is denied. | |
2682 */ | |
2683 function hook_field_access($op, $field, $entity_type, $entity, $account) { | |
2684 if ($field['field_name'] == 'field_of_interest' && $op == 'edit') { | |
2685 return user_access('edit field of interest', $account); | |
2686 } | |
2687 return TRUE; | |
2688 } | |
2689 | |
2690 /** | |
2691 * @} End of "addtogroup hooks". | |
2692 */ |