5 * Field attach API, allowing entities (nodes, users, ...) to be 'fieldable'.
9 * Exception thrown by field_attach_validate() on field validation errors.
11 class FieldValidationException
extends FieldException
{
15 * Constructor for FieldValidationException.
18 * An array of field validation errors, keyed by field name and
19 * delta that contains two keys:
20 * - 'error': A machine-readable error code string, prefixed by
21 * the field module name. A field widget may use this code to decide
22 * how to report the error.
23 * - 'message': A human-readable error message such as to be
24 * passed to form_error() for the appropriate form element.
26 function __construct($errors) {
27 $this->errors
= $errors;
28 parent
::__construct(t('Field validation errors'));
33 * @defgroup field_storage Field Storage API
35 * Implement a storage engine for Field API data.
37 * The Field Attach API uses the Field Storage API to perform all "database
38 * access". Each Field Storage API hook function defines a primitive database
39 * operation such as read, write, or delete. The default field storage module,
40 * field_sql_storage.module, uses the local SQL database to implement these
41 * operations, but alternative field storage backends can choose to represent
42 * the data in SQL differently or use a completely different storage mechanism
43 * such as a cloud-based database.
45 * Each field defines which storage backend it uses. The Drupal system variable
46 * 'field_storage_default' identifies the storage backend used by default.
48 * See @link field Field API @endlink for information about the other parts of
53 * Argument for an update operation.
55 * This is used in hook_field_storage_write when updating an
58 define('FIELD_STORAGE_UPDATE', 'update');
61 * Argument for an insert operation.
63 * This is used in hook_field_storage_write when inserting a new entity.
65 define('FIELD_STORAGE_INSERT', 'insert');
68 * @} End of "defgroup field_storage".
72 * @defgroup field_attach Field Attach API
74 * Operate on Field API data attached to Drupal entities.
76 * Field Attach API functions load, store, display, generate Field API
77 * structures, and perform a variety of other functions for field data attached
78 * to individual entities.
80 * Field Attach API functions generally take $entity_type and $entity arguments
81 * along with additional function-specific arguments. $entity_type is the type
82 * of the fieldable entity, such as 'node' or 'user', and $entity is the entity
85 * hook_entity_info() is the central place for entity types to define if and
86 * how Field API should operate on their entity objects. Notably, the
87 * 'fieldable' property needs to be set to TRUE.
89 * The Field Attach API uses the concept of bundles: the set of fields for a
90 * given entity is defined on a per-bundle basis. The collection of bundles for
91 * an entity type is defined its hook_entity_info() implementation. For
92 * instance, node_entity_info() exposes each node type as its own bundle. This
93 * means that the set of fields of a node is determined by the node type. The
94 * Field API reads the bundle name for a given entity from a particular
95 * property of the entity object, and hook_entity_info() defines which property
96 * to use. For instance, node_entity_info() specifies:
97 * @code $info['entity keys']['bundle'] = 'type'@endcode
98 * This indicates that for a particular node object, the bundle name can be
99 * found in $node->type. This property can be omitted if the entity type only
100 * exposes a single bundle (all entities of this type have the same collection
101 * of fields). This is the case for the 'user' entity type.
103 * Most Field Attach API functions define a corresponding hook function that
104 * allows any module to act on Field Attach operations for any entity after the
105 * operation is complete, and access or modify all the field, form, or display
106 * data for that entity and operation. For example, field_attach_view() invokes
107 * hook_field_attach_view_alter(). These all-module hooks are distinct from
108 * those of the Field Types API, such as hook_field_load(), that are only
109 * invoked for the module that defines a specific field type.
111 * field_attach_load(), field_attach_insert(), and field_attach_update() also
112 * define pre-operation hooks, e.g. hook_field_attach_pre_load(). These hooks
113 * run before the corresponding Field Storage API and Field Type API
114 * operations. They allow modules to define additional storage locations (e.g.
115 * denormalizing, mirroring) for field data on a per-field basis. They also
116 * allow modules to take over field storage completely by instructing other
117 * implementations of the same hook and the Field Storage API itself not to
118 * operate on specified fields.
120 * The pre-operation hooks do not make the Field Storage API irrelevant. The
121 * Field Storage API is essentially the "fallback mechanism" for any fields
122 * that aren't being intercepted explicitly by pre-operation hooks.
124 * @link field_language Field Language API @endlink provides information about
125 * the structure of field objects.
127 * See @link field Field API @endlink for information about the other parts of
132 * Invoke a field hook.
135 * Possible operations include:
144 * - prepare translation
145 * @param $entity_type
146 * The type of $entity; e.g. 'node' or 'user'.
148 * The fully formed $entity_type entity.
150 * - The $form in the 'form' operation.
151 * - The value of $view_mode in the 'view' operation.
154 * - The $form_state in the 'submit' operation.
157 * An associative array of additional options, with the following keys:
158 * - 'field_name': The name of the field whose operation should be
159 * invoked. By default, the operation is invoked on all the fields
160 * in the entity's bundle. NOTE: This option is not compatible with
161 * the 'deleted' option; the 'field_id' option should be used
163 * - 'field_id': The id of the field whose operation should be
164 * invoked. By default, the operation is invoked on all the fields
165 * in the entity's' bundles.
166 * - 'default': A boolean value, specifying which implementation of
167 * the operation should be invoked.
168 * - if FALSE (default), the field types implementation of the operation
169 * will be invoked (hook_field_[op])
170 * - If TRUE, the default field implementation of the field operation
171 * will be invoked (field_default_[op])
172 * Internal use only. Do not explicitely set to TRUE, but use
173 * _field_invoke_default() instead.
174 * - 'deleted': If TRUE, the function will operate on deleted fields
175 * as well as non-deleted fields. If unset or FALSE, only
176 * non-deleted fields are operated on.
177 * - 'language': A language code or an array of language codes keyed by field
178 * name. It will be used to narrow down to a single value the available
179 * languages to act on.
181 function _field_invoke($op, $entity_type, $entity, &$a = NULL
, &$b = NULL
, $options = array()) {
182 // Merge default options.
183 $default_options = array(
188 $options += $default_options;
190 // Determine the list of instances to iterate on.
191 list(, , $bundle) = entity_extract_ids($entity_type, $entity);
192 $instances = _field_invoke_get_instances($entity_type, $bundle, $options);
194 // Iterate through the instances and collect results.
196 foreach ($instances as
$instance) {
197 // field_info_field() is not available for deleted fields, so use
198 // field_info_field_by_id().
199 $field = field_info_field_by_id($instance['field_id']);
200 $field_name = $field['field_name'];
201 $function = $options['default'] ?
'field_default_' .
$op : $field['module'] .
'_field_' .
$op;
202 if (function_exists($function)) {
203 // Determine the list of languages to iterate on.
204 $available_languages = field_available_languages($entity_type, $field);
205 $languages = _field_language_suggestion($available_languages, $options['language'], $field_name);
207 foreach ($languages as
$langcode) {
208 $items = isset($entity->{$field_name}[$langcode]) ?
$entity->{$field_name}[$langcode] : array();
209 $result = $function($entity_type, $entity, $field, $instance, $langcode, $items, $a, $b);
210 if (isset($result)) {
211 // For hooks with array results, we merge results together.
212 // For hooks with scalar results, we collect results in an array.
213 if (is_array($result)) {
214 $return = array_merge($return, $result);
221 // Populate $items back in the field values, but avoid replacing missing
222 // fields with an empty array (those are not equivalent on update).
223 if ($items !== array() || isset($entity->{$field_name}[$langcode])) {
224 $entity->{$field_name}[$langcode] = $items;
234 * Invoke a field hook across fields on multiple entities.
237 * Possible operations include:
240 * For all other operations, use _field_invoke() / field_invoke_default()
242 * @param $entity_type
243 * The type of $entity; e.g. 'node' or 'user'.
245 * An array of entities, keyed by entity id.
247 * - The $age parameter in the 'load' operation.
250 * Currently always NULL.
252 * An associative array of additional options, with the following keys:
253 * - 'field_name': The name of the field whose operation should be
254 * invoked. By default, the operation is invoked on all the fields
255 * in the entity's bundle. NOTE: This option is not compatible with
256 * the 'deleted' option; the 'field_id' option should be used instead.
257 * - 'field_id': The id of the field whose operation should be
258 * invoked. By default, the operation is invoked on all the fields
259 * in the entity's' bundles.
260 * - 'default': A boolean value, specifying which implementation of
261 * the operation should be invoked.
262 * - if FALSE (default), the field types implementation of the operation
263 * will be invoked (hook_field_[op])
264 * - If TRUE, the default field implementation of the field operation
265 * will be invoked (field_default_[op])
266 * Internal use only. Do not explicitely set to TRUE, but use
267 * _field_invoke_multiple_default() instead.
268 * - 'deleted': If TRUE, the function will operate on deleted fields
269 * as well as non-deleted fields. If unset or FALSE, only
270 * non-deleted fields are operated on.
271 * - 'language': A language code or an array of arrays of language codes keyed
272 * by entity id and field name. It will be used to narrow down to a single
273 * value the available languages to act on.
276 * An array of returned values keyed by entity id.
278 function _field_invoke_multiple($op, $entity_type, $entities, &$a = NULL
, &$b = NULL
, $options = array()) {
279 // Merge default options.
280 $default_options = array(
285 $options += $default_options;
288 $grouped_instances = array();
289 $grouped_entities = array();
290 $grouped_items = array();
293 // Go through the entities and collect the fields on which the hook should be
296 // We group fields by id, not by name, because this function can operate on
297 // deleted fields which may have non-unique names. However, entities can only
298 // contain data for a single field for each name, even if that field
299 // is deleted, so we reference field data via the
300 // $entity->$field_name property.
301 foreach ($entities as
$entity) {
302 // Determine the list of instances to iterate on.
303 list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
304 $instances = _field_invoke_get_instances($entity_type, $bundle, $options);
306 foreach ($instances as
$instance) {
307 $field_id = $instance['field_id'];
308 $field_name = $instance['field_name'];
309 $field = field_info_field_by_id($field_id);
310 $function = $options['default'] ?
'field_default_' .
$op : $field['module'] .
'_field_' .
$op;
311 if (function_exists($function)) {
312 // Add the field to the list of fields to invoke the hook on.
313 if (!isset($fields[$field_id])) {
314 $fields[$field_id] = $field;
316 // Extract the field values into a separate variable, easily accessed
317 // by hook implementations.
318 // Unless a language suggestion is provided we iterate on all the
319 // available languages.
320 $available_languages = field_available_languages($entity_type, $field);
321 $language = !empty($options['language'][$id]) ?
$options['language'][$id] : $options['language'];
322 $languages = _field_language_suggestion($available_languages, $language, $field_name);
323 foreach ($languages as
$langcode) {
324 $grouped_items[$field_id][$langcode][$id] = isset($entity->{$field_name}[$langcode]) ?
$entity->{$field_name}[$langcode] : array();
325 // Group the instances and entities corresponding to the current
327 $grouped_instances[$field_id][$langcode][$id] = $instance;
328 $grouped_entities[$field_id][$langcode][$id] = $entities[$id];
332 // Initialize the return value for each entity.
333 $return[$id] = array();
336 // For each field, invoke the field hook and collect results.
337 foreach ($fields as
$field_id => $field) {
338 $field_name = $field['field_name'];
339 $function = $options['default'] ?
'field_default_' .
$op : $field['module'] .
'_field_' .
$op;
340 // Iterate over all the field translations.
341 foreach ($grouped_items[$field_id] as
$langcode => &$items) {
342 $entities = $grouped_entities[$field_id][$langcode];
343 $instances = $grouped_instances[$field_id][$langcode];
344 $results = $function($entity_type, $entities, $field, $instances, $langcode, $items, $a, $b);
345 if (isset($results)) {
346 // Collect results by entity.
347 // For hooks with array results, we merge results together.
348 // For hooks with scalar results, we collect results in an array.
349 foreach ($results as
$id => $result) {
350 if (is_array($result)) {
351 $return[$id] = array_merge($return[$id], $result);
354 $return[$id][] = $result;
360 // Populate field values back in the entities, but avoid replacing missing
361 // fields with an empty array (those are not equivalent on update).
362 foreach ($grouped_entities[$field_id] as
$langcode => $entities) {
363 foreach ($entities as
$id => $entity) {
364 if ($grouped_items[$field_id][$langcode][$id] !== array() || isset($entity->{$field_name}[$langcode])) {
365 $entity->{$field_name}[$langcode] = $grouped_items[$field_id][$langcode][$id];
375 * Invoke field.module's version of a field hook.
377 * This function invokes the field_default_[op]() function.
378 * Use _field_invoke() to invoke the field type implementation,
381 * @see _field_invoke()
383 function _field_invoke_default($op, $entity_type, $entity, &$a = NULL
, &$b = NULL
, $options = array()) {
384 $options['default'] = TRUE
;
385 return _field_invoke($op, $entity_type, $entity, $a, $b, $options);
389 * Invoke field.module's version of a field hook on multiple entities.
391 * This function invokes the field_default_[op]() function.
392 * Use _field_invoke_multiple() to invoke the field type implementation,
395 * @see _field_invoke_multiple()
397 function _field_invoke_multiple_default($op, $entity_type, $entities, &$a = NULL
, &$b = NULL
, $options = array()) {
398 $options['default'] = TRUE
;
399 return _field_invoke_multiple($op, $entity_type, $entities, $a, $b, $options);
403 * Helper for _field_invoke(): retrieves a list of instances to operate on.
405 * @param $entity_type
410 * An associative array of options, as provided to _field_invoke(). Only the
411 * following keys are considered :
415 * See _field_invoke() for details.
418 * The array of selected instance definitions.
420 function _field_invoke_get_instances($entity_type, $bundle, $options) {
421 if ($options['deleted']) {
422 // Deleted fields are not included in field_info_instances(), and need to
423 // be fetched from the database with field_read_instances().
424 $params = array('entity_type' => $entity_type, 'bundle' => $bundle);
425 if (isset($options['field_id'])) {
426 // Single-field mode by field id: field_read_instances() does the filtering.
427 // Single-field mode by field name is not compatible with the 'deleted'
429 $params['field_id'] = $options['field_id'];
431 $instances = field_read_instances($params, array('include_deleted' => TRUE
));
433 elseif (isset($options['field_name'])) {
434 // Single-field mode by field name: field_info_instance() does the
436 $instances = array(field_info_instance($entity_type, $options['field_name'], $bundle));
439 $instances = field_info_instances($entity_type, $bundle);
440 if (isset($options['field_id'])) {
441 // Single-field mode by field id: we need to loop on each instance to
442 // find the right one.
443 foreach ($instances as
$instance) {
444 if ($instance['field_id'] == $options['field_id']) {
445 $instances = array($instance);
456 * Add form elements for all fields for an entity to a form structure.
458 * The form elements for the entity's fields are added by reference as direct
459 * children in the $form parameter. This parameter can be a full form structure
460 * (most common case for entity edit forms), or a sub-element of a larger form.
462 * By default, submitted field values appear at the top-level of
463 * $form_state['values']. A different location within $form_state['values'] can
464 * be specified by setting the '#parents' property on the incoming $form
465 * parameter. Because of name clashes, two instances of the same field cannot
466 * appear within the same $form element, or within the same '#parents' space.
468 * For each call to field_attach_form(), field values are processed by calling
469 * field_attach_form_validate() and field_attach_submit() on the same $form
472 * Sample resulting structure in $form:
474 * '#parents' => The location of field values in $form_state['values'],
475 * '#entity_type' => The name of the entity type,
476 * '#bundle' => The name of the bundle,
477 * // One sub-array per field appearing in the entity, keyed by field name.
478 * // The structure of the array differs slightly depending on whether the
479 * // widget is 'single-value' (provides the input for one field value,
480 * // most common case), and will therefore be repeated as many times as
481 * // needed, or 'multiple-values' (one single widget allows the input of
482 * // several values, e.g checkboxes, select box...).
483 * // The sub-array is nested into a $langcode key where $langcode has the
484 * // same value of the $langcode parameter above.
485 * // The '#language' key holds the same value of $langcode and it is used
486 * // to access the field sub-array when $langcode is unknown.
487 * 'field_foo' => array(
489 * '#field_name' => The name of the field,
490 * '#language' => $langcode,
491 * $langcode => array(
492 * '#field_name' => The name of the field,
493 * '#language' => $langcode,
494 * '#field_parents' => The 'parents' space for the field in the form,
495 * equal to the #parents property of the $form parameter received by
496 * field_attach_form(),
497 * '#required' => Whether or not the field is required,
498 * '#title' => The label of the field instance,
499 * '#description' => The description text for the field instance,
501 * // Only for 'single' widgets:
502 * '#theme' => 'field_multiple_value_form',
503 * '#cardinality' => The field cardinality,
504 * // One sub-array per copy of the widget, keyed by delta.
506 * '#entity_type' => The name of the entity type,
507 * '#bundle' => The name of the bundle,
508 * '#field_name' => The name of the field,
509 * '#field_parents' => The 'parents' space for the field in the form,
510 * equal to the #parents property of the $form parameter received by
511 * field_attach_form(),
512 * '#title' => The title to be displayed by the widget,
513 * '#default_value' => The field value for delta 0,
514 * '#required' => Whether the widget should be marked required,
516 * '#columns' => The array of field columns,
517 * // The remaining elements in the sub-array depend on the widget.
518 * '#type' => The type of the widget,
525 * // Only for multiple widgets:
526 * '#entity_type' => The name of the entity type,
527 * '#bundle' => $instance['bundle'],
528 * '#columns' => array_keys($field['columns']),
529 * // The remaining elements in the sub-array depend on the widget.
530 * '#type' => The type of the widget,
538 * Additionally, some processing data is placed in $form_state, and can be
539 * accessed by field_form_get_state() and field_form_set_state().
541 * @param $entity_type
542 * The type of $entity; e.g. 'node' or 'user'.
544 * The entity for which to load form elements, used to initialize
545 * default form values.
547 * The form structure to fill in. This can be a full form structure, or a
548 * sub-element of a larger form. The #parents property can be set to control
549 * the location of submitted field values within $form_state['values']. If
550 * not specified, $form['#parents'] is set to an empty array, placing field
551 * values at the top-level of $form_state['values'].
553 * An associative array containing the current state of the form.
555 * The language the field values are going to be entered, if no language
556 * is provided the default site language will be used.
557 * @param array $options
558 * An associative array of additional options. See _field_invoke() for
561 * @see field_form_get_state()
562 * @see field_form_set_state()
564 function field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode = NULL
, $options = array()) {
565 // Validate $options since this is a new parameter added after Drupal 7 was
567 $options = is_array($options) ?
$options : array();
569 // Set #parents to 'top-level' by default.
570 $form += array('#parents' => array());
572 // If no language is provided use the default site language.
573 $options['language'] = field_valid_language($langcode);
574 $form += (array) _field_invoke_default('form', $entity_type, $entity, $form, $form_state, $options);
576 // Add custom weight handling.
577 list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
578 $form['#pre_render'][] = '_field_extra_fields_pre_render';
579 $form['#entity_type'] = $entity_type;
580 $form['#bundle'] = $bundle;
582 // Let other modules make changes to the form.
583 // Avoid module_invoke_all() to let parameters be taken by reference.
584 foreach (module_implements('field_attach_form') as
$module) {
585 $function = $module .
'_field_attach_form';
586 $function($entity_type, $entity, $form, $form_state, $langcode);
591 * Loads fields for the current revisions of a group of entities.
593 * Loads all fields for each entity object in a group of a single entity type.
594 * The loaded field values are added directly to the entity objects.
596 * field_attach_load() is automatically called by the default entity controller
597 * class, and thus, in most cases, doesn't need to be explicitly called by the
598 * entity type module.
600 * @param $entity_type
601 * The type of $entity; e.g., 'node' or 'user'.
603 * An array of entities for which to load fields, keyed by entity ID.
604 * Each entity needs to have its 'bundle', 'id' and (if applicable)
605 * 'revision' keys filled in. The function adds the loaded field data
606 * directly in the entity objects of the $entities array.
608 * FIELD_LOAD_CURRENT to load the most recent revision for all
609 * fields, or FIELD_LOAD_REVISION to load the version indicated by
610 * each entity. Defaults to FIELD_LOAD_CURRENT; use
611 * field_attach_load_revision() instead of passing FIELD_LOAD_REVISION.
613 * An associative array of additional options, with the following keys:
614 * - 'field_id': The field ID that should be loaded, instead of
615 * loading all fields, for each entity. Note that returned entities
616 * may contain data for other fields, for example if they are read
618 * - 'deleted': If TRUE, the function will operate on deleted fields
619 * as well as non-deleted fields. If unset or FALSE, only
620 * non-deleted fields are operated on.
622 function field_attach_load($entity_type, $entities, $age = FIELD_LOAD_CURRENT
, $options = array()) {
623 $load_current = $age == FIELD_LOAD_CURRENT
;
625 // Merge default options.
626 $default_options = array(
629 $options += $default_options;
631 $info = entity_get_info($entity_type);
632 // Only the most current revision of non-deleted fields for cacheable entity
633 // types can be cached.
634 $cache_read = $load_current && $info['field cache'] && empty($options['deleted']);
635 // In addition, do not write to the cache when loading a single field.
636 $cache_write = $cache_read && !isset($options['field_id']);
638 if (empty($entities)) {
642 // Assume all entities will need to be queried. Entities found in the cache
643 // will be removed from the list.
644 $queried_entities = $entities;
646 // Fetch available entities from cache, if applicable.
648 // Build the list of cache entries to retrieve.
650 foreach ($entities as
$id => $entity) {
651 $cids[] = "field:$entity_type:$id";
653 $cache = cache_get_multiple($cids, 'cache_field');
654 // Put the cached field values back into the entities and remove them from
655 // the list of entities to query.
656 foreach ($entities as
$id => $entity) {
657 $cid = "field:$entity_type:$id";
658 if (isset($cache[$cid])) {
659 unset($queried_entities[$id]);
660 foreach ($cache[$cid]->data as
$field_name => $values) {
661 $entity->$field_name = $values;
667 // Fetch other entities from their storage location.
668 if ($queried_entities) {
669 // The invoke order is:
670 // - hook_field_storage_pre_load()
671 // - storage backend's hook_field_storage_load()
672 // - field-type module's hook_field_load()
673 // - hook_field_attach_load()
675 // Invoke hook_field_storage_pre_load(): let any module load field
676 // data before the storage engine, accumulating along the way.
677 $skip_fields = array();
678 foreach (module_implements('field_storage_pre_load') as
$module) {
679 $function = $module .
'_field_storage_pre_load';
680 $function($entity_type, $queried_entities, $age, $skip_fields, $options);
683 $instances = array();
685 // Collect the storage backends used by the remaining fields in the entities.
687 foreach ($queried_entities as
$entity) {
688 list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
689 $instances = _field_invoke_get_instances($entity_type, $bundle, $options);
691 foreach ($instances as
$instance) {
692 $field_name = $instance['field_name'];
693 $field_id = $instance['field_id'];
694 // Make sure all fields are present at least as empty arrays.
695 if (!isset($queried_entities[$id]->{$field_name})) {
696 $queried_entities[$id]->{$field_name} = array();
698 // Collect the storage backend if the field has not been loaded yet.
699 if (!isset($skip_fields[$field_id])) {
700 $field = field_info_field_by_id($field_id);
701 $storages[$field['storage']['type']][$field_id][] = $load_current ?
$id : $vid;
706 // Invoke hook_field_storage_load() on the relevant storage backends.
707 foreach ($storages as
$storage => $fields) {
708 $storage_info = field_info_storage_types($storage);
709 module_invoke($storage_info['module'], 'field_storage_load', $entity_type, $queried_entities, $age, $fields, $options);
712 // Invoke field-type module's hook_field_load().
714 _field_invoke_multiple('load', $entity_type, $queried_entities, $age, $null, $options);
716 // Invoke hook_field_attach_load(): let other modules act on loading the
718 module_invoke_all('field_attach_load', $entity_type, $queried_entities, $age, $options);
722 foreach ($queried_entities as
$id => $entity) {
724 list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
725 $instances = field_info_instances($entity_type, $bundle);
726 foreach ($instances as
$instance) {
727 $data[$instance['field_name']] = $queried_entities[$id]->{$instance['field_name']};
729 $cid = "field:$entity_type:$id";
730 cache_set($cid, $data, 'cache_field');
737 * Load all fields for previous versions of a group of entities.
739 * Loading different versions of the same entities is not supported, and should
740 * be done by separate calls to the function.
742 * field_attach_load_revision() is automatically called by the default entity
743 * controller class, and thus, in most cases, doesn't need to be explicitly
744 * called by the entity type module.
746 * @param $entity_type
747 * The type of $entity; e.g. 'node' or 'user'.
749 * An array of entities for which to load fields, keyed by entity ID. Each
750 * entity needs to have its 'bundle', 'id' and (if applicable) 'revision'
751 * keys filled. The function adds the loaded field data directly in the
752 * entity objects of the $entities array.
754 * An associative array of additional options. See field_attach_load() for
757 function field_attach_load_revision($entity_type, $entities, $options = array()) {
758 return field_attach_load($entity_type, $entities, FIELD_LOAD_REVISION
, $options);
762 * Perform field validation against the field data in an entity.
764 * This function does not perform field widget validation on form
765 * submissions. It is intended to be called during API save
766 * operations. Use field_attach_form_validate() to validate form
769 * @param $entity_type
770 * The type of $entity; e.g. 'node' or 'user'.
772 * The entity with fields to validate.
773 * @throws FieldValidationException
774 * If validation errors are found, a FieldValidationException is thrown. The
775 * 'errors' property contains the array of errors, keyed by field name,
776 * language and delta.
777 * @param array $options
778 * An associative array of additional options. See _field_invoke() for
781 function field_attach_validate($entity_type, $entity, $options = array()) {
782 // Validate $options since this is a new parameter added after Drupal 7 was
784 $options = is_array($options) ?
$options : array();
787 // Check generic, field-type-agnostic errors first.
789 _field_invoke_default('validate', $entity_type, $entity, $errors, $null, $options);
790 // Check field-type specific errors.
791 _field_invoke('validate', $entity_type, $entity, $errors, $null, $options);
793 // Let other modules validate the entity.
794 // Avoid module_invoke_all() to let $errors be taken by reference.
795 foreach (module_implements('field_attach_validate') as
$module) {
796 $function = $module .
'_field_attach_validate';
797 $function($entity_type, $entity, $errors);
801 throw new
FieldValidationException($errors);
806 * Perform field validation against form-submitted field values.
808 * There are two levels of validation for fields in forms: widget
809 * validation, and field validation.
810 * - Widget validation steps are specific to a given widget's own form
811 * structure and UI metaphors. They are executed through FAPI's
812 * #element_validate property during normal form validation.
813 * - Field validation steps are common to a given field type, independently of
814 * the specific widget being used in a given form. They are defined in the
815 * field type's implementation of hook_field_validate().
817 * This function performs field validation in the context of a form
818 * submission. It converts field validation errors into form errors
819 * on the correct form elements. Fieldable entity types should call
820 * this function during their own form validation function.
822 * @param $entity_type
823 * The type of $entity; e.g. 'node' or 'user'.
825 * The entity being submitted. The 'bundle', 'id' and (if applicable)
826 * 'revision' keys should be present. The actual field values will be read
827 * from $form_state['values'].
829 * The form structure where field elements are attached to. This might be a
830 * full form structure, or a sub-element of a larger form.
832 * An associative array containing the current state of the form.
833 * @param array $options
834 * An associative array of additional options. See _field_invoke() for
837 function field_attach_form_validate($entity_type, $entity, $form, &$form_state, $options = array()) {
838 // Validate $options since this is a new parameter added after Drupal 7 was
840 $options = is_array($options) ?
$options : array();
842 // Extract field values from submitted values.
843 _field_invoke_default('extract_form_values', $entity_type, $entity, $form, $form_state);
845 // Perform field_level validation.
847 field_attach_validate($entity_type, $entity, $options);
849 catch (FieldValidationException
$e) {
850 // Pass field-level validation errors back to widgets for accurate error
852 foreach ($e->errors as
$field_name => $field_errors) {
853 foreach ($field_errors as
$langcode => $errors) {
854 $field_state = field_form_get_state($form['#parents'], $field_name, $langcode, $form_state);
855 $field_state['errors'] = $errors;
856 field_form_set_state($form['#parents'], $field_name, $langcode, $form_state, $field_state);
859 _field_invoke_default('form_errors', $entity_type, $entity, $form, $form_state, $options);
864 * Perform necessary operations on field data submitted by a form.
866 * Currently, this accounts for drag-and-drop reordering of
867 * field values, and filtering of empty values.
869 * @param $entity_type
870 * The type of $entity; e.g. 'node' or 'user'.
872 * The entity being submitted. The 'bundle', 'id' and (if applicable)
873 * 'revision' keys should be present. The actual field values will be read
874 * from $form_state['values'].
876 * The form structure where field elements are attached to. This might be a
877 * full form structure, or a sub-element of a larger form.
879 * An associative array containing the current state of the form.
880 * @param array $options
881 * An associative array of additional options. See _field_invoke() for
884 function field_attach_submit($entity_type, $entity, $form, &$form_state, $options = array()) {
885 // Validate $options since this is a new parameter added after Drupal 7 was
887 $options = is_array($options) ?
$options : array();
889 // Extract field values from submitted values.
890 _field_invoke_default('extract_form_values', $entity_type, $entity, $form, $form_state, $options);
892 _field_invoke_default('submit', $entity_type, $entity, $form, $form_state, $options);
894 // Let other modules act on submitting the entity.
895 // Avoid module_invoke_all() to let $form_state be taken by reference.
896 foreach (module_implements('field_attach_submit') as
$module) {
897 $function = $module .
'_field_attach_submit';
898 $function($entity_type, $entity, $form, $form_state);
903 * Perform necessary operations just before fields data get saved.
905 * We take no specific action here, we just give other
906 * modules the opportunity to act.
908 * @param $entity_type
909 * The type of $entity; e.g. 'node' or 'user'.
911 * The entity with fields to process.
913 function field_attach_presave($entity_type, $entity) {
914 _field_invoke('presave', $entity_type, $entity);
916 // Let other modules act on presaving the entity.
917 module_invoke_all('field_attach_presave', $entity_type, $entity);
921 * Save field data for a new entity.
923 * The passed-in entity must already contain its id and (if applicable)
924 * revision id attributes.
925 * Default values (if any) will be saved for fields not present in the
928 * @param $entity_type
929 * The type of $entity; e.g. 'node' or 'user'.
931 * The entity with fields to save.
933 * Default values (if any) will be added to the $entity parameter for fields
934 * it leaves unspecified.
936 function field_attach_insert($entity_type, $entity) {
937 _field_invoke_default('insert', $entity_type, $entity);
938 _field_invoke('insert', $entity_type, $entity);
940 list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
942 // Let any module insert field data before the storage engine, accumulating
943 // saved fields along the way.
944 $skip_fields = array();
945 foreach (module_implements('field_storage_pre_insert') as
$module) {
946 $function = $module .
'_field_storage_pre_insert';
947 $function($entity_type, $entity, $skip_fields);
950 // Collect the storage backends used by the remaining fields in the entities.
952 foreach (field_info_instances($entity_type, $bundle) as
$instance) {
953 $field = field_info_field_by_id($instance['field_id']);
954 $field_id = $field['id'];
955 $field_name = $field['field_name'];
956 if (!empty($entity->$field_name)) {
957 // Collect the storage backend if the field has not been written yet.
958 if (!isset($skip_fields[$field_id])) {
959 $storages[$field['storage']['type']][$field_id] = $field_id;
964 // Field storage backends save any remaining unsaved fields.
965 foreach ($storages as
$storage => $fields) {
966 $storage_info = field_info_storage_types($storage);
967 module_invoke($storage_info['module'], 'field_storage_write', $entity_type, $entity, FIELD_STORAGE_INSERT
, $fields);
970 // Let other modules act on inserting the entity.
971 module_invoke_all('field_attach_insert', $entity_type, $entity);
973 $entity_info = entity_get_info($entity_type);
977 * Save field data for an existing entity.
979 * @param $entity_type
980 * The type of $entity; e.g. 'node' or 'user'.
982 * The entity with fields to save.
984 function field_attach_update($entity_type, $entity) {
985 _field_invoke('update', $entity_type, $entity);
987 list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
989 // Let any module update field data before the storage engine, accumulating
990 // saved fields along the way.
991 $skip_fields = array();
992 foreach (module_implements('field_storage_pre_update') as
$module) {
993 $function = $module .
'_field_storage_pre_update';
994 $function($entity_type, $entity, $skip_fields);
997 // Collect the storage backends used by the remaining fields in the entities.
999 foreach (field_info_instances($entity_type, $bundle) as
$instance) {
1000 $field = field_info_field_by_id($instance['field_id']);
1001 $field_id = $field['id'];
1002 $field_name = $field['field_name'];
1003 // Leave the field untouched if $entity comes with no $field_name property,
1004 // but empty the field if it comes as a NULL value or an empty array.
1005 // Function property_exists() is slower, so we catch the more frequent
1006 // cases where it's an empty array with the faster isset().
1007 if (isset($entity->$field_name) || property_exists($entity, $field_name)) {
1008 // Collect the storage backend if the field has not been written yet.
1009 if (!isset($skip_fields[$field_id])) {
1010 $storages[$field['storage']['type']][$field_id] = $field_id;
1015 // Field storage backends save any remaining unsaved fields.
1016 foreach ($storages as
$storage => $fields) {
1017 $storage_info = field_info_storage_types($storage);
1018 module_invoke($storage_info['module'], 'field_storage_write', $entity_type, $entity, FIELD_STORAGE_UPDATE
, $fields);
1021 // Let other modules act on updating the entity.
1022 module_invoke_all('field_attach_update', $entity_type, $entity);
1024 $entity_info = entity_get_info($entity_type);
1025 if ($entity_info['field cache']) {
1026 cache_clear_all("field:$entity_type:$id", 'cache_field');
1031 * Delete field data for an existing entity. This deletes all
1032 * revisions of field data for the entity.
1034 * @param $entity_type
1035 * The type of $entity; e.g. 'node' or 'user'.
1037 * The entity whose field data to delete.
1039 function field_attach_delete($entity_type, $entity) {
1040 _field_invoke('delete', $entity_type, $entity);
1042 list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
1044 // Collect the storage backends used by the fields in the entities.
1045 $storages = array();
1046 foreach (field_info_instances($entity_type, $bundle) as
$instance) {
1047 $field = field_info_field_by_id($instance['field_id']);
1048 $field_id = $field['id'];
1049 $storages[$field['storage']['type']][$field_id] = $field_id;
1052 // Field storage backends delete their data.
1053 foreach ($storages as
$storage => $fields) {
1054 $storage_info = field_info_storage_types($storage);
1055 module_invoke($storage_info['module'], 'field_storage_delete', $entity_type, $entity, $fields);
1058 // Let other modules act on deleting the entity.
1059 module_invoke_all('field_attach_delete', $entity_type, $entity);
1061 $entity_info = entity_get_info($entity_type);
1062 if ($entity_info['field cache']) {
1063 cache_clear_all("field:$entity_type:$id", 'cache_field');
1068 * Delete field data for a single revision of an existing entity. The
1069 * passed entity must have a revision id attribute.
1071 * @param $entity_type
1072 * The type of $entity; e.g. 'node' or 'user'.
1074 * The entity with fields to save.
1076 function field_attach_delete_revision($entity_type, $entity) {
1077 _field_invoke('delete_revision', $entity_type, $entity);
1079 list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
1081 // Collect the storage backends used by the fields in the entities.
1082 $storages = array();
1083 foreach (field_info_instances($entity_type, $bundle) as
$instance) {
1084 $field = field_info_field_by_id($instance['field_id']);
1085 $field_id = $field['id'];
1086 $storages[$field['storage']['type']][$field_id] = $field_id;
1089 // Field storage backends delete their data.
1090 foreach ($storages as
$storage => $fields) {
1091 $storage_info = field_info_storage_types($storage);
1092 module_invoke($storage_info['module'], 'field_storage_delete_revision', $entity_type, $entity, $fields);
1095 // Let other modules act on deleting the revision.
1096 module_invoke_all('field_attach_delete_revision', $entity_type, $entity);
1100 * Prepare field data prior to display.
1102 * This function lets field types and formatters load additional data
1103 * needed for display that is not automatically loaded during
1104 * field_attach_load(). It accepts an array of entities to allow query
1105 * optimisation when displaying lists of entities.
1107 * field_attach_prepare_view() and field_attach_view() are two halves
1108 * of the same operation. It is safe to call
1109 * field_attach_prepare_view() multiple times on the same entity
1110 * before calling field_attach_view() on it, but calling any Field
1111 * API operation on an entity between passing that entity to these two
1112 * functions may yield incorrect results.
1114 * @param $entity_type
1115 * The type of $entities; e.g. 'node' or 'user'.
1117 * An array of entities, keyed by entity id.
1119 * View mode, e.g. 'full', 'teaser'...
1121 * (Optional) The language the field values are to be shown in. If no language
1122 * is provided the current language is used.
1123 * @param array $options
1124 * An associative array of additional options. See _field_invoke() for
1127 function field_attach_prepare_view($entity_type, $entities, $view_mode, $langcode = NULL
, $options = array()) {
1128 // Validate $options since this is a new parameter added after Drupal 7 was
1130 $options = is_array($options) ?
$options : array();
1132 $options['language'] = array();
1134 // To ensure hooks are only run once per entity, only process items without
1135 // the _field_view_prepared flag.
1136 // @todo: resolve this more generally for both entity and field level hooks.
1138 foreach ($entities as
$id => $entity) {
1139 if (empty($entity->_field_view_prepared
)) {
1140 // Add this entity to the items to be prepared.
1141 $prepare[$id] = $entity;
1143 // Determine the actual language to display for each field, given the
1144 // languages available in the field data.
1145 $options['language'][$id] = field_language($entity_type, $entity, NULL
, $langcode);
1147 // Mark this item as prepared.
1148 $entity->_field_view_prepared
= TRUE
;
1153 // First let the field types do their preparation.
1154 _field_invoke_multiple('prepare_view', $entity_type, $prepare, $null, $null, $options);
1155 // Then let the formatters do their own specific massaging.
1156 // field_default_prepare_view() takes care of dispatching to the correct
1157 // formatters according to the display settings for the view mode.
1158 _field_invoke_multiple_default('prepare_view', $entity_type, $prepare, $view_mode, $null, $options);
1162 * Returns a renderable array for the fields on an entity.
1164 * Each field is displayed according to the display options specified in the
1165 * $instance definition for the given $view_mode.
1167 * field_attach_prepare_view() and field_attach_view() are two halves
1168 * of the same operation. It is safe to call
1169 * field_attach_prepare_view() multiple times on the same entity
1170 * before calling field_attach_view() on it, but calling any Field
1171 * API operation on an entity between passing that entity to these two
1172 * functions may yield incorrect results.
1177 * 'field_foo' => array(
1178 * '#theme' => 'field',
1179 * '#title' => the label of the field instance,
1180 * '#label_display' => the label display mode,
1181 * '#object' => the fieldable entity being displayed,
1182 * '#entity_type' => the type of the entity being displayed,
1183 * '#language' => the language of the field values being displayed,
1184 * '#view_mode' => the view mode,
1185 * '#field_name' => the name of the field,
1186 * '#field_type' => the type of the field,
1187 * '#formatter' => the name of the formatter,
1188 * '#items' => the field values being displayed,
1189 * // The element's children are the formatted values returned by
1190 * // hook_field_formatter_view().
1195 * @param $entity_type
1196 * The type of $entity; e.g. 'node' or 'user'.
1198 * The entity with fields to render.
1200 * View mode, e.g. 'full', 'teaser'...
1202 * The language the field values are to be shown in. If no language is
1203 * provided the current language is used.
1204 * @param array $options
1205 * An associative array of additional options. See _field_invoke() for
1208 * A renderable array for the field values.
1210 function field_attach_view($entity_type, $entity, $view_mode, $langcode = NULL
, $options = array()) {
1211 // Validate $options since this is a new parameter added after Drupal 7 was
1213 $options = is_array($options) ?
$options : array();
1215 // Determine the actual language to display for each field, given the
1216 // languages available in the field data.
1217 $display_language = field_language($entity_type, $entity, NULL
, $langcode);
1218 $options['language'] = $display_language;
1220 // Invoke field_default_view().
1222 $output = _field_invoke_default('view', $entity_type, $entity, $view_mode, $null, $options);
1224 // Add custom weight handling.
1225 list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
1226 $output['#pre_render'][] = '_field_extra_fields_pre_render';
1227 $output['#entity_type'] = $entity_type;
1228 $output['#bundle'] = $bundle;
1230 // Let other modules alter the renderable array.
1232 'entity_type' => $entity_type,
1233 'entity' => $entity,
1234 'view_mode' => $view_mode,
1235 'display' => $view_mode,
1236 'language' => $langcode,
1238 drupal_alter('field_attach_view', $output, $context);
1240 // Reset the _field_view_prepared flag set in field_attach_prepare_view(),
1241 // in case the same entity is displayed with different settings later in
1243 unset($entity->_field_view_prepared
);
1249 * Populate the template variables with the field values available for rendering.
1251 * The $variables array will be populated with all the field instance values
1252 * associated with the given entity type, keyed by field name; in case of
1253 * translatable fields the language currently chosen for display will be
1256 * @param $entity_type
1257 * The type of $entity; e.g. 'node' or 'user'.
1259 * The entity with fields to render.
1261 * The structured array containing the values ready for rendering.
1263 * The variables array is passed by reference and will be populated with field
1266 function field_attach_preprocess($entity_type, $entity, $element, &$variables) {
1267 list(, , $bundle) = entity_extract_ids($entity_type, $entity);
1269 foreach (field_info_instances($entity_type, $bundle) as
$instance) {
1270 $field_name = $instance['field_name'];
1271 if (isset($element[$field_name]['#language'])) {
1272 $langcode = $element[$field_name]['#language'];
1273 $variables[$field_name] = isset($entity->{$field_name}[$langcode]) ?
$entity->{$field_name}[$langcode] : NULL
;
1277 // Let other modules make changes to the $variables array.
1279 'entity_type' => $entity_type,
1280 'entity' => $entity,
1281 'element' => $element,
1283 drupal_alter('field_attach_preprocess', $variables, $context);
1287 * Prepares an entity for translation.
1289 * This function is used to fill-in the form default values for Field API fields
1290 * while performing entity translation. By default it copies all the source
1291 * values in the given source language to the new entity and assigns them the
1294 * This is used as part of the 'per entity' translation pattern, which is
1295 * implemented only for nodes by translation.module. Other entity types may be
1296 * supported through contributed modules.
1298 * @param $entity_type
1299 * The type of $entity; e.g. 'node' or 'user'.
1301 * The entity to be prepared for translation.
1303 * The language the entity has to be translated in.
1304 * @param $source_entity
1305 * The source entity holding the field values to be translated.
1306 * @param $source_langcode
1307 * The source language from which translate.
1309 function field_attach_prepare_translation($entity_type, $entity, $langcode, $source_entity, $source_langcode) {
1310 $options = array('language' => $langcode);
1311 // Copy source field values into the entity to be prepared.
1312 _field_invoke_default('prepare_translation', $entity_type, $entity, $source_entity, $source_langcode, $options);
1313 // Let field types handle their own advanced translation pattern if needed.
1314 _field_invoke('prepare_translation', $entity_type, $entity, $source_entity, $source_langcode, $options);
1315 // Let other modules alter the entity translation.
1317 'entity_type' => $entity_type,
1318 'langcode' => $langcode,
1319 'source_entity' => $source_entity,
1320 'source_langcode' => $source_langcode,
1322 drupal_alter('field_attach_prepare_translation', $entity, $context);
1326 * Notify field.module that a new bundle was created.
1328 * The default SQL-based storage doesn't need to do anything about it, but
1331 * @param $entity_type
1332 * The entity type to which the bundle is bound.
1334 * The name of the newly created bundle.
1336 function field_attach_create_bundle($entity_type, $bundle) {
1338 field_cache_clear();
1340 // Let other modules act on creating the bundle.
1341 module_invoke_all('field_attach_create_bundle', $entity_type, $bundle);
1345 * Notify field.module that a bundle was renamed.
1347 * @param $entity_type
1348 * The entity type to which the bundle is bound.
1349 * @param $bundle_old
1350 * The previous name of the bundle.
1351 * @param $bundle_new
1352 * The new name of the bundle.
1354 function field_attach_rename_bundle($entity_type, $bundle_old, $bundle_new) {
1355 db_update('field_config_instance')
1356 ->fields(array('bundle' => $bundle_new))
1357 ->condition('entity_type', $entity_type)
1358 ->condition('bundle', $bundle_old)
1362 field_cache_clear();
1364 // Update bundle settings.
1365 $settings = variable_get('field_bundle_settings_' .
$entity_type .
'__' .
$bundle_old, array());
1366 variable_set('field_bundle_settings_' .
$entity_type .
'__' .
$bundle_new, $settings);
1367 variable_del('field_bundle_settings_' .
$entity_type .
'__' .
$bundle_old);
1369 // Let other modules act on renaming the bundle.
1370 module_invoke_all('field_attach_rename_bundle', $entity_type, $bundle_old, $bundle_new);
1374 * Notify field.module the a bundle was deleted.
1376 * This deletes the data for the field instances as well as the field instances
1377 * themselves. This function actually just marks the data and field instances
1378 * and deleted, leaving the garbage collection for a separate process, because
1379 * it is not always possible to delete this much data in a single page request
1380 * (particularly since for some field types, the deletion is more than just a
1381 * simple DELETE query).
1383 * @param $entity_type
1384 * The entity type to which the bundle is bound.
1386 * The bundle to delete.
1388 function field_attach_delete_bundle($entity_type, $bundle) {
1389 // First, delete the instances themselves. field_read_instances() must be
1390 // used here since field_info_instances() does not return instances for
1391 // disabled entity types or bundles.
1392 $instances = field_read_instances(array('entity_type' => $entity_type, 'bundle' => $bundle), array('include_inactive' => 1));
1393 foreach ($instances as
$instance) {
1394 field_delete_instance($instance);
1398 field_cache_clear();
1400 // Clear bundle display settings.
1401 variable_del('field_bundle_settings_' .
$entity_type .
'__' .
$bundle);
1403 // Let other modules act on deleting the bundle.
1404 module_invoke_all('field_attach_delete_bundle', $entity_type, $bundle, $instances);
1409 * @} End of "defgroup field_attach".