2009-02-05 03:42:58 +00:00
|
|
|
<?php
|
|
|
|
|
2009-02-10 03:16:15 +00:00
|
|
|
/**
|
|
|
|
* @file
|
2010-02-12 05:38:10 +00:00
|
|
|
* Field attach API, allowing entities (nodes, users, ...) to be 'fieldable'.
|
2009-02-10 03:16:15 +00:00
|
|
|
*/
|
|
|
|
|
2009-03-26 13:31:28 +00:00
|
|
|
/**
|
2009-06-06 16:17:30 +00:00
|
|
|
* Exception thrown by field_attach_validate() on field validation errors.
|
2009-03-26 13:31:28 +00:00
|
|
|
*/
|
|
|
|
class FieldValidationException extends FieldException {
|
|
|
|
var $errors;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor for FieldValidationException.
|
|
|
|
*
|
|
|
|
* @param $errors
|
|
|
|
* An array of field validation errors, keyed by field name and
|
|
|
|
* delta that contains two keys:
|
|
|
|
* - 'error': A machine-readable error code string, prefixed by
|
2009-07-15 17:55:18 +00:00
|
|
|
* the field module name. A field widget may use this code to decide
|
2009-03-26 13:31:28 +00:00
|
|
|
* how to report the error.
|
|
|
|
* - 'message': A human-readable error message such as to be
|
|
|
|
* passed to form_error() for the appropriate form element.
|
|
|
|
*/
|
|
|
|
function __construct($errors) {
|
|
|
|
$this->errors = $errors;
|
|
|
|
parent::__construct(t('Field validation errors'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-05 03:42:58 +00:00
|
|
|
/**
|
|
|
|
* @defgroup field_storage Field Storage API
|
|
|
|
* @{
|
|
|
|
* Implement a storage engine for Field API data.
|
|
|
|
*
|
- Patch #443422 by yched, bjaspan | chx, merlinofchaos, Scott Reynolds, plach, profix898, mattyoung: added support for pluggable 'per field' storage engine. Comes with documentation and tests.
The Field Attach API uses the Field Storage API to perform all "database access". Each Field Storage API hook function defines a primitive database operation such as read, write, or delete. The default field storage module, field_sql_storage.module, uses the local SQL database to implement these operations, but alternative field storage backends can choose to represent the data in SQL differently or use a completely different storage mechanism such as a cloud-based database.
2009-09-27 12:52:55 +00:00
|
|
|
* The Field Attach API uses the Field Storage API to perform all "database
|
|
|
|
* access". Each Field Storage API hook function defines a primitive database
|
|
|
|
* operation such as read, write, or delete. The default field storage module,
|
|
|
|
* field_sql_storage.module, uses the local SQL database to implement these
|
|
|
|
* operations, but alternative field storage backends can choose to represent
|
|
|
|
* the data in SQL differently or use a completely different storage mechanism
|
|
|
|
* such as a cloud-based database.
|
2009-02-05 03:42:58 +00:00
|
|
|
*
|
- Patch #443422 by yched, bjaspan | chx, merlinofchaos, Scott Reynolds, plach, profix898, mattyoung: added support for pluggable 'per field' storage engine. Comes with documentation and tests.
The Field Attach API uses the Field Storage API to perform all "database access". Each Field Storage API hook function defines a primitive database operation such as read, write, or delete. The default field storage module, field_sql_storage.module, uses the local SQL database to implement these operations, but alternative field storage backends can choose to represent the data in SQL differently or use a completely different storage mechanism such as a cloud-based database.
2009-09-27 12:52:55 +00:00
|
|
|
* Each field defines which storage backend it uses. The Drupal system variable
|
2011-06-27 06:19:01 +00:00
|
|
|
* 'field_storage_default' identifies the storage backend used by default.
|
2011-11-30 02:42:42 +00:00
|
|
|
*
|
|
|
|
* @see field
|
2009-02-05 03:42:58 +00:00
|
|
|
*/
|
2009-03-13 21:25:40 +00:00
|
|
|
|
|
|
|
/**
|
2009-08-11 04:46:29 +00:00
|
|
|
* Argument for an update operation.
|
|
|
|
*
|
2009-04-13 05:18:18 +00:00
|
|
|
* This is used in hook_field_storage_write when updating an
|
2010-02-12 05:38:10 +00:00
|
|
|
* existing entity.
|
2009-03-13 21:25:40 +00:00
|
|
|
*/
|
2011-11-29 09:56:53 +00:00
|
|
|
const FIELD_STORAGE_UPDATE = 'update';
|
2009-03-13 21:25:40 +00:00
|
|
|
|
|
|
|
/**
|
2009-08-11 04:46:29 +00:00
|
|
|
* Argument for an insert operation.
|
|
|
|
*
|
2010-02-12 05:38:10 +00:00
|
|
|
* This is used in hook_field_storage_write when inserting a new entity.
|
2009-03-13 21:25:40 +00:00
|
|
|
*/
|
2011-11-29 09:56:53 +00:00
|
|
|
const FIELD_STORAGE_INSERT = 'insert';
|
2009-03-13 21:25:40 +00:00
|
|
|
|
2009-02-05 03:42:58 +00:00
|
|
|
/**
|
|
|
|
* @} End of "defgroup field_storage"
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @defgroup field_attach Field Attach API
|
|
|
|
* @{
|
2010-02-12 05:38:10 +00:00
|
|
|
* Operate on Field API data attached to Drupal entities.
|
2009-02-05 03:42:58 +00:00
|
|
|
*
|
2010-04-04 12:48:18 +00:00
|
|
|
* Field Attach API functions load, store, display, generate Form API
|
|
|
|
* structures, and perform a variety of other functions for field data attached
|
|
|
|
* to individual entities.
|
|
|
|
*
|
|
|
|
* Field Attach API functions generally take $entity_type and $entity arguments
|
|
|
|
* along with additional function-specific arguments. $entity_type is the type
|
|
|
|
* of the fieldable entity, such as 'node' or 'user', and $entity is the entity
|
|
|
|
* itself.
|
|
|
|
*
|
|
|
|
* hook_entity_info() is the central place for entity types to define if and
|
|
|
|
* how Field API should operate on their entity objects. Notably, the
|
|
|
|
* 'fieldable' property needs to be set to TRUE.
|
|
|
|
*
|
|
|
|
* The Field Attach API uses the concept of bundles: the set of fields for a
|
|
|
|
* given entity is defined on a per-bundle basis. The collection of bundles for
|
|
|
|
* an entity type is defined its hook_entity_info() implementation. For
|
|
|
|
* instance, node_entity_info() exposes each node type as its own bundle. This
|
|
|
|
* means that the set of fields of a node is determined by the node type. The
|
|
|
|
* Field API reads the bundle name for a given entity from a particular
|
|
|
|
* property of the entity object, and hook_entity_info() defines which property
|
|
|
|
* to use. For instance, node_entity_info() specifies:
|
|
|
|
* @code $info['entity keys']['bundle'] = 'type'@endcode
|
|
|
|
* This indicates that for a particular node object, the bundle name can be
|
|
|
|
* found in $node->type. This property can be omitted if the entity type only
|
|
|
|
* exposes a single bundle (all entities of this type have the same collection
|
|
|
|
* of fields). This is the case for the 'user' entity type.
|
|
|
|
*
|
|
|
|
* Most Field Attach API functions define a corresponding hook function that
|
|
|
|
* allows any module to act on Field Attach operations for any entity after the
|
|
|
|
* operation is complete, and access or modify all the field, form, or display
|
|
|
|
* data for that entity and operation. For example, field_attach_view() invokes
|
2009-05-17 00:32:29 +00:00
|
|
|
* hook_field_attach_view_alter(). These all-module hooks are distinct from
|
2010-04-04 12:48:18 +00:00
|
|
|
* those of the Field Types API, such as hook_field_load(), that are only
|
|
|
|
* invoked for the module that defines a specific field type.
|
|
|
|
*
|
|
|
|
* field_attach_load(), field_attach_insert(), and field_attach_update() also
|
|
|
|
* define pre-operation hooks, e.g. hook_field_attach_pre_load(). These hooks
|
|
|
|
* run before the corresponding Field Storage API and Field Type API
|
|
|
|
* operations. They allow modules to define additional storage locations (e.g.
|
|
|
|
* denormalizing, mirroring) for field data on a per-field basis. They also
|
|
|
|
* allow modules to take over field storage completely by instructing other
|
|
|
|
* implementations of the same hook and the Field Storage API itself not to
|
|
|
|
* operate on specified fields.
|
|
|
|
*
|
|
|
|
* The pre-operation hooks do not make the Field Storage API irrelevant. The
|
|
|
|
* Field Storage API is essentially the "fallback mechanism" for any fields
|
|
|
|
* that aren't being intercepted explicitly by pre-operation hooks.
|
2011-11-30 02:42:42 +00:00
|
|
|
*
|
2011-12-21 03:30:24 +00:00
|
|
|
* @link field_language Field language API @endlink provides information about
|
|
|
|
* the structure of field objects.
|
|
|
|
*
|
2011-11-30 02:42:42 +00:00
|
|
|
* @see field
|
2009-02-05 03:42:58 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoke a field hook.
|
|
|
|
*
|
|
|
|
* @param $op
|
2009-05-17 00:32:29 +00:00
|
|
|
* Possible operations include:
|
2009-02-05 03:42:58 +00:00
|
|
|
* - form
|
|
|
|
* - validate
|
|
|
|
* - presave
|
|
|
|
* - insert
|
|
|
|
* - update
|
|
|
|
* - delete
|
|
|
|
* - delete revision
|
|
|
|
* - view
|
|
|
|
* - prepare translation
|
2010-02-11 17:44:47 +00:00
|
|
|
* @param $entity_type
|
|
|
|
* The type of $entity; e.g. 'node' or 'user'.
|
|
|
|
* @param $entity
|
2010-02-12 05:38:10 +00:00
|
|
|
* The fully formed $entity_type entity.
|
2009-02-05 03:42:58 +00:00
|
|
|
* @param $a
|
2009-05-17 00:32:29 +00:00
|
|
|
* - The $form in the 'form' operation.
|
2009-12-26 16:50:09 +00:00
|
|
|
* - The value of $view_mode in the 'view' operation.
|
2009-05-17 00:32:29 +00:00
|
|
|
* - Otherwise NULL.
|
2009-02-05 03:42:58 +00:00
|
|
|
* @param $b
|
2009-05-17 00:32:29 +00:00
|
|
|
* - The $form_state in the 'submit' operation.
|
|
|
|
* - Otherwise NULL.
|
2009-06-06 16:17:30 +00:00
|
|
|
* @param $options
|
|
|
|
* An associative array of additional options, with the following keys:
|
2009-08-11 14:59:40 +00:00
|
|
|
* - 'field_name': The name of the field whose operation should be
|
|
|
|
* invoked. By default, the operation is invoked on all the fields
|
2010-02-12 05:38:10 +00:00
|
|
|
* in the entity's bundle. NOTE: This option is not compatible with
|
2009-08-11 14:59:40 +00:00
|
|
|
* the 'deleted' option; the 'field_id' option should be used
|
2009-08-13 00:17:47 +00:00
|
|
|
* instead.
|
2009-08-11 14:59:40 +00:00
|
|
|
* - 'field_id': The id of the field whose operation should be
|
|
|
|
* invoked. By default, the operation is invoked on all the fields
|
2010-02-12 05:38:10 +00:00
|
|
|
* in the entity's' bundles.
|
2009-08-11 14:59:40 +00:00
|
|
|
* - 'default': A boolean value, specifying which implementation of
|
|
|
|
* the operation should be invoked.
|
2009-06-06 16:17:30 +00:00
|
|
|
* - if FALSE (default), the field types implementation of the operation
|
|
|
|
* will be invoked (hook_field_[op])
|
|
|
|
* - If TRUE, the default field implementation of the field operation
|
|
|
|
* will be invoked (field_default_[op])
|
|
|
|
* Internal use only. Do not explicitely set to TRUE, but use
|
|
|
|
* _field_invoke_default() instead.
|
2009-08-11 14:59:40 +00:00
|
|
|
* - 'deleted': If TRUE, the function will operate on deleted fields
|
|
|
|
* as well as non-deleted fields. If unset or FALSE, only
|
|
|
|
* non-deleted fields are operated on.
|
2010-03-25 11:46:21 +00:00
|
|
|
* - 'language': A language code or an array of language codes keyed by field
|
|
|
|
* name. It will be used to narrow down to a single value the available
|
|
|
|
* languages to act on.
|
2009-02-05 03:42:58 +00:00
|
|
|
*/
|
2010-02-11 17:44:47 +00:00
|
|
|
function _field_invoke($op, $entity_type, $entity, &$a = NULL, &$b = NULL, $options = array()) {
|
2009-06-06 16:17:30 +00:00
|
|
|
// Merge default options.
|
|
|
|
$default_options = array(
|
|
|
|
'default' => FALSE,
|
2009-08-11 14:59:40 +00:00
|
|
|
'deleted' => FALSE,
|
2009-08-22 00:58:55 +00:00
|
|
|
'language' => NULL,
|
2009-06-06 16:17:30 +00:00
|
|
|
);
|
|
|
|
$options += $default_options;
|
|
|
|
|
2010-04-04 07:15:48 +00:00
|
|
|
// Determine the list of instances to iterate on.
|
2010-02-11 17:44:47 +00:00
|
|
|
list(, , $bundle) = entity_extract_ids($entity_type, $entity);
|
2010-04-04 07:15:48 +00:00
|
|
|
$instances = _field_invoke_get_instances($entity_type, $bundle, $options);
|
2009-08-11 14:59:40 +00:00
|
|
|
|
2010-04-04 07:15:48 +00:00
|
|
|
// Iterate through the instances and collect results.
|
|
|
|
$return = array();
|
2009-08-11 14:59:40 +00:00
|
|
|
foreach ($instances as $instance) {
|
2009-02-05 03:42:58 +00:00
|
|
|
$field_name = $instance['field_name'];
|
2010-04-04 07:15:48 +00:00
|
|
|
$field = field_info_field($field_name);
|
|
|
|
$function = $options['default'] ? 'field_default_' . $op : $field['module'] . '_field_' . $op;
|
|
|
|
if (function_exists($function)) {
|
|
|
|
// Determine the list of languages to iterate on.
|
2010-03-25 11:46:21 +00:00
|
|
|
$available_languages = field_available_languages($entity_type, $field);
|
|
|
|
$languages = _field_language_suggestion($available_languages, $options['language'], $field_name);
|
2009-06-06 16:17:30 +00:00
|
|
|
|
2010-03-25 11:46:21 +00:00
|
|
|
foreach ($languages as $langcode) {
|
2010-04-04 07:15:48 +00:00
|
|
|
$items = isset($entity->{$field_name}[$langcode]) ? $entity->{$field_name}[$langcode] : array();
|
|
|
|
$result = $function($entity_type, $entity, $field, $instance, $langcode, $items, $a, $b);
|
|
|
|
if (isset($result)) {
|
|
|
|
// For hooks with array results, we merge results together.
|
|
|
|
// For hooks with scalar results, we collect results in an array.
|
|
|
|
if (is_array($result)) {
|
|
|
|
$return = array_merge($return, $result);
|
2009-06-06 16:17:30 +00:00
|
|
|
}
|
2010-04-04 07:15:48 +00:00
|
|
|
else {
|
|
|
|
$return[] = $result;
|
2009-06-06 16:17:30 +00:00
|
|
|
}
|
|
|
|
}
|
2010-04-04 07:15:48 +00:00
|
|
|
|
|
|
|
// Populate $items back in the field values, but avoid replacing missing
|
|
|
|
// fields with an empty array (those are not equivalent on update).
|
|
|
|
if ($items !== array() || isset($entity->{$field_name}[$langcode])) {
|
|
|
|
$entity->{$field_name}[$langcode] = $items;
|
|
|
|
}
|
2009-02-05 03:42:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
2009-05-17 00:32:29 +00:00
|
|
|
/**
|
2010-02-12 05:38:10 +00:00
|
|
|
* Invoke a field hook across fields on multiple entities.
|
2009-05-17 00:32:29 +00:00
|
|
|
*
|
|
|
|
* @param $op
|
|
|
|
* Possible operations include:
|
|
|
|
* - load
|
2009-12-28 20:24:08 +00:00
|
|
|
* - prepare_view
|
2009-06-06 16:17:30 +00:00
|
|
|
* For all other operations, use _field_invoke() / field_invoke_default()
|
|
|
|
* instead.
|
2010-02-11 17:44:47 +00:00
|
|
|
* @param $entity_type
|
|
|
|
* The type of $entity; e.g. 'node' or 'user'.
|
|
|
|
* @param $entities
|
2010-02-12 05:38:10 +00:00
|
|
|
* An array of entities, keyed by entity id.
|
2009-05-17 00:32:29 +00:00
|
|
|
* @param $a
|
|
|
|
* - The $age parameter in the 'load' operation.
|
|
|
|
* - Otherwise NULL.
|
|
|
|
* @param $b
|
|
|
|
* Currently always NULL.
|
2009-06-06 16:17:30 +00:00
|
|
|
* @param $options
|
|
|
|
* An associative array of additional options, with the following keys:
|
2009-08-11 14:59:40 +00:00
|
|
|
* - 'field_name': The name of the field whose operation should be
|
|
|
|
* invoked. By default, the operation is invoked on all the fields
|
2010-02-12 05:38:10 +00:00
|
|
|
* in the entity's bundle. NOTE: This option is not compatible with
|
2009-08-11 14:59:40 +00:00
|
|
|
* the 'deleted' option; the 'field_id' option should be used instead.
|
|
|
|
* - 'field_id': The id of the field whose operation should be
|
|
|
|
* invoked. By default, the operation is invoked on all the fields
|
2010-02-12 05:38:10 +00:00
|
|
|
* in the entity's' bundles.
|
2009-08-11 14:59:40 +00:00
|
|
|
* - 'default': A boolean value, specifying which implementation of
|
|
|
|
* the operation should be invoked.
|
2009-06-06 16:17:30 +00:00
|
|
|
* - if FALSE (default), the field types implementation of the operation
|
|
|
|
* will be invoked (hook_field_[op])
|
|
|
|
* - If TRUE, the default field implementation of the field operation
|
|
|
|
* will be invoked (field_default_[op])
|
|
|
|
* Internal use only. Do not explicitely set to TRUE, but use
|
|
|
|
* _field_invoke_multiple_default() instead.
|
2009-08-11 14:59:40 +00:00
|
|
|
* - 'deleted': If TRUE, the function will operate on deleted fields
|
|
|
|
* as well as non-deleted fields. If unset or FALSE, only
|
|
|
|
* non-deleted fields are operated on.
|
2011-05-13 14:29:20 +00:00
|
|
|
* - 'language': A language code or an array of arrays of language codes keyed
|
|
|
|
* by entity id and field name. It will be used to narrow down to a single
|
|
|
|
* value the available languages to act on.
|
2010-03-25 11:46:21 +00:00
|
|
|
*
|
2009-05-17 00:32:29 +00:00
|
|
|
* @return
|
2010-02-12 05:38:10 +00:00
|
|
|
* An array of returned values keyed by entity id.
|
2009-05-17 00:32:29 +00:00
|
|
|
*/
|
2010-02-11 17:44:47 +00:00
|
|
|
function _field_invoke_multiple($op, $entity_type, $entities, &$a = NULL, &$b = NULL, $options = array()) {
|
2009-06-06 16:17:30 +00:00
|
|
|
// Merge default options.
|
|
|
|
$default_options = array(
|
|
|
|
'default' => FALSE,
|
2009-08-11 14:59:40 +00:00
|
|
|
'deleted' => FALSE,
|
2009-08-22 00:58:55 +00:00
|
|
|
'language' => NULL,
|
2009-06-06 16:17:30 +00:00
|
|
|
);
|
|
|
|
$options += $default_options;
|
2010-08-01 19:49:35 +00:00
|
|
|
$field_info = field_info_field_by_ids();
|
2009-06-06 16:17:30 +00:00
|
|
|
|
2009-05-17 00:32:29 +00:00
|
|
|
$fields = array();
|
|
|
|
$grouped_instances = array();
|
2010-02-12 05:38:10 +00:00
|
|
|
$grouped_entities = array();
|
2009-05-17 00:32:29 +00:00
|
|
|
$grouped_items = array();
|
|
|
|
$return = array();
|
|
|
|
|
2010-02-12 05:38:10 +00:00
|
|
|
// Go through the entities and collect the fields on which the hook should be
|
2009-06-06 16:17:30 +00:00
|
|
|
// invoked.
|
2009-08-11 14:59:40 +00:00
|
|
|
//
|
|
|
|
// We group fields by id, not by name, because this function can operate on
|
2010-02-12 05:38:10 +00:00
|
|
|
// deleted fields which may have non-unique names. However, entities can only
|
2009-08-11 14:59:40 +00:00
|
|
|
// contain data for a single field for each name, even if that field
|
|
|
|
// is deleted, so we reference field data via the
|
2010-02-11 17:44:47 +00:00
|
|
|
// $entity->$field_name property.
|
|
|
|
foreach ($entities as $entity) {
|
2010-04-04 07:15:48 +00:00
|
|
|
// Determine the list of instances to iterate on.
|
2010-02-11 17:44:47 +00:00
|
|
|
list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
|
2010-04-04 07:15:48 +00:00
|
|
|
$instances = _field_invoke_get_instances($entity_type, $bundle, $options);
|
2009-08-11 14:59:40 +00:00
|
|
|
|
|
|
|
foreach ($instances as $instance) {
|
|
|
|
$field_id = $instance['field_id'];
|
2009-05-17 00:32:29 +00:00
|
|
|
$field_name = $instance['field_name'];
|
2010-08-01 19:49:35 +00:00
|
|
|
$field = $field_info[$field_id];
|
2010-04-04 07:15:48 +00:00
|
|
|
$function = $options['default'] ? 'field_default_' . $op : $field['module'] . '_field_' . $op;
|
|
|
|
if (function_exists($function)) {
|
2009-06-06 16:17:30 +00:00
|
|
|
// Add the field to the list of fields to invoke the hook on.
|
2009-08-11 14:59:40 +00:00
|
|
|
if (!isset($fields[$field_id])) {
|
2010-04-04 07:15:48 +00:00
|
|
|
$fields[$field_id] = $field;
|
2009-06-06 16:17:30 +00:00
|
|
|
}
|
|
|
|
// Extract the field values into a separate variable, easily accessed
|
|
|
|
// by hook implementations.
|
2010-03-25 11:46:21 +00:00
|
|
|
// Unless a language suggestion is provided we iterate on all the
|
|
|
|
// available languages.
|
2010-04-04 07:15:48 +00:00
|
|
|
$available_languages = field_available_languages($entity_type, $field);
|
2011-05-13 14:29:20 +00:00
|
|
|
$language = !empty($options['language'][$id]) ? $options['language'][$id] : $options['language'];
|
|
|
|
$languages = _field_language_suggestion($available_languages, $language, $field_name);
|
2010-03-25 11:46:21 +00:00
|
|
|
foreach ($languages as $langcode) {
|
2010-02-11 17:44:47 +00:00
|
|
|
$grouped_items[$field_id][$langcode][$id] = isset($entity->{$field_name}[$langcode]) ? $entity->{$field_name}[$langcode] : array();
|
2011-06-22 05:31:44 +00:00
|
|
|
// Group the instances and entities corresponding to the current
|
|
|
|
// field.
|
|
|
|
$grouped_instances[$field_id][$langcode][$id] = $instance;
|
|
|
|
$grouped_entities[$field_id][$langcode][$id] = $entities[$id];
|
2009-08-22 00:58:55 +00:00
|
|
|
}
|
2009-05-17 00:32:29 +00:00
|
|
|
}
|
|
|
|
}
|
2010-02-12 05:38:10 +00:00
|
|
|
// Initialize the return value for each entity.
|
2009-05-17 00:32:29 +00:00
|
|
|
$return[$id] = array();
|
|
|
|
}
|
|
|
|
|
2009-06-06 16:17:30 +00:00
|
|
|
// For each field, invoke the field hook and collect results.
|
2009-08-11 14:59:40 +00:00
|
|
|
foreach ($fields as $field_id => $field) {
|
|
|
|
$field_name = $field['field_name'];
|
2009-06-06 16:17:30 +00:00
|
|
|
$function = $options['default'] ? 'field_default_' . $op : $field['module'] . '_field_' . $op;
|
2010-04-04 07:15:48 +00:00
|
|
|
// Iterate over all the field translations.
|
2011-06-22 05:31:44 +00:00
|
|
|
foreach ($grouped_items[$field_id] as $langcode => &$items) {
|
|
|
|
$entities = $grouped_entities[$field_id][$langcode];
|
|
|
|
$instances = $grouped_instances[$field_id][$langcode];
|
|
|
|
$results = $function($entity_type, $entities, $field, $instances, $langcode, $items, $a, $b);
|
2010-04-04 07:15:48 +00:00
|
|
|
if (isset($results)) {
|
|
|
|
// Collect results by entity.
|
|
|
|
// For hooks with array results, we merge results together.
|
|
|
|
// For hooks with scalar results, we collect results in an array.
|
|
|
|
foreach ($results as $id => $result) {
|
|
|
|
if (is_array($result)) {
|
|
|
|
$return[$id] = array_merge($return[$id], $result);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$return[$id][] = $result;
|
2009-05-17 00:32:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-12 05:38:10 +00:00
|
|
|
// Populate field values back in the entities, but avoid replacing missing
|
2009-05-17 00:32:29 +00:00
|
|
|
// fields with an empty array (those are not equivalent on update).
|
2011-06-22 05:31:44 +00:00
|
|
|
foreach ($grouped_entities[$field_id] as $langcode => $entities) {
|
|
|
|
foreach ($entities as $id => $entity) {
|
|
|
|
if ($grouped_items[$field_id][$langcode][$id] !== array() || isset($entity->{$field_name}[$langcode])) {
|
2010-02-11 17:44:47 +00:00
|
|
|
$entity->{$field_name}[$langcode] = $grouped_items[$field_id][$langcode][$id];
|
2009-08-22 00:58:55 +00:00
|
|
|
}
|
2009-05-17 00:32:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
2009-02-05 03:42:58 +00:00
|
|
|
/**
|
|
|
|
* Invoke field.module's version of a field hook.
|
2009-06-06 16:17:30 +00:00
|
|
|
*
|
|
|
|
* This function invokes the field_default_[op]() function.
|
|
|
|
* Use _field_invoke() to invoke the field type implementation,
|
|
|
|
* hook_field_[op]().
|
|
|
|
*
|
2010-03-26 17:14:46 +00:00
|
|
|
* @see _field_invoke()
|
2009-02-05 03:42:58 +00:00
|
|
|
*/
|
2010-02-11 17:44:47 +00:00
|
|
|
function _field_invoke_default($op, $entity_type, $entity, &$a = NULL, &$b = NULL, $options = array()) {
|
2009-06-06 16:17:30 +00:00
|
|
|
$options['default'] = TRUE;
|
2010-02-11 17:44:47 +00:00
|
|
|
return _field_invoke($op, $entity_type, $entity, $a, $b, $options);
|
2009-02-05 03:42:58 +00:00
|
|
|
}
|
|
|
|
|
2009-05-17 00:32:29 +00:00
|
|
|
/**
|
2010-02-12 05:38:10 +00:00
|
|
|
* Invoke field.module's version of a field hook on multiple entities.
|
2009-06-06 16:17:30 +00:00
|
|
|
*
|
|
|
|
* This function invokes the field_default_[op]() function.
|
|
|
|
* Use _field_invoke_multiple() to invoke the field type implementation,
|
|
|
|
* hook_field_[op]().
|
|
|
|
*
|
2010-03-26 17:14:46 +00:00
|
|
|
* @see _field_invoke_multiple()
|
2009-05-17 00:32:29 +00:00
|
|
|
*/
|
2010-02-11 17:44:47 +00:00
|
|
|
function _field_invoke_multiple_default($op, $entity_type, $entities, &$a = NULL, &$b = NULL, $options = array()) {
|
2009-06-06 16:17:30 +00:00
|
|
|
$options['default'] = TRUE;
|
2010-02-11 17:44:47 +00:00
|
|
|
return _field_invoke_multiple($op, $entity_type, $entities, $a, $b, $options);
|
2009-05-17 00:32:29 +00:00
|
|
|
}
|
|
|
|
|
2010-04-04 07:15:48 +00:00
|
|
|
/**
|
|
|
|
* Helper for _field_invoke(): retrieves a list of instances to operate on.
|
|
|
|
*
|
|
|
|
* @param $entity_type
|
|
|
|
* The entity type.
|
|
|
|
* @param $bundle
|
|
|
|
* The bundle name.
|
|
|
|
* @param $options
|
|
|
|
* An associative array of options, as provided to _field_invoke(). Only the
|
|
|
|
* following keys are considered :
|
|
|
|
* - deleted
|
|
|
|
* - field_name
|
|
|
|
* - field_id
|
|
|
|
* See _field_invoke() for details.
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* The array of selected instance definitions.
|
|
|
|
*/
|
|
|
|
function _field_invoke_get_instances($entity_type, $bundle, $options) {
|
|
|
|
if ($options['deleted']) {
|
|
|
|
// Deleted fields are not included in field_info_instances(), and need to
|
|
|
|
// be fetched from the database with field_read_instances().
|
|
|
|
$params = array('entity_type' => $entity_type, 'bundle' => $bundle);
|
|
|
|
if (isset($options['field_id'])) {
|
|
|
|
// Single-field mode by field id: field_read_instances() does the filtering.
|
|
|
|
// Single-field mode by field name is not compatible with the 'deleted'
|
|
|
|
// option.
|
|
|
|
$params['field_id'] = $options['field_id'];
|
|
|
|
}
|
|
|
|
$instances = field_read_instances($params, array('include_deleted' => TRUE));
|
|
|
|
}
|
|
|
|
elseif (isset($options['field_name'])) {
|
|
|
|
// Single-field mode by field name: field_info_instance() does the
|
|
|
|
// filtering.
|
|
|
|
$instances = array(field_info_instance($entity_type, $options['field_name'], $bundle));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$instances = field_info_instances($entity_type, $bundle);
|
|
|
|
if (isset($options['field_id'])) {
|
|
|
|
// Single-field mode by field id: we need to loop on each instance to
|
|
|
|
// find the right one.
|
|
|
|
foreach ($instances as $instance) {
|
|
|
|
if ($instance['field_id'] == $options['field_id']) {
|
|
|
|
$instances = array($instance);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $instances;
|
|
|
|
}
|
|
|
|
|
2009-02-05 03:42:58 +00:00
|
|
|
/**
|
2010-02-12 05:38:10 +00:00
|
|
|
* Add form elements for all fields for an entity to a form structure.
|
2009-02-05 03:42:58 +00:00
|
|
|
*
|
2010-11-20 19:57:01 +00:00
|
|
|
* The form elements for the entity's fields are added by reference as direct
|
|
|
|
* children in the $form parameter. This parameter can be a full form structure
|
|
|
|
* (most common case for entity edit forms), or a sub-element of a larger form.
|
|
|
|
*
|
|
|
|
* By default, submitted field values appear at the top-level of
|
|
|
|
* $form_state['values']. A different location within $form_state['values'] can
|
|
|
|
* be specified by setting the '#parents' property on the incoming $form
|
|
|
|
* parameter. Because of name clashes, two instances of the same field cannot
|
|
|
|
* appear within the same $form element, or within the same '#parents' space.
|
|
|
|
*
|
|
|
|
* For each call to field_attach_form(), field values are processed by calling
|
|
|
|
* field_attach_form_validate() and field_attach_submit() on the same $form
|
|
|
|
* element.
|
|
|
|
*
|
|
|
|
* Sample resulting structure in $form:
|
2010-02-11 15:42:14 +00:00
|
|
|
* @code
|
2010-11-20 19:57:01 +00:00
|
|
|
* '#parents' => The location of field values in $form_state['values'],
|
|
|
|
* '#entity_type' => The name of the entity type,
|
|
|
|
* '#bundle' => The name of the bundle,
|
|
|
|
* // One sub-array per field appearing in the entity, keyed by field name.
|
2010-02-11 15:42:14 +00:00
|
|
|
* // The structure of the array differs slightly depending on whether the
|
|
|
|
* // widget is 'single-value' (provides the input for one field value,
|
|
|
|
* // most common case), and will therefore be repeated as many times as
|
|
|
|
* // needed, or 'multiple-values' (one single widget allows the input of
|
|
|
|
* // several values, e.g checkboxes, select box...).
|
|
|
|
* // The sub-array is nested into a $langcode key where $langcode has the
|
|
|
|
* // same value of the $langcode parameter above.
|
|
|
|
* // The '#language' key holds the same value of $langcode and it is used
|
|
|
|
* // to access the field sub-array when $langcode is unknown.
|
|
|
|
* 'field_foo' => array(
|
|
|
|
* '#tree' => TRUE,
|
2010-11-20 19:57:01 +00:00
|
|
|
* '#field_name' => The name of the field,
|
2010-02-11 15:42:14 +00:00
|
|
|
* '#language' => $langcode,
|
|
|
|
* $langcode => array(
|
2010-11-20 19:57:01 +00:00
|
|
|
* '#field_name' => The name of the field,
|
|
|
|
* '#language' => $langcode,
|
|
|
|
* '#field_parents' => The 'parents' space for the field in the form,
|
|
|
|
* equal to the #parents property of the $form parameter received by
|
|
|
|
* field_attach_form(),
|
|
|
|
* '#required' => Whether or not the field is required,
|
|
|
|
* '#title' => The label of the field instance,
|
|
|
|
* '#description' => The description text for the field instance,
|
2010-02-11 15:42:14 +00:00
|
|
|
*
|
|
|
|
* // Only for 'single' widgets:
|
|
|
|
* '#theme' => 'field_multiple_value_form',
|
2010-11-20 19:57:01 +00:00
|
|
|
* '#cardinality' => The field cardinality,
|
2010-02-11 15:42:14 +00:00
|
|
|
* // One sub-array per copy of the widget, keyed by delta.
|
|
|
|
* 0 => array(
|
2010-11-20 19:57:01 +00:00
|
|
|
* '#entity_type' => The name of the entity type,
|
|
|
|
* '#bundle' => The name of the bundle,
|
|
|
|
* '#field_name' => The name of the field,
|
|
|
|
* '#field_parents' => The 'parents' space for the field in the form,
|
|
|
|
* equal to the #parents property of the $form parameter received by
|
|
|
|
* field_attach_form(),
|
|
|
|
* '#title' => The title to be displayed by the widget,
|
|
|
|
* '#default_value' => The field value for delta 0,
|
|
|
|
* '#required' => Whether the widget should be marked required,
|
2010-02-11 15:42:14 +00:00
|
|
|
* '#delta' => 0,
|
2010-11-20 19:57:01 +00:00
|
|
|
* '#columns' => The array of field columns,
|
2010-02-11 15:42:14 +00:00
|
|
|
* // The remaining elements in the sub-array depend on the widget.
|
2010-11-20 19:57:01 +00:00
|
|
|
* '#type' => The type of the widget,
|
2010-02-11 15:42:14 +00:00
|
|
|
* ...
|
|
|
|
* ),
|
|
|
|
* 1 => array(
|
|
|
|
* ...
|
|
|
|
* ),
|
|
|
|
*
|
|
|
|
* // Only for multiple widgets:
|
2010-11-20 19:57:01 +00:00
|
|
|
* '#entity_type' => The name of the entity type,
|
2010-02-11 15:42:14 +00:00
|
|
|
* '#bundle' => $instance['bundle'],
|
|
|
|
* '#columns' => array_keys($field['columns']),
|
|
|
|
* // The remaining elements in the sub-array depend on the widget.
|
2010-11-20 19:57:01 +00:00
|
|
|
* '#type' => The type of the widget,
|
2010-02-11 15:42:14 +00:00
|
|
|
* ...
|
|
|
|
* ),
|
|
|
|
* ...
|
|
|
|
* ),
|
|
|
|
* )
|
|
|
|
* @endcode
|
|
|
|
*
|
2010-11-20 19:57:01 +00:00
|
|
|
* Additionally, some processing data is placed in $form_state, and can be
|
|
|
|
* accessed by field_form_get_state() and field_form_set_state().
|
2010-04-04 12:48:18 +00:00
|
|
|
*
|
2010-02-11 17:44:47 +00:00
|
|
|
* @param $entity_type
|
|
|
|
* The type of $entity; e.g. 'node' or 'user'.
|
|
|
|
* @param $entity
|
2010-02-12 05:38:10 +00:00
|
|
|
* The entity for which to load form elements, used to initialize
|
2009-02-05 03:42:58 +00:00
|
|
|
* default form values.
|
|
|
|
* @param $form
|
2010-11-20 19:57:01 +00:00
|
|
|
* The form structure to fill in. This can be a full form structure, or a
|
|
|
|
* sub-element of a larger form. The #parents property can be set to control
|
|
|
|
* the location of submitted field values within $form_state['values']. If
|
|
|
|
* not specified, $form['#parents'] is set to an empty array, placing field
|
|
|
|
* values at the top-level of $form_state['values'].
|
2009-02-05 03:42:58 +00:00
|
|
|
* @param $form_state
|
|
|
|
* An associative array containing the current state of the form.
|
2009-08-22 00:58:55 +00:00
|
|
|
* @param $langcode
|
|
|
|
* The language the field values are going to be entered, if no language
|
|
|
|
* is provided the default site language will be used.
|
2010-11-20 19:57:01 +00:00
|
|
|
*
|
|
|
|
* @see field_form_get_state()
|
|
|
|
* @see field_form_set_state()
|
2009-02-05 03:42:58 +00:00
|
|
|
*/
|
2010-02-11 17:44:47 +00:00
|
|
|
function field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode = NULL) {
|
2010-11-20 19:57:01 +00:00
|
|
|
// Set #parents to 'top-level' by default.
|
|
|
|
$form += array('#parents' => array());
|
|
|
|
|
2009-08-22 00:58:55 +00:00
|
|
|
// If no language is provided use the default site language.
|
2010-03-25 11:46:21 +00:00
|
|
|
$options = array('language' => field_valid_language($langcode));
|
2010-02-11 17:44:47 +00:00
|
|
|
$form += (array) _field_invoke_default('form', $entity_type, $entity, $form, $form_state, $options);
|
2009-02-05 03:42:58 +00:00
|
|
|
|
2009-08-19 13:31:14 +00:00
|
|
|
// Add custom weight handling.
|
2010-02-11 17:44:47 +00:00
|
|
|
list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
|
2010-05-23 19:10:23 +00:00
|
|
|
$form['#pre_render'][] = '_field_extra_fields_pre_render';
|
|
|
|
$form['#entity_type'] = $entity_type;
|
|
|
|
$form['#bundle'] = $bundle;
|
2009-08-19 13:31:14 +00:00
|
|
|
|
2009-02-05 03:42:58 +00:00
|
|
|
// Let other modules make changes to the form.
|
2009-11-08 19:11:56 +00:00
|
|
|
// Avoid module_invoke_all() to let parameters be taken by reference.
|
2009-02-05 03:42:58 +00:00
|
|
|
foreach (module_implements('field_attach_form') as $module) {
|
|
|
|
$function = $module . '_field_attach_form';
|
2010-02-11 17:44:47 +00:00
|
|
|
$function($entity_type, $entity, $form, $form_state, $langcode);
|
2009-02-05 03:42:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-04-04 07:33:15 +00:00
|
|
|
* Loads fields for the current revisions of a group of entities.
|
|
|
|
*
|
|
|
|
* Loads all fields for each entity object in a group of a single entity type.
|
|
|
|
* The loaded field values are added directly to the entity objects.
|
2009-02-05 03:42:58 +00:00
|
|
|
*
|
2010-11-20 19:57:01 +00:00
|
|
|
* field_attach_load() is automatically called by the default entity controller
|
|
|
|
* class, and thus, in most cases, doesn't need to be explicitly called by the
|
|
|
|
* entity type module.
|
|
|
|
*
|
2010-02-11 17:44:47 +00:00
|
|
|
* @param $entity_type
|
2010-04-04 07:33:15 +00:00
|
|
|
* The type of $entity; e.g., 'node' or 'user'.
|
2010-02-11 17:44:47 +00:00
|
|
|
* @param $entities
|
2010-04-04 07:33:15 +00:00
|
|
|
* An array of entities for which to load fields, keyed by entity ID.
|
2010-02-12 05:38:10 +00:00
|
|
|
* Each entity needs to have its 'bundle', 'id' and (if applicable)
|
2010-04-04 07:33:15 +00:00
|
|
|
* 'revision' keys filled in. The function adds the loaded field data
|
|
|
|
* directly in the entity objects of the $entities array.
|
2009-02-05 03:42:58 +00:00
|
|
|
* @param $age
|
|
|
|
* FIELD_LOAD_CURRENT to load the most recent revision for all
|
|
|
|
* fields, or FIELD_LOAD_REVISION to load the version indicated by
|
2010-02-12 05:38:10 +00:00
|
|
|
* each entity. Defaults to FIELD_LOAD_CURRENT; use
|
2009-02-05 03:42:58 +00:00
|
|
|
* field_attach_load_revision() instead of passing FIELD_LOAD_REVISION.
|
2009-07-15 17:55:18 +00:00
|
|
|
* @param $options
|
|
|
|
* An associative array of additional options, with the following keys:
|
2010-04-04 07:33:15 +00:00
|
|
|
* - 'field_id': The field ID that should be loaded, instead of
|
|
|
|
* loading all fields, for each entity. Note that returned entities
|
|
|
|
* may contain data for other fields, for example if they are read
|
|
|
|
* from a cache.
|
|
|
|
* - 'deleted': If TRUE, the function will operate on deleted fields
|
|
|
|
* as well as non-deleted fields. If unset or FALSE, only
|
|
|
|
* non-deleted fields are operated on.
|
2009-02-05 03:42:58 +00:00
|
|
|
*/
|
2010-02-11 17:44:47 +00:00
|
|
|
function field_attach_load($entity_type, $entities, $age = FIELD_LOAD_CURRENT, $options = array()) {
|
2010-08-01 19:49:35 +00:00
|
|
|
$field_info = field_info_field_by_ids();
|
2009-05-17 03:12:17 +00:00
|
|
|
$load_current = $age == FIELD_LOAD_CURRENT;
|
2009-02-05 03:42:58 +00:00
|
|
|
|
2009-08-11 14:59:40 +00:00
|
|
|
// Merge default options.
|
|
|
|
$default_options = array(
|
|
|
|
'deleted' => FALSE,
|
|
|
|
);
|
|
|
|
$options += $default_options;
|
|
|
|
|
2010-02-11 17:44:47 +00:00
|
|
|
$info = entity_get_info($entity_type);
|
2010-03-27 18:41:14 +00:00
|
|
|
// Only the most current revision of non-deleted fields for cacheable entity
|
|
|
|
// types can be cached.
|
|
|
|
$cache_read = $load_current && $info['field cache'] && empty($options['deleted']);
|
2009-07-15 17:55:18 +00:00
|
|
|
// In addition, do not write to the cache when loading a single field.
|
2009-08-11 14:59:40 +00:00
|
|
|
$cache_write = $cache_read && !isset($options['field_id']);
|
2009-05-17 03:12:17 +00:00
|
|
|
|
2010-02-11 17:44:47 +00:00
|
|
|
if (empty($entities)) {
|
2009-06-28 01:00:42 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-02-12 05:38:10 +00:00
|
|
|
// Assume all entities will need to be queried. Entities found in the cache
|
2009-06-28 01:00:42 +00:00
|
|
|
// will be removed from the list.
|
2010-02-12 05:38:10 +00:00
|
|
|
$queried_entities = $entities;
|
2009-05-17 00:32:29 +00:00
|
|
|
|
2010-02-12 05:38:10 +00:00
|
|
|
// Fetch available entities from cache, if applicable.
|
2009-07-15 17:55:18 +00:00
|
|
|
if ($cache_read) {
|
2009-06-28 01:00:42 +00:00
|
|
|
// Build the list of cache entries to retrieve.
|
|
|
|
$cids = array();
|
2010-02-11 17:44:47 +00:00
|
|
|
foreach ($entities as $id => $entity) {
|
|
|
|
$cids[] = "field:$entity_type:$id";
|
2009-06-28 01:00:42 +00:00
|
|
|
}
|
|
|
|
$cache = cache_get_multiple($cids, 'cache_field');
|
2010-02-12 05:38:10 +00:00
|
|
|
// Put the cached field values back into the entities and remove them from
|
|
|
|
// the list of entities to query.
|
2010-02-11 17:44:47 +00:00
|
|
|
foreach ($entities as $id => $entity) {
|
|
|
|
$cid = "field:$entity_type:$id";
|
2009-06-28 01:00:42 +00:00
|
|
|
if (isset($cache[$cid])) {
|
2010-02-12 05:38:10 +00:00
|
|
|
unset($queried_entities[$id]);
|
2009-06-28 01:00:42 +00:00
|
|
|
foreach ($cache[$cid]->data as $field_name => $values) {
|
2010-02-11 17:44:47 +00:00
|
|
|
$entity->$field_name = $values;
|
2009-05-17 03:12:17 +00:00
|
|
|
}
|
|
|
|
}
|
2009-02-05 03:42:58 +00:00
|
|
|
}
|
2009-05-17 03:12:17 +00:00
|
|
|
}
|
|
|
|
|
2010-02-12 05:38:10 +00:00
|
|
|
// Fetch other entities from their storage location.
|
|
|
|
if ($queried_entities) {
|
2009-05-17 00:32:29 +00:00
|
|
|
// The invoke order is:
|
2009-11-08 19:11:56 +00:00
|
|
|
// - hook_field_storage_pre_load()
|
- Patch #443422 by yched, bjaspan | chx, merlinofchaos, Scott Reynolds, plach, profix898, mattyoung: added support for pluggable 'per field' storage engine. Comes with documentation and tests.
The Field Attach API uses the Field Storage API to perform all "database access". Each Field Storage API hook function defines a primitive database operation such as read, write, or delete. The default field storage module, field_sql_storage.module, uses the local SQL database to implement these operations, but alternative field storage backends can choose to represent the data in SQL differently or use a completely different storage mechanism such as a cloud-based database.
2009-09-27 12:52:55 +00:00
|
|
|
// - storage backend's hook_field_storage_load()
|
2009-05-17 00:32:29 +00:00
|
|
|
// - field-type module's hook_field_load()
|
2009-03-30 03:44:55 +00:00
|
|
|
// - hook_field_attach_load()
|
|
|
|
|
2009-11-08 19:11:56 +00:00
|
|
|
// Invoke hook_field_storage_pre_load(): let any module load field
|
2009-03-30 03:44:55 +00:00
|
|
|
// data before the storage engine, accumulating along the way.
|
|
|
|
$skip_fields = array();
|
2009-11-08 19:11:56 +00:00
|
|
|
foreach (module_implements('field_storage_pre_load') as $module) {
|
|
|
|
$function = $module . '_field_storage_pre_load';
|
2010-02-12 05:38:10 +00:00
|
|
|
$function($entity_type, $queried_entities, $age, $skip_fields, $options);
|
2009-03-30 03:44:55 +00:00
|
|
|
}
|
|
|
|
|
2010-04-04 07:15:48 +00:00
|
|
|
$instances = array();
|
|
|
|
|
2010-02-12 05:38:10 +00:00
|
|
|
// Collect the storage backends used by the remaining fields in the entities.
|
- Patch #443422 by yched, bjaspan | chx, merlinofchaos, Scott Reynolds, plach, profix898, mattyoung: added support for pluggable 'per field' storage engine. Comes with documentation and tests.
The Field Attach API uses the Field Storage API to perform all "database access". Each Field Storage API hook function defines a primitive database operation such as read, write, or delete. The default field storage module, field_sql_storage.module, uses the local SQL database to implement these operations, but alternative field storage backends can choose to represent the data in SQL differently or use a completely different storage mechanism such as a cloud-based database.
2009-09-27 12:52:55 +00:00
|
|
|
$storages = array();
|
2010-02-12 05:38:10 +00:00
|
|
|
foreach ($queried_entities as $entity) {
|
|
|
|
list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
|
2010-04-04 07:15:48 +00:00
|
|
|
$instances = _field_invoke_get_instances($entity_type, $bundle, $options);
|
- Patch #443422 by yched, bjaspan | chx, merlinofchaos, Scott Reynolds, plach, profix898, mattyoung: added support for pluggable 'per field' storage engine. Comes with documentation and tests.
The Field Attach API uses the Field Storage API to perform all "database access". Each Field Storage API hook function defines a primitive database operation such as read, write, or delete. The default field storage module, field_sql_storage.module, uses the local SQL database to implement these operations, but alternative field storage backends can choose to represent the data in SQL differently or use a completely different storage mechanism such as a cloud-based database.
2009-09-27 12:52:55 +00:00
|
|
|
|
|
|
|
foreach ($instances as $instance) {
|
2010-04-04 07:15:48 +00:00
|
|
|
$field_name = $instance['field_name'];
|
|
|
|
$field_id = $instance['field_id'];
|
|
|
|
// Make sure all fields are present at least as empty arrays.
|
|
|
|
if (!isset($queried_entities[$id]->{$field_name})) {
|
|
|
|
$queried_entities[$id]->{$field_name} = array();
|
|
|
|
}
|
|
|
|
// Collect the storage backend if the field has not been loaded yet.
|
|
|
|
if (!isset($skip_fields[$field_id])) {
|
2010-08-01 19:49:35 +00:00
|
|
|
$field = $field_info[$field_id];
|
2010-04-04 07:15:48 +00:00
|
|
|
$storages[$field['storage']['type']][$field_id][] = $load_current ? $id : $vid;
|
- Patch #443422 by yched, bjaspan | chx, merlinofchaos, Scott Reynolds, plach, profix898, mattyoung: added support for pluggable 'per field' storage engine. Comes with documentation and tests.
The Field Attach API uses the Field Storage API to perform all "database access". Each Field Storage API hook function defines a primitive database operation such as read, write, or delete. The default field storage module, field_sql_storage.module, uses the local SQL database to implement these operations, but alternative field storage backends can choose to represent the data in SQL differently or use a completely different storage mechanism such as a cloud-based database.
2009-09-27 12:52:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Invoke hook_field_storage_load() on the relevant storage backends.
|
|
|
|
foreach ($storages as $storage => $fields) {
|
|
|
|
$storage_info = field_info_storage_types($storage);
|
2010-02-12 05:38:10 +00:00
|
|
|
module_invoke($storage_info['module'], 'field_storage_load', $entity_type, $queried_entities, $age, $fields, $options);
|
- Patch #443422 by yched, bjaspan | chx, merlinofchaos, Scott Reynolds, plach, profix898, mattyoung: added support for pluggable 'per field' storage engine. Comes with documentation and tests.
The Field Attach API uses the Field Storage API to perform all "database access". Each Field Storage API hook function defines a primitive database operation such as read, write, or delete. The default field storage module, field_sql_storage.module, uses the local SQL database to implement these operations, but alternative field storage backends can choose to represent the data in SQL differently or use a completely different storage mechanism such as a cloud-based database.
2009-09-27 12:52:55 +00:00
|
|
|
}
|
2009-03-30 03:44:55 +00:00
|
|
|
|
2009-05-17 00:32:29 +00:00
|
|
|
// Invoke field-type module's hook_field_load().
|
2010-02-12 05:38:10 +00:00
|
|
|
_field_invoke_multiple('load', $entity_type, $queried_entities, $age, $options);
|
2009-05-17 00:32:29 +00:00
|
|
|
|
|
|
|
// Invoke hook_field_attach_load(): let other modules act on loading the
|
2010-02-12 05:38:10 +00:00
|
|
|
// entitiy.
|
|
|
|
module_invoke_all('field_attach_load', $entity_type, $queried_entities, $age, $options);
|
2009-02-05 03:42:58 +00:00
|
|
|
|
2009-05-17 00:32:29 +00:00
|
|
|
// Build cache data.
|
2009-07-15 17:55:18 +00:00
|
|
|
if ($cache_write) {
|
2010-02-12 05:38:10 +00:00
|
|
|
foreach ($queried_entities as $id => $entity) {
|
2009-05-17 00:32:29 +00:00
|
|
|
$data = array();
|
2010-02-11 17:44:47 +00:00
|
|
|
list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
|
|
|
|
$instances = field_info_instances($entity_type, $bundle);
|
2009-05-17 00:32:29 +00:00
|
|
|
foreach ($instances as $instance) {
|
2010-02-12 05:38:10 +00:00
|
|
|
$data[$instance['field_name']] = $queried_entities[$id]->{$instance['field_name']};
|
2009-02-05 03:42:58 +00:00
|
|
|
}
|
2010-02-11 17:44:47 +00:00
|
|
|
$cid = "field:$entity_type:$id";
|
2011-09-11 16:14:18 +00:00
|
|
|
cache('field')->set($cid, $data);
|
2009-02-05 03:42:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-04-04 07:33:15 +00:00
|
|
|
* Load all fields for previous versions of a group of entities.
|
2009-02-05 03:42:58 +00:00
|
|
|
*
|
2010-04-04 07:33:15 +00:00
|
|
|
* Loading different versions of the same entities is not supported, and should
|
|
|
|
* be done by separate calls to the function.
|
2009-05-17 00:32:29 +00:00
|
|
|
*
|
2010-04-04 12:48:18 +00:00
|
|
|
* field_attach_load_revision() is automatically called by the default entity
|
|
|
|
* controller class, and thus, in most cases, doesn't need to be explicitly
|
|
|
|
* called by the entity type module.
|
|
|
|
*
|
2010-02-11 17:44:47 +00:00
|
|
|
* @param $entity_type
|
|
|
|
* The type of $entity; e.g. 'node' or 'user'.
|
|
|
|
* @param $entities
|
2010-04-04 07:33:15 +00:00
|
|
|
* An array of entities for which to load fields, keyed by entity ID. Each
|
|
|
|
* entity needs to have its 'bundle', 'id' and (if applicable) 'revision'
|
|
|
|
* keys filled. The function adds the loaded field data directly in the
|
|
|
|
* entity objects of the $entities array.
|
2009-07-15 17:55:18 +00:00
|
|
|
* @param $options
|
2010-04-04 07:33:15 +00:00
|
|
|
* An associative array of additional options. See field_attach_load() for
|
|
|
|
* details.
|
2009-02-05 03:42:58 +00:00
|
|
|
*/
|
2010-02-11 17:44:47 +00:00
|
|
|
function field_attach_load_revision($entity_type, $entities, $options = array()) {
|
|
|
|
return field_attach_load($entity_type, $entities, FIELD_LOAD_REVISION, $options);
|
2009-02-05 03:42:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-02-12 05:38:10 +00:00
|
|
|
* Perform field validation against the field data in an entity.
|
2009-02-05 03:42:58 +00:00
|
|
|
*
|
2009-03-26 13:31:28 +00:00
|
|
|
* This function does not perform field widget validation on form
|
|
|
|
* submissions. It is intended to be called during API save
|
|
|
|
* operations. Use field_attach_form_validate() to validate form
|
|
|
|
* submissions.
|
2009-02-05 03:42:58 +00:00
|
|
|
*
|
2010-02-11 17:44:47 +00:00
|
|
|
* @param $entity_type
|
|
|
|
* The type of $entity; e.g. 'node' or 'user'.
|
|
|
|
* @param $entity
|
2010-02-12 05:38:10 +00:00
|
|
|
* The entity with fields to validate.
|
2009-12-13 12:41:08 +00:00
|
|
|
* @throws FieldValidationException
|
|
|
|
* If validation errors are found, a FieldValidationException is thrown. The
|
|
|
|
* 'errors' property contains the array of errors, keyed by field name,
|
|
|
|
* language and delta.
|
2009-02-05 03:42:58 +00:00
|
|
|
*/
|
2010-02-11 17:44:47 +00:00
|
|
|
function field_attach_validate($entity_type, $entity) {
|
2009-03-26 13:31:28 +00:00
|
|
|
$errors = array();
|
2009-12-13 12:41:08 +00:00
|
|
|
// Check generic, field-type-agnostic errors first.
|
2010-02-11 17:44:47 +00:00
|
|
|
_field_invoke_default('validate', $entity_type, $entity, $errors);
|
2009-12-13 12:41:08 +00:00
|
|
|
// Check field-type specific errors.
|
2010-02-11 17:44:47 +00:00
|
|
|
_field_invoke('validate', $entity_type, $entity, $errors);
|
2009-02-05 03:42:58 +00:00
|
|
|
|
2010-02-12 05:38:10 +00:00
|
|
|
// Let other modules validate the entity.
|
2009-11-08 19:11:56 +00:00
|
|
|
// Avoid module_invoke_all() to let $errors be taken by reference.
|
2009-02-05 03:42:58 +00:00
|
|
|
foreach (module_implements('field_attach_validate') as $module) {
|
|
|
|
$function = $module . '_field_attach_validate';
|
2010-02-11 17:44:47 +00:00
|
|
|
$function($entity_type, $entity, $errors);
|
2009-03-26 13:31:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($errors) {
|
|
|
|
throw new FieldValidationException($errors);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform field validation against form-submitted field values.
|
|
|
|
*
|
|
|
|
* There are two levels of validation for fields in forms: widget
|
|
|
|
* validation, and field validation.
|
|
|
|
* - Widget validation steps are specific to a given widget's own form
|
2011-03-27 23:48:37 +00:00
|
|
|
* structure and UI metaphors. They are executed through FAPI's
|
|
|
|
* #element_validate property during normal form validation.
|
2009-03-26 13:31:28 +00:00
|
|
|
* - Field validation steps are common to a given field type, independently of
|
2011-03-27 23:48:37 +00:00
|
|
|
* the specific widget being used in a given form. They are defined in the
|
|
|
|
* field type's implementation of hook_field_validate().
|
2009-03-26 13:31:28 +00:00
|
|
|
*
|
|
|
|
* This function performs field validation in the context of a form
|
|
|
|
* submission. It converts field validation errors into form errors
|
2010-02-12 05:38:10 +00:00
|
|
|
* on the correct form elements. Fieldable entity types should call
|
2009-03-26 13:31:28 +00:00
|
|
|
* this function during their own form validation function.
|
|
|
|
*
|
2010-02-11 17:44:47 +00:00
|
|
|
* @param $entity_type
|
|
|
|
* The type of $entity; e.g. 'node' or 'user'.
|
|
|
|
* @param $entity
|
2010-02-12 05:38:10 +00:00
|
|
|
* The entity being submitted. The 'bundle', 'id' and (if applicable)
|
2009-07-10 05:58:13 +00:00
|
|
|
* 'revision' keys should be present. The actual field values will be read
|
2009-03-26 13:31:28 +00:00
|
|
|
* from $form_state['values'].
|
|
|
|
* @param $form
|
2010-11-20 19:57:01 +00:00
|
|
|
* The form structure where field elements are attached to. This might be a
|
|
|
|
* full form structure, or a sub-element of a larger form.
|
2009-03-26 13:31:28 +00:00
|
|
|
* @param $form_state
|
|
|
|
* An associative array containing the current state of the form.
|
|
|
|
*/
|
2010-02-11 17:44:47 +00:00
|
|
|
function field_attach_form_validate($entity_type, $entity, $form, &$form_state) {
|
2009-03-26 13:31:28 +00:00
|
|
|
// Extract field values from submitted values.
|
2010-02-11 17:44:47 +00:00
|
|
|
_field_invoke_default('extract_form_values', $entity_type, $entity, $form, $form_state);
|
2009-03-26 13:31:28 +00:00
|
|
|
|
|
|
|
// Perform field_level validation.
|
|
|
|
try {
|
2010-02-11 17:44:47 +00:00
|
|
|
field_attach_validate($entity_type, $entity);
|
2009-03-26 13:31:28 +00:00
|
|
|
}
|
|
|
|
catch (FieldValidationException $e) {
|
|
|
|
// Pass field-level validation errors back to widgets for accurate error
|
|
|
|
// flagging.
|
2010-02-11 15:42:14 +00:00
|
|
|
foreach ($e->errors as $field_name => $field_errors) {
|
2010-11-20 19:57:01 +00:00
|
|
|
foreach ($field_errors as $langcode => $errors) {
|
|
|
|
$field_state = field_form_get_state($form['#parents'], $field_name, $langcode, $form_state);
|
|
|
|
$field_state['errors'] = $errors;
|
|
|
|
field_form_set_state($form['#parents'], $field_name, $langcode, $form_state, $field_state);
|
2010-02-11 15:42:14 +00:00
|
|
|
}
|
|
|
|
}
|
2010-02-11 17:44:47 +00:00
|
|
|
_field_invoke_default('form_errors', $entity_type, $entity, $form, $form_state);
|
2009-02-05 03:42:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform necessary operations on field data submitted by a form.
|
|
|
|
*
|
|
|
|
* Currently, this accounts for drag-and-drop reordering of
|
|
|
|
* field values, and filtering of empty values.
|
|
|
|
*
|
2010-02-11 17:44:47 +00:00
|
|
|
* @param $entity_type
|
|
|
|
* The type of $entity; e.g. 'node' or 'user'.
|
|
|
|
* @param $entity
|
2010-02-12 05:38:10 +00:00
|
|
|
* The entity being submitted. The 'bundle', 'id' and (if applicable)
|
2009-07-10 05:58:13 +00:00
|
|
|
* 'revision' keys should be present. The actual field values will be read
|
2009-02-05 03:42:58 +00:00
|
|
|
* from $form_state['values'].
|
|
|
|
* @param $form
|
2010-11-20 19:57:01 +00:00
|
|
|
* The form structure where field elements are attached to. This might be a
|
|
|
|
* full form structure, or a sub-element of a larger form.
|
2009-02-05 03:42:58 +00:00
|
|
|
* @param $form_state
|
|
|
|
* An associative array containing the current state of the form.
|
|
|
|
*/
|
2010-02-11 17:44:47 +00:00
|
|
|
function field_attach_submit($entity_type, $entity, $form, &$form_state) {
|
2009-03-26 13:31:28 +00:00
|
|
|
// Extract field values from submitted values.
|
2010-02-11 17:44:47 +00:00
|
|
|
_field_invoke_default('extract_form_values', $entity_type, $entity, $form, $form_state);
|
2009-03-26 13:31:28 +00:00
|
|
|
|
2010-02-11 17:44:47 +00:00
|
|
|
_field_invoke_default('submit', $entity_type, $entity, $form, $form_state);
|
2009-02-05 03:42:58 +00:00
|
|
|
|
2010-02-12 05:38:10 +00:00
|
|
|
// Let other modules act on submitting the entity.
|
2009-11-08 19:11:56 +00:00
|
|
|
// Avoid module_invoke_all() to let $form_state be taken by reference.
|
2009-02-05 03:42:58 +00:00
|
|
|
foreach (module_implements('field_attach_submit') as $module) {
|
|
|
|
$function = $module . '_field_attach_submit';
|
2010-02-11 17:44:47 +00:00
|
|
|
$function($entity_type, $entity, $form, $form_state);
|
2009-02-05 03:42:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform necessary operations just before fields data get saved.
|
|
|
|
*
|
|
|
|
* We take no specific action here, we just give other
|
|
|
|
* modules the opportunity to act.
|
|
|
|
*
|
2010-02-11 17:44:47 +00:00
|
|
|
* @param $entity_type
|
|
|
|
* The type of $entity; e.g. 'node' or 'user'.
|
|
|
|
* @param $entity
|
2010-02-12 05:38:10 +00:00
|
|
|
* The entity with fields to process.
|
2009-02-05 03:42:58 +00:00
|
|
|
*/
|
2010-02-11 17:44:47 +00:00
|
|
|
function field_attach_presave($entity_type, $entity) {
|
|
|
|
_field_invoke('presave', $entity_type, $entity);
|
2009-02-05 03:42:58 +00:00
|
|
|
|
2010-02-12 05:38:10 +00:00
|
|
|
// Let other modules act on presaving the entity.
|
2010-02-11 17:44:47 +00:00
|
|
|
module_invoke_all('field_attach_presave', $entity_type, $entity);
|
2009-02-05 03:42:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-02-12 05:38:10 +00:00
|
|
|
* Save field data for a new entity.
|
2009-05-01 15:28:13 +00:00
|
|
|
*
|
2011-04-12 20:54:16 +00:00
|
|
|
* The passed-in entity must already contain its id and (if applicable)
|
2009-05-01 15:28:13 +00:00
|
|
|
* revision id attributes.
|
|
|
|
* Default values (if any) will be saved for fields not present in the
|
2010-02-11 17:44:47 +00:00
|
|
|
* $entity.
|
2009-02-05 03:42:58 +00:00
|
|
|
*
|
2010-02-11 17:44:47 +00:00
|
|
|
* @param $entity_type
|
|
|
|
* The type of $entity; e.g. 'node' or 'user'.
|
|
|
|
* @param $entity
|
2010-02-12 05:38:10 +00:00
|
|
|
* The entity with fields to save.
|
2009-05-01 15:28:13 +00:00
|
|
|
* @return
|
2010-02-11 17:44:47 +00:00
|
|
|
* Default values (if any) will be added to the $entity parameter for fields
|
2009-05-01 15:28:13 +00:00
|
|
|
* it leaves unspecified.
|
2009-02-05 03:42:58 +00:00
|
|
|
*/
|
2010-02-11 17:44:47 +00:00
|
|
|
function field_attach_insert($entity_type, $entity) {
|
|
|
|
_field_invoke_default('insert', $entity_type, $entity);
|
|
|
|
_field_invoke('insert', $entity_type, $entity);
|
2009-03-30 03:44:55 +00:00
|
|
|
|
2010-03-27 18:41:14 +00:00
|
|
|
list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
|
- Patch #443422 by yched, bjaspan | chx, merlinofchaos, Scott Reynolds, plach, profix898, mattyoung: added support for pluggable 'per field' storage engine. Comes with documentation and tests.
The Field Attach API uses the Field Storage API to perform all "database access". Each Field Storage API hook function defines a primitive database operation such as read, write, or delete. The default field storage module, field_sql_storage.module, uses the local SQL database to implement these operations, but alternative field storage backends can choose to represent the data in SQL differently or use a completely different storage mechanism such as a cloud-based database.
2009-09-27 12:52:55 +00:00
|
|
|
|
2009-11-08 19:11:56 +00:00
|
|
|
// Let any module insert field data before the storage engine, accumulating
|
|
|
|
// saved fields along the way.
|
2009-05-01 15:28:13 +00:00
|
|
|
$skip_fields = array();
|
2009-11-08 19:11:56 +00:00
|
|
|
foreach (module_implements('field_storage_pre_insert') as $module) {
|
|
|
|
$function = $module . '_field_storage_pre_insert';
|
2010-02-11 17:44:47 +00:00
|
|
|
$function($entity_type, $entity, $skip_fields);
|
2009-02-05 03:42:58 +00:00
|
|
|
}
|
|
|
|
|
2010-02-12 05:38:10 +00:00
|
|
|
// Collect the storage backends used by the remaining fields in the entities.
|
- Patch #443422 by yched, bjaspan | chx, merlinofchaos, Scott Reynolds, plach, profix898, mattyoung: added support for pluggable 'per field' storage engine. Comes with documentation and tests.
The Field Attach API uses the Field Storage API to perform all "database access". Each Field Storage API hook function defines a primitive database operation such as read, write, or delete. The default field storage module, field_sql_storage.module, uses the local SQL database to implement these operations, but alternative field storage backends can choose to represent the data in SQL differently or use a completely different storage mechanism such as a cloud-based database.
2009-09-27 12:52:55 +00:00
|
|
|
$storages = array();
|
2010-02-11 17:44:47 +00:00
|
|
|
foreach (field_info_instances($entity_type, $bundle) as $instance) {
|
- Patch #443422 by yched, bjaspan | chx, merlinofchaos, Scott Reynolds, plach, profix898, mattyoung: added support for pluggable 'per field' storage engine. Comes with documentation and tests.
The Field Attach API uses the Field Storage API to perform all "database access". Each Field Storage API hook function defines a primitive database operation such as read, write, or delete. The default field storage module, field_sql_storage.module, uses the local SQL database to implement these operations, but alternative field storage backends can choose to represent the data in SQL differently or use a completely different storage mechanism such as a cloud-based database.
2009-09-27 12:52:55 +00:00
|
|
|
$field = field_info_field_by_id($instance['field_id']);
|
|
|
|
$field_id = $field['id'];
|
|
|
|
$field_name = $field['field_name'];
|
2010-02-11 17:44:47 +00:00
|
|
|
if (!empty($entity->$field_name)) {
|
- Patch #443422 by yched, bjaspan | chx, merlinofchaos, Scott Reynolds, plach, profix898, mattyoung: added support for pluggable 'per field' storage engine. Comes with documentation and tests.
The Field Attach API uses the Field Storage API to perform all "database access". Each Field Storage API hook function defines a primitive database operation such as read, write, or delete. The default field storage module, field_sql_storage.module, uses the local SQL database to implement these operations, but alternative field storage backends can choose to represent the data in SQL differently or use a completely different storage mechanism such as a cloud-based database.
2009-09-27 12:52:55 +00:00
|
|
|
// Collect the storage backend if the field has not been written yet.
|
|
|
|
if (!isset($skip_fields[$field_id])) {
|
|
|
|
$storages[$field['storage']['type']][$field_id] = $field_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Field storage backends save any remaining unsaved fields.
|
|
|
|
foreach ($storages as $storage => $fields) {
|
|
|
|
$storage_info = field_info_storage_types($storage);
|
2010-02-11 17:44:47 +00:00
|
|
|
module_invoke($storage_info['module'], 'field_storage_write', $entity_type, $entity, FIELD_STORAGE_INSERT, $fields);
|
- Patch #443422 by yched, bjaspan | chx, merlinofchaos, Scott Reynolds, plach, profix898, mattyoung: added support for pluggable 'per field' storage engine. Comes with documentation and tests.
The Field Attach API uses the Field Storage API to perform all "database access". Each Field Storage API hook function defines a primitive database operation such as read, write, or delete. The default field storage module, field_sql_storage.module, uses the local SQL database to implement these operations, but alternative field storage backends can choose to represent the data in SQL differently or use a completely different storage mechanism such as a cloud-based database.
2009-09-27 12:52:55 +00:00
|
|
|
}
|
2009-02-05 03:42:58 +00:00
|
|
|
|
2010-02-12 05:38:10 +00:00
|
|
|
// Let other modules act on inserting the entity.
|
2010-02-11 17:44:47 +00:00
|
|
|
module_invoke_all('field_attach_insert', $entity_type, $entity);
|
2009-11-08 19:11:56 +00:00
|
|
|
|
2010-03-27 18:41:14 +00:00
|
|
|
$entity_info = entity_get_info($entity_type);
|
2009-02-05 03:42:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-02-12 05:38:10 +00:00
|
|
|
* Save field data for an existing entity.
|
2009-02-05 03:42:58 +00:00
|
|
|
*
|
2010-02-11 17:44:47 +00:00
|
|
|
* @param $entity_type
|
|
|
|
* The type of $entity; e.g. 'node' or 'user'.
|
|
|
|
* @param $entity
|
2010-02-12 05:38:10 +00:00
|
|
|
* The entity with fields to save.
|
2009-02-05 03:42:58 +00:00
|
|
|
*/
|
2010-02-11 17:44:47 +00:00
|
|
|
function field_attach_update($entity_type, $entity) {
|
|
|
|
_field_invoke('update', $entity_type, $entity);
|
2009-03-30 03:44:55 +00:00
|
|
|
|
2010-03-27 18:41:14 +00:00
|
|
|
list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
|
- Patch #443422 by yched, bjaspan | chx, merlinofchaos, Scott Reynolds, plach, profix898, mattyoung: added support for pluggable 'per field' storage engine. Comes with documentation and tests.
The Field Attach API uses the Field Storage API to perform all "database access". Each Field Storage API hook function defines a primitive database operation such as read, write, or delete. The default field storage module, field_sql_storage.module, uses the local SQL database to implement these operations, but alternative field storage backends can choose to represent the data in SQL differently or use a completely different storage mechanism such as a cloud-based database.
2009-09-27 12:52:55 +00:00
|
|
|
|
2009-11-08 19:11:56 +00:00
|
|
|
// Let any module update field data before the storage engine, accumulating
|
|
|
|
// saved fields along the way.
|
2009-05-01 15:28:13 +00:00
|
|
|
$skip_fields = array();
|
2009-11-08 19:11:56 +00:00
|
|
|
foreach (module_implements('field_storage_pre_update') as $module) {
|
|
|
|
$function = $module . '_field_storage_pre_update';
|
2010-02-11 17:44:47 +00:00
|
|
|
$function($entity_type, $entity, $skip_fields);
|
2009-02-05 03:42:58 +00:00
|
|
|
}
|
|
|
|
|
2010-02-12 05:38:10 +00:00
|
|
|
// Collect the storage backends used by the remaining fields in the entities.
|
- Patch #443422 by yched, bjaspan | chx, merlinofchaos, Scott Reynolds, plach, profix898, mattyoung: added support for pluggable 'per field' storage engine. Comes with documentation and tests.
The Field Attach API uses the Field Storage API to perform all "database access". Each Field Storage API hook function defines a primitive database operation such as read, write, or delete. The default field storage module, field_sql_storage.module, uses the local SQL database to implement these operations, but alternative field storage backends can choose to represent the data in SQL differently or use a completely different storage mechanism such as a cloud-based database.
2009-09-27 12:52:55 +00:00
|
|
|
$storages = array();
|
2010-02-11 17:44:47 +00:00
|
|
|
foreach (field_info_instances($entity_type, $bundle) as $instance) {
|
- Patch #443422 by yched, bjaspan | chx, merlinofchaos, Scott Reynolds, plach, profix898, mattyoung: added support for pluggable 'per field' storage engine. Comes with documentation and tests.
The Field Attach API uses the Field Storage API to perform all "database access". Each Field Storage API hook function defines a primitive database operation such as read, write, or delete. The default field storage module, field_sql_storage.module, uses the local SQL database to implement these operations, but alternative field storage backends can choose to represent the data in SQL differently or use a completely different storage mechanism such as a cloud-based database.
2009-09-27 12:52:55 +00:00
|
|
|
$field = field_info_field_by_id($instance['field_id']);
|
|
|
|
$field_id = $field['id'];
|
|
|
|
$field_name = $field['field_name'];
|
2010-02-11 17:44:47 +00:00
|
|
|
// Leave the field untouched if $entity comes with no $field_name property,
|
- Patch #443422 by yched, bjaspan | chx, merlinofchaos, Scott Reynolds, plach, profix898, mattyoung: added support for pluggable 'per field' storage engine. Comes with documentation and tests.
The Field Attach API uses the Field Storage API to perform all "database access". Each Field Storage API hook function defines a primitive database operation such as read, write, or delete. The default field storage module, field_sql_storage.module, uses the local SQL database to implement these operations, but alternative field storage backends can choose to represent the data in SQL differently or use a completely different storage mechanism such as a cloud-based database.
2009-09-27 12:52:55 +00:00
|
|
|
// but empty the field if it comes as a NULL value or an empty array.
|
|
|
|
// Function property_exists() is slower, so we catch the more frequent
|
|
|
|
// cases where it's an empty array with the faster isset().
|
2010-02-11 17:44:47 +00:00
|
|
|
if (isset($entity->$field_name) || property_exists($entity, $field_name)) {
|
- Patch #443422 by yched, bjaspan | chx, merlinofchaos, Scott Reynolds, plach, profix898, mattyoung: added support for pluggable 'per field' storage engine. Comes with documentation and tests.
The Field Attach API uses the Field Storage API to perform all "database access". Each Field Storage API hook function defines a primitive database operation such as read, write, or delete. The default field storage module, field_sql_storage.module, uses the local SQL database to implement these operations, but alternative field storage backends can choose to represent the data in SQL differently or use a completely different storage mechanism such as a cloud-based database.
2009-09-27 12:52:55 +00:00
|
|
|
// Collect the storage backend if the field has not been written yet.
|
|
|
|
if (!isset($skip_fields[$field_id])) {
|
|
|
|
$storages[$field['storage']['type']][$field_id] = $field_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Field storage backends save any remaining unsaved fields.
|
|
|
|
foreach ($storages as $storage => $fields) {
|
|
|
|
$storage_info = field_info_storage_types($storage);
|
2010-02-11 17:44:47 +00:00
|
|
|
module_invoke($storage_info['module'], 'field_storage_write', $entity_type, $entity, FIELD_STORAGE_UPDATE, $fields);
|
- Patch #443422 by yched, bjaspan | chx, merlinofchaos, Scott Reynolds, plach, profix898, mattyoung: added support for pluggable 'per field' storage engine. Comes with documentation and tests.
The Field Attach API uses the Field Storage API to perform all "database access". Each Field Storage API hook function defines a primitive database operation such as read, write, or delete. The default field storage module, field_sql_storage.module, uses the local SQL database to implement these operations, but alternative field storage backends can choose to represent the data in SQL differently or use a completely different storage mechanism such as a cloud-based database.
2009-09-27 12:52:55 +00:00
|
|
|
}
|
2009-02-05 03:42:58 +00:00
|
|
|
|
2010-02-12 05:38:10 +00:00
|
|
|
// Let other modules act on updating the entity.
|
2010-02-11 17:44:47 +00:00
|
|
|
module_invoke_all('field_attach_update', $entity_type, $entity);
|
2009-11-08 19:11:56 +00:00
|
|
|
|
2010-03-27 18:41:14 +00:00
|
|
|
$entity_info = entity_get_info($entity_type);
|
|
|
|
if ($entity_info['field cache']) {
|
2011-09-11 16:14:18 +00:00
|
|
|
cache('field')->delete("field:$entity_type:$id");
|
2009-02-05 03:42:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-02-12 05:38:10 +00:00
|
|
|
* Delete field data for an existing entity. This deletes all
|
|
|
|
* revisions of field data for the entity.
|
2009-02-05 03:42:58 +00:00
|
|
|
*
|
2010-02-11 17:44:47 +00:00
|
|
|
* @param $entity_type
|
|
|
|
* The type of $entity; e.g. 'node' or 'user'.
|
|
|
|
* @param $entity
|
2010-02-12 05:38:10 +00:00
|
|
|
* The entity whose field data to delete.
|
2009-02-05 03:42:58 +00:00
|
|
|
*/
|
2010-02-11 17:44:47 +00:00
|
|
|
function field_attach_delete($entity_type, $entity) {
|
|
|
|
_field_invoke('delete', $entity_type, $entity);
|
- Patch #443422 by yched, bjaspan | chx, merlinofchaos, Scott Reynolds, plach, profix898, mattyoung: added support for pluggable 'per field' storage engine. Comes with documentation and tests.
The Field Attach API uses the Field Storage API to perform all "database access". Each Field Storage API hook function defines a primitive database operation such as read, write, or delete. The default field storage module, field_sql_storage.module, uses the local SQL database to implement these operations, but alternative field storage backends can choose to represent the data in SQL differently or use a completely different storage mechanism such as a cloud-based database.
2009-09-27 12:52:55 +00:00
|
|
|
|
2010-03-27 18:41:14 +00:00
|
|
|
list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
|
- Patch #443422 by yched, bjaspan | chx, merlinofchaos, Scott Reynolds, plach, profix898, mattyoung: added support for pluggable 'per field' storage engine. Comes with documentation and tests.
The Field Attach API uses the Field Storage API to perform all "database access". Each Field Storage API hook function defines a primitive database operation such as read, write, or delete. The default field storage module, field_sql_storage.module, uses the local SQL database to implement these operations, but alternative field storage backends can choose to represent the data in SQL differently or use a completely different storage mechanism such as a cloud-based database.
2009-09-27 12:52:55 +00:00
|
|
|
|
2010-02-12 05:38:10 +00:00
|
|
|
// Collect the storage backends used by the fields in the entities.
|
- Patch #443422 by yched, bjaspan | chx, merlinofchaos, Scott Reynolds, plach, profix898, mattyoung: added support for pluggable 'per field' storage engine. Comes with documentation and tests.
The Field Attach API uses the Field Storage API to perform all "database access". Each Field Storage API hook function defines a primitive database operation such as read, write, or delete. The default field storage module, field_sql_storage.module, uses the local SQL database to implement these operations, but alternative field storage backends can choose to represent the data in SQL differently or use a completely different storage mechanism such as a cloud-based database.
2009-09-27 12:52:55 +00:00
|
|
|
$storages = array();
|
2010-02-11 17:44:47 +00:00
|
|
|
foreach (field_info_instances($entity_type, $bundle) as $instance) {
|
- Patch #443422 by yched, bjaspan | chx, merlinofchaos, Scott Reynolds, plach, profix898, mattyoung: added support for pluggable 'per field' storage engine. Comes with documentation and tests.
The Field Attach API uses the Field Storage API to perform all "database access". Each Field Storage API hook function defines a primitive database operation such as read, write, or delete. The default field storage module, field_sql_storage.module, uses the local SQL database to implement these operations, but alternative field storage backends can choose to represent the data in SQL differently or use a completely different storage mechanism such as a cloud-based database.
2009-09-27 12:52:55 +00:00
|
|
|
$field = field_info_field_by_id($instance['field_id']);
|
|
|
|
$field_id = $field['id'];
|
|
|
|
$storages[$field['storage']['type']][$field_id] = $field_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Field storage backends delete their data.
|
|
|
|
foreach ($storages as $storage => $fields) {
|
|
|
|
$storage_info = field_info_storage_types($storage);
|
2010-02-11 17:44:47 +00:00
|
|
|
module_invoke($storage_info['module'], 'field_storage_delete', $entity_type, $entity, $fields);
|
- Patch #443422 by yched, bjaspan | chx, merlinofchaos, Scott Reynolds, plach, profix898, mattyoung: added support for pluggable 'per field' storage engine. Comes with documentation and tests.
The Field Attach API uses the Field Storage API to perform all "database access". Each Field Storage API hook function defines a primitive database operation such as read, write, or delete. The default field storage module, field_sql_storage.module, uses the local SQL database to implement these operations, but alternative field storage backends can choose to represent the data in SQL differently or use a completely different storage mechanism such as a cloud-based database.
2009-09-27 12:52:55 +00:00
|
|
|
}
|
2009-02-05 03:42:58 +00:00
|
|
|
|
2010-02-12 05:38:10 +00:00
|
|
|
// Let other modules act on deleting the entity.
|
2010-02-11 17:44:47 +00:00
|
|
|
module_invoke_all('field_attach_delete', $entity_type, $entity);
|
2009-02-05 03:42:58 +00:00
|
|
|
|
2010-03-27 18:41:14 +00:00
|
|
|
$entity_info = entity_get_info($entity_type);
|
|
|
|
if ($entity_info['field cache']) {
|
2011-09-11 16:14:18 +00:00
|
|
|
cache('field')->delete("field:$entity_type:$id");
|
2009-02-05 03:42:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-02-12 05:38:10 +00:00
|
|
|
* Delete field data for a single revision of an existing entity. The
|
|
|
|
* passed entity must have a revision id attribute.
|
2009-02-05 03:42:58 +00:00
|
|
|
*
|
2010-02-11 17:44:47 +00:00
|
|
|
* @param $entity_type
|
|
|
|
* The type of $entity; e.g. 'node' or 'user'.
|
|
|
|
* @param $entity
|
2010-02-12 05:38:10 +00:00
|
|
|
* The entity with fields to save.
|
2009-02-05 03:42:58 +00:00
|
|
|
*/
|
2010-02-11 17:44:47 +00:00
|
|
|
function field_attach_delete_revision($entity_type, $entity) {
|
|
|
|
_field_invoke('delete_revision', $entity_type, $entity);
|
- Patch #443422 by yched, bjaspan | chx, merlinofchaos, Scott Reynolds, plach, profix898, mattyoung: added support for pluggable 'per field' storage engine. Comes with documentation and tests.
The Field Attach API uses the Field Storage API to perform all "database access". Each Field Storage API hook function defines a primitive database operation such as read, write, or delete. The default field storage module, field_sql_storage.module, uses the local SQL database to implement these operations, but alternative field storage backends can choose to represent the data in SQL differently or use a completely different storage mechanism such as a cloud-based database.
2009-09-27 12:52:55 +00:00
|
|
|
|
2010-03-27 18:41:14 +00:00
|
|
|
list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
|
- Patch #443422 by yched, bjaspan | chx, merlinofchaos, Scott Reynolds, plach, profix898, mattyoung: added support for pluggable 'per field' storage engine. Comes with documentation and tests.
The Field Attach API uses the Field Storage API to perform all "database access". Each Field Storage API hook function defines a primitive database operation such as read, write, or delete. The default field storage module, field_sql_storage.module, uses the local SQL database to implement these operations, but alternative field storage backends can choose to represent the data in SQL differently or use a completely different storage mechanism such as a cloud-based database.
2009-09-27 12:52:55 +00:00
|
|
|
|
2010-02-12 05:38:10 +00:00
|
|
|
// Collect the storage backends used by the fields in the entities.
|
- Patch #443422 by yched, bjaspan | chx, merlinofchaos, Scott Reynolds, plach, profix898, mattyoung: added support for pluggable 'per field' storage engine. Comes with documentation and tests.
The Field Attach API uses the Field Storage API to perform all "database access". Each Field Storage API hook function defines a primitive database operation such as read, write, or delete. The default field storage module, field_sql_storage.module, uses the local SQL database to implement these operations, but alternative field storage backends can choose to represent the data in SQL differently or use a completely different storage mechanism such as a cloud-based database.
2009-09-27 12:52:55 +00:00
|
|
|
$storages = array();
|
2010-02-11 17:44:47 +00:00
|
|
|
foreach (field_info_instances($entity_type, $bundle) as $instance) {
|
- Patch #443422 by yched, bjaspan | chx, merlinofchaos, Scott Reynolds, plach, profix898, mattyoung: added support for pluggable 'per field' storage engine. Comes with documentation and tests.
The Field Attach API uses the Field Storage API to perform all "database access". Each Field Storage API hook function defines a primitive database operation such as read, write, or delete. The default field storage module, field_sql_storage.module, uses the local SQL database to implement these operations, but alternative field storage backends can choose to represent the data in SQL differently or use a completely different storage mechanism such as a cloud-based database.
2009-09-27 12:52:55 +00:00
|
|
|
$field = field_info_field_by_id($instance['field_id']);
|
|
|
|
$field_id = $field['id'];
|
|
|
|
$storages[$field['storage']['type']][$field_id] = $field_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Field storage backends delete their data.
|
|
|
|
foreach ($storages as $storage => $fields) {
|
|
|
|
$storage_info = field_info_storage_types($storage);
|
2010-02-11 17:44:47 +00:00
|
|
|
module_invoke($storage_info['module'], 'field_storage_delete_revision', $entity_type, $entity, $fields);
|
- Patch #443422 by yched, bjaspan | chx, merlinofchaos, Scott Reynolds, plach, profix898, mattyoung: added support for pluggable 'per field' storage engine. Comes with documentation and tests.
The Field Attach API uses the Field Storage API to perform all "database access". Each Field Storage API hook function defines a primitive database operation such as read, write, or delete. The default field storage module, field_sql_storage.module, uses the local SQL database to implement these operations, but alternative field storage backends can choose to represent the data in SQL differently or use a completely different storage mechanism such as a cloud-based database.
2009-09-27 12:52:55 +00:00
|
|
|
}
|
2009-02-05 03:42:58 +00:00
|
|
|
|
|
|
|
// Let other modules act on deleting the revision.
|
2010-02-11 17:44:47 +00:00
|
|
|
module_invoke_all('field_attach_delete_revision', $entity_type, $entity);
|
2009-06-06 16:17:30 +00:00
|
|
|
}
|
|
|
|
|
2009-10-16 03:21:23 +00:00
|
|
|
/**
|
2009-12-31 08:26:59 +00:00
|
|
|
* Prepare field data prior to display.
|
|
|
|
*
|
2010-07-30 03:06:18 +00:00
|
|
|
* This function lets field types and formatters load additional data
|
|
|
|
* needed for display that is not automatically loaded during
|
|
|
|
* field_attach_load(). It accepts an array of entities to allow query
|
|
|
|
* optimisation when displaying lists of entities.
|
|
|
|
*
|
|
|
|
* field_attach_prepare_view() and field_attach_view() are two halves
|
|
|
|
* of the same operation. It is safe to call
|
|
|
|
* field_attach_prepare_view() multiple times on the same entity
|
|
|
|
* before calling field_attach_view() on it, but calling any Field
|
|
|
|
* API operation on an entity between passing that entity to these two
|
|
|
|
* functions may yield incorrect results.
|
2009-12-31 08:26:59 +00:00
|
|
|
*
|
2010-02-11 17:44:47 +00:00
|
|
|
* @param $entity_type
|
|
|
|
* The type of $entities; e.g. 'node' or 'user'.
|
|
|
|
* @param $entities
|
2010-02-12 05:38:10 +00:00
|
|
|
* An array of entities, keyed by entity id.
|
2009-12-31 08:26:59 +00:00
|
|
|
* @param $view_mode
|
|
|
|
* View mode, e.g. 'full', 'teaser'...
|
2011-05-13 14:29:20 +00:00
|
|
|
* @param $langcode
|
|
|
|
* (Optional) The language the field values are to be shown in. If no language
|
|
|
|
* is provided the current language is used.
|
2009-10-16 03:21:23 +00:00
|
|
|
*/
|
2011-05-13 14:29:20 +00:00
|
|
|
function field_attach_prepare_view($entity_type, $entities, $view_mode, $langcode = NULL) {
|
|
|
|
$options = array('language' => array());
|
|
|
|
|
2009-12-31 08:26:59 +00:00
|
|
|
// To ensure hooks are only run once per entity, only process items without
|
|
|
|
// the _field_view_prepared flag.
|
|
|
|
// @todo: resolve this more generally for both entity and field level hooks.
|
|
|
|
$prepare = array();
|
2010-02-11 17:44:47 +00:00
|
|
|
foreach ($entities as $id => $entity) {
|
|
|
|
if (empty($entity->_field_view_prepared)) {
|
2009-12-31 08:26:59 +00:00
|
|
|
// Add this entity to the items to be prepared.
|
2010-02-11 17:44:47 +00:00
|
|
|
$prepare[$id] = $entity;
|
2009-12-31 08:26:59 +00:00
|
|
|
|
2011-05-13 14:29:20 +00:00
|
|
|
// Determine the actual language to display for each field, given the
|
|
|
|
// languages available in the field data.
|
|
|
|
$options['language'][$id] = field_language($entity_type, $entity, NULL, $langcode);
|
|
|
|
|
2009-12-31 08:26:59 +00:00
|
|
|
// Mark this item as prepared.
|
2010-02-11 17:44:47 +00:00
|
|
|
$entity->_field_view_prepared = TRUE;
|
2009-12-31 08:26:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-13 14:29:20 +00:00
|
|
|
$null = NULL;
|
2009-12-13 12:42:28 +00:00
|
|
|
// First let the field types do their preparation.
|
2011-05-13 14:29:20 +00:00
|
|
|
_field_invoke_multiple('prepare_view', $entity_type, $prepare, $null, $null, $options);
|
2009-12-13 12:42:28 +00:00
|
|
|
// Then let the formatters do their own specific massaging.
|
|
|
|
// field_default_prepare_view() takes care of dispatching to the correct
|
2009-12-26 16:50:09 +00:00
|
|
|
// formatters according to the display settings for the view mode.
|
2011-05-13 14:29:20 +00:00
|
|
|
_field_invoke_multiple_default('prepare_view', $entity_type, $prepare, $view_mode, $null, $options);
|
2009-10-16 03:21:23 +00:00
|
|
|
}
|
|
|
|
|
2009-02-05 03:42:58 +00:00
|
|
|
/**
|
2010-02-12 05:38:10 +00:00
|
|
|
* Returns a renderable array for the fields on an entity.
|
2009-12-11 16:49:40 +00:00
|
|
|
*
|
|
|
|
* Each field is displayed according to the display options specified in the
|
2009-12-26 16:50:09 +00:00
|
|
|
* $instance definition for the given $view_mode.
|
2009-12-11 16:49:40 +00:00
|
|
|
*
|
2010-07-30 03:06:18 +00:00
|
|
|
* field_attach_prepare_view() and field_attach_view() are two halves
|
|
|
|
* of the same operation. It is safe to call
|
|
|
|
* field_attach_prepare_view() multiple times on the same entity
|
|
|
|
* before calling field_attach_view() on it, but calling any Field
|
|
|
|
* API operation on an entity between passing that entity to these two
|
|
|
|
* functions may yield incorrect results.
|
2009-12-31 08:26:59 +00:00
|
|
|
*
|
2009-12-11 16:49:40 +00:00
|
|
|
* Sample structure:
|
|
|
|
* @code
|
|
|
|
* array(
|
|
|
|
* 'field_foo' => array(
|
|
|
|
* '#theme' => 'field',
|
|
|
|
* '#title' => the label of the field instance,
|
|
|
|
* '#label_display' => the label display mode,
|
2010-02-12 05:38:10 +00:00
|
|
|
* '#object' => the fieldable entity being displayed,
|
2010-03-27 05:52:50 +00:00
|
|
|
* '#entity_type' => the type of the entity being displayed,
|
2009-12-11 16:49:40 +00:00
|
|
|
* '#language' => the language of the field values being displayed,
|
2009-12-26 16:50:09 +00:00
|
|
|
* '#view_mode' => the view mode,
|
2009-12-11 16:49:40 +00:00
|
|
|
* '#field_name' => the name of the field,
|
|
|
|
* '#field_type' => the type of the field,
|
|
|
|
* '#formatter' => the name of the formatter,
|
|
|
|
* '#items' => the field values being displayed,
|
|
|
|
* // The element's children are the formatted values returned by
|
2009-12-21 13:47:32 +00:00
|
|
|
* // hook_field_formatter_view().
|
2009-12-11 16:49:40 +00:00
|
|
|
* ),
|
|
|
|
* );
|
|
|
|
* @endcode
|
2009-02-05 03:42:58 +00:00
|
|
|
*
|
2010-02-11 17:44:47 +00:00
|
|
|
* @param $entity_type
|
|
|
|
* The type of $entity; e.g. 'node' or 'user'.
|
|
|
|
* @param $entity
|
2010-02-12 05:38:10 +00:00
|
|
|
* The entity with fields to render.
|
2009-12-26 16:50:09 +00:00
|
|
|
* @param $view_mode
|
|
|
|
* View mode, e.g. 'full', 'teaser'...
|
2009-08-22 00:58:55 +00:00
|
|
|
* @param $langcode
|
|
|
|
* The language the field values are to be shown in. If no language is
|
|
|
|
* provided the current language is used.
|
2009-02-05 03:42:58 +00:00
|
|
|
* @return
|
2009-12-11 16:49:40 +00:00
|
|
|
* A renderable array for the field values.
|
2009-02-05 03:42:58 +00:00
|
|
|
*/
|
2010-05-23 19:10:23 +00:00
|
|
|
function field_attach_view($entity_type, $entity, $view_mode, $langcode = NULL) {
|
2010-03-25 11:46:21 +00:00
|
|
|
// Determine the actual language to display for each field, given the
|
|
|
|
// languages available in the field data.
|
|
|
|
$display_language = field_language($entity_type, $entity, NULL, $langcode);
|
|
|
|
$options = array('language' => $display_language);
|
|
|
|
|
|
|
|
// Invoke field_default_view().
|
2009-08-22 00:58:55 +00:00
|
|
|
$null = NULL;
|
2010-02-11 17:44:47 +00:00
|
|
|
$output = _field_invoke_default('view', $entity_type, $entity, $view_mode, $null, $options);
|
2009-02-05 03:42:58 +00:00
|
|
|
|
2009-08-19 13:31:14 +00:00
|
|
|
// Add custom weight handling.
|
2010-02-11 17:44:47 +00:00
|
|
|
list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
|
2010-05-23 19:10:23 +00:00
|
|
|
$output['#pre_render'][] = '_field_extra_fields_pre_render';
|
|
|
|
$output['#entity_type'] = $entity_type;
|
|
|
|
$output['#bundle'] = $bundle;
|
2009-08-19 13:31:14 +00:00
|
|
|
|
2009-12-20 21:08:26 +00:00
|
|
|
// Let other modules alter the renderable array.
|
2009-10-13 16:38:43 +00:00
|
|
|
$context = array(
|
2010-03-27 05:52:50 +00:00
|
|
|
'entity_type' => $entity_type,
|
|
|
|
'entity' => $entity,
|
2009-12-26 16:50:09 +00:00
|
|
|
'view_mode' => $view_mode,
|
2010-12-07 05:09:58 +00:00
|
|
|
'display' => $view_mode,
|
2010-10-03 01:15:34 +00:00
|
|
|
'language' => $langcode,
|
2009-10-13 16:38:43 +00:00
|
|
|
);
|
|
|
|
drupal_alter('field_attach_view', $output, $context);
|
2009-02-05 03:42:58 +00:00
|
|
|
|
2009-12-31 08:26:59 +00:00
|
|
|
// Reset the _field_view_prepared flag set in field_attach_prepare_view(),
|
2010-02-12 05:38:10 +00:00
|
|
|
// in case the same entity is displayed with different settings later in
|
2009-12-31 08:26:59 +00:00
|
|
|
// the request.
|
2010-02-11 17:44:47 +00:00
|
|
|
unset($entity->_field_view_prepared);
|
2009-12-31 08:26:59 +00:00
|
|
|
|
2009-02-05 03:42:58 +00:00
|
|
|
return $output;
|
2009-08-22 00:58:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Populate the template variables with the field values available for rendering.
|
|
|
|
*
|
|
|
|
* The $variables array will be populated with all the field instance values
|
|
|
|
* associated with the given entity type, keyed by field name; in case of
|
|
|
|
* translatable fields the language currently chosen for display will be
|
|
|
|
* selected.
|
|
|
|
*
|
2010-02-11 17:44:47 +00:00
|
|
|
* @param $entity_type
|
|
|
|
* The type of $entity; e.g. 'node' or 'user'.
|
|
|
|
* @param $entity
|
2010-02-12 05:38:10 +00:00
|
|
|
* The entity with fields to render.
|
2009-08-22 00:58:55 +00:00
|
|
|
* @param $element
|
|
|
|
* The structured array containing the values ready for rendering.
|
|
|
|
* @param $variables
|
|
|
|
* The variables array is passed by reference and will be populated with field
|
|
|
|
* values.
|
|
|
|
*/
|
2010-02-11 17:44:47 +00:00
|
|
|
function field_attach_preprocess($entity_type, $entity, $element, &$variables) {
|
|
|
|
list(, , $bundle) = entity_extract_ids($entity_type, $entity);
|
2009-08-22 00:58:55 +00:00
|
|
|
|
2010-02-11 17:44:47 +00:00
|
|
|
foreach (field_info_instances($entity_type, $bundle) as $instance) {
|
2009-08-22 00:58:55 +00:00
|
|
|
$field_name = $instance['field_name'];
|
|
|
|
if (isset($element[$field_name]['#language'])) {
|
|
|
|
$langcode = $element[$field_name]['#language'];
|
2010-02-11 17:44:47 +00:00
|
|
|
$variables[$field_name] = isset($entity->{$field_name}[$langcode]) ? $entity->{$field_name}[$langcode] : NULL;
|
2009-08-22 00:58:55 +00:00
|
|
|
}
|
|
|
|
}
|
2009-02-05 03:42:58 +00:00
|
|
|
|
2009-08-22 00:58:55 +00:00
|
|
|
// Let other modules make changes to the $variables array.
|
2009-10-13 16:38:43 +00:00
|
|
|
$context = array(
|
2010-03-27 05:52:50 +00:00
|
|
|
'entity_type' => $entity_type,
|
|
|
|
'entity' => $entity,
|
2009-10-13 16:38:43 +00:00
|
|
|
'element' => $element,
|
|
|
|
);
|
|
|
|
drupal_alter('field_attach_preprocess', $variables, $context);
|
2009-02-05 03:42:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-06-06 00:24:16 +00:00
|
|
|
* Prepares an entity for translation.
|
2009-02-05 03:42:58 +00:00
|
|
|
*
|
2010-06-06 00:24:16 +00:00
|
|
|
* This function is used to fill-in the form default values for Field API fields
|
|
|
|
* while performing entity translation. By default it copies all the source
|
|
|
|
* values in the given source language to the new entity and assigns them the
|
|
|
|
* target language.
|
|
|
|
*
|
|
|
|
* This is used as part of the 'per entity' translation pattern, which is
|
|
|
|
* implemented only for nodes by translation.module. Other entity types may be
|
|
|
|
* supported through contributed modules.
|
|
|
|
*
|
|
|
|
* @param $entity_type
|
|
|
|
* The type of $entity; e.g. 'node' or 'user'.
|
|
|
|
* @param $entity
|
|
|
|
* The entity to be prepared for translation.
|
|
|
|
* @param $langcode
|
|
|
|
* The language the entity has to be translated in.
|
|
|
|
* @param $source_entity
|
|
|
|
* The source entity holding the field values to be translated.
|
|
|
|
* @param $source_langcode
|
|
|
|
* The source language from which translate.
|
2009-02-05 03:42:58 +00:00
|
|
|
*/
|
2010-06-06 00:24:16 +00:00
|
|
|
function field_attach_prepare_translation($entity_type, $entity, $langcode, $source_entity, $source_langcode) {
|
|
|
|
$options = array('language' => $langcode);
|
|
|
|
// Copy source field values into the entity to be prepared.
|
|
|
|
_field_invoke_default('prepare_translation', $entity_type, $entity, $source_entity, $source_langcode, $options);
|
|
|
|
// Let field types handle their own advanced translation pattern if needed.
|
|
|
|
_field_invoke('prepare_translation', $entity_type, $entity, $source_entity, $source_langcode, $options);
|
|
|
|
// Let other modules alter the entity translation.
|
|
|
|
$context = array(
|
|
|
|
'entity_type' => $entity_type,
|
|
|
|
'langcode' => $langcode,
|
|
|
|
'source_entity' => $source_entity,
|
|
|
|
'source_langcode' => $source_langcode,
|
|
|
|
);
|
|
|
|
drupal_alter('field_attach_prepare_translation', $entity, $context);
|
2009-02-05 03:42:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notify field.module that a new bundle was created.
|
|
|
|
*
|
2009-10-15 12:44:36 +00:00
|
|
|
* The default SQL-based storage doesn't need to do anything about it, but
|
2009-02-05 03:42:58 +00:00
|
|
|
* others might.
|
|
|
|
*
|
2010-02-11 17:44:47 +00:00
|
|
|
* @param $entity_type
|
2010-02-12 05:38:10 +00:00
|
|
|
* The entity type to which the bundle is bound.
|
2009-02-05 03:42:58 +00:00
|
|
|
* @param $bundle
|
|
|
|
* The name of the newly created bundle.
|
|
|
|
*/
|
2010-02-11 17:44:47 +00:00
|
|
|
function field_attach_create_bundle($entity_type, $bundle) {
|
2009-02-05 03:42:58 +00:00
|
|
|
// Clear the cache.
|
|
|
|
field_cache_clear();
|
2009-08-23 03:54:47 +00:00
|
|
|
|
2009-11-08 19:11:56 +00:00
|
|
|
// Let other modules act on creating the bundle.
|
2010-02-11 17:44:47 +00:00
|
|
|
module_invoke_all('field_attach_create_bundle', $entity_type, $bundle);
|
2009-02-05 03:42:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notify field.module that a bundle was renamed.
|
|
|
|
*
|
2010-02-11 17:44:47 +00:00
|
|
|
* @param $entity_type
|
2010-02-12 05:38:10 +00:00
|
|
|
* The entity type to which the bundle is bound.
|
2009-02-05 03:42:58 +00:00
|
|
|
* @param $bundle_old
|
|
|
|
* The previous name of the bundle.
|
|
|
|
* @param $bundle_new
|
|
|
|
* The new name of the bundle.
|
|
|
|
*/
|
2010-02-11 17:44:47 +00:00
|
|
|
function field_attach_rename_bundle($entity_type, $bundle_old, $bundle_new) {
|
2009-02-05 03:42:58 +00:00
|
|
|
db_update('field_config_instance')
|
|
|
|
->fields(array('bundle' => $bundle_new))
|
2010-03-27 05:52:50 +00:00
|
|
|
->condition('entity_type', $entity_type)
|
2009-02-05 03:42:58 +00:00
|
|
|
->condition('bundle', $bundle_old)
|
|
|
|
->execute();
|
|
|
|
|
|
|
|
// Clear the cache.
|
|
|
|
field_cache_clear();
|
2011-12-20 05:59:16 +00:00
|
|
|
entity_info_cache_clear();
|
2009-02-05 03:42:58 +00:00
|
|
|
|
2010-05-23 19:10:23 +00:00
|
|
|
// Update bundle settings.
|
2011-10-13 20:12:38 +00:00
|
|
|
$settings = variable_get('field_bundle_settings_' . $entity_type . '__' . $bundle_old, array());
|
|
|
|
variable_set('field_bundle_settings_' . $entity_type . '__' . $bundle_new, $settings);
|
|
|
|
variable_del('field_bundle_settings_' . $entity_type . '__' . $bundle_old);
|
2010-05-23 19:10:23 +00:00
|
|
|
|
2009-11-08 19:11:56 +00:00
|
|
|
// Let other modules act on renaming the bundle.
|
2010-02-11 17:44:47 +00:00
|
|
|
module_invoke_all('field_attach_rename_bundle', $entity_type, $bundle_old, $bundle_new);
|
2009-02-05 03:42:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notify field.module the a bundle was deleted.
|
|
|
|
*
|
|
|
|
* This deletes the data for the field instances as well as the field instances
|
2009-02-10 03:16:15 +00:00
|
|
|
* themselves. This function actually just marks the data and field instances
|
2009-02-05 03:42:58 +00:00
|
|
|
* and deleted, leaving the garbage collection for a separate process, because
|
|
|
|
* it is not always possible to delete this much data in a single page request
|
|
|
|
* (particularly since for some field types, the deletion is more than just a
|
|
|
|
* simple DELETE query).
|
|
|
|
*
|
2010-02-11 17:44:47 +00:00
|
|
|
* @param $entity_type
|
2010-02-12 05:38:10 +00:00
|
|
|
* The entity type to which the bundle is bound.
|
2009-02-05 03:42:58 +00:00
|
|
|
* @param $bundle
|
|
|
|
* The bundle to delete.
|
|
|
|
*/
|
2010-02-11 17:44:47 +00:00
|
|
|
function field_attach_delete_bundle($entity_type, $bundle) {
|
2011-06-14 08:26:10 +00:00
|
|
|
// First, delete the instances themselves. field_read_instances() must be
|
|
|
|
// used here since field_info_instances() does not return instances for
|
|
|
|
// disabled entity types or bundles.
|
|
|
|
$instances = field_read_instances(array('entity_type' => $entity_type, 'bundle' => $bundle), array('include_inactive' => 1));
|
2009-02-05 03:42:58 +00:00
|
|
|
foreach ($instances as $instance) {
|
2009-10-02 14:39:43 +00:00
|
|
|
field_delete_instance($instance);
|
2009-02-05 03:42:58 +00:00
|
|
|
}
|
2009-04-26 09:18:20 +00:00
|
|
|
|
- Patch #443422 by yched, bjaspan | chx, merlinofchaos, Scott Reynolds, plach, profix898, mattyoung: added support for pluggable 'per field' storage engine. Comes with documentation and tests.
The Field Attach API uses the Field Storage API to perform all "database access". Each Field Storage API hook function defines a primitive database operation such as read, write, or delete. The default field storage module, field_sql_storage.module, uses the local SQL database to implement these operations, but alternative field storage backends can choose to represent the data in SQL differently or use a completely different storage mechanism such as a cloud-based database.
2009-09-27 12:52:55 +00:00
|
|
|
// Clear the cache.
|
|
|
|
field_cache_clear();
|
|
|
|
|
2010-05-23 19:10:23 +00:00
|
|
|
// Clear bundle display settings.
|
2011-10-13 20:12:38 +00:00
|
|
|
variable_del('field_bundle_settings_' . $entity_type . '__' . $bundle);
|
2010-05-23 19:10:23 +00:00
|
|
|
|
2009-04-30 14:41:41 +00:00
|
|
|
// Let other modules act on deleting the bundle.
|
2010-02-11 17:44:47 +00:00
|
|
|
module_invoke_all('field_attach_delete_bundle', $entity_type, $bundle, $instances);
|
2009-02-05 03:42:58 +00:00
|
|
|
}
|
|
|
|
|
2009-06-06 16:17:30 +00:00
|
|
|
|
2009-02-05 03:42:58 +00:00
|
|
|
/**
|
2009-06-05 18:25:41 +00:00
|
|
|
* @} End of "defgroup field_attach"
|
2009-02-05 03:42:58 +00:00
|
|
|
*/
|