cms/drupal/modules/field/field.module
changeset 541 e756a8c72c3d
equal deleted inserted replaced
540:07239de796bb 541:e756a8c72c3d
       
     1 <?php
       
     2 /**
       
     3  * @file
       
     4  * Attach custom data fields to Drupal entities.
       
     5  */
       
     6 
       
     7 /**
       
     8  * Base class for all exceptions thrown by Field API functions.
       
     9  *
       
    10  * This class has no functionality of its own other than allowing all
       
    11  * Field API exceptions to be caught by a single catch block.
       
    12  */
       
    13 class FieldException extends Exception {}
       
    14 
       
    15 /*
       
    16  * Load all public Field API functions. Drupal currently has no
       
    17  * mechanism for auto-loading core APIs, so we have to load them on
       
    18  * every page request.
       
    19  */
       
    20 require_once DRUPAL_ROOT . '/modules/field/field.crud.inc';
       
    21 require_once DRUPAL_ROOT . '/modules/field/field.default.inc';
       
    22 require_once DRUPAL_ROOT . '/modules/field/field.info.inc';
       
    23 require_once DRUPAL_ROOT . '/modules/field/field.multilingual.inc';
       
    24 require_once DRUPAL_ROOT . '/modules/field/field.attach.inc';
       
    25 require_once DRUPAL_ROOT . '/modules/field/field.form.inc';
       
    26 
       
    27 /**
       
    28  * @defgroup field Field API
       
    29  * @{
       
    30  * Attach custom data fields to Drupal entities.
       
    31  *
       
    32  * The Field API allows custom data fields to be attached to Drupal
       
    33  * entities and takes care of storing, loading, editing, and rendering
       
    34  * field data. Any entity type (node, user, etc.) can use the Field
       
    35  * API to make itself "fieldable" and thus allow fields to be attached
       
    36  * to it. Other modules can provide a user interface for managing custom
       
    37  * fields via a web browser as well as a wide and flexible variety of
       
    38  * data type, form element, and display format capabilities.
       
    39  *
       
    40  * The Field API defines two primary data structures, Field and
       
    41  * Instance, and the concept of a Bundle. A Field defines a
       
    42  * particular type of data that can be attached to entities. A Field
       
    43  * Instance is a Field attached to a single Bundle. A Bundle is a set
       
    44  * of fields that are treated as a group by the Field Attach API and
       
    45  * is related to a single fieldable entity type.
       
    46  *
       
    47  * For example, suppose a site administrator wants Article nodes to
       
    48  * have a subtitle and photo. Using the Field API or Field UI module,
       
    49  * the administrator creates a field named 'subtitle' of type 'text'
       
    50  * and a field named 'photo' of type 'image'. The administrator
       
    51  * (again, via a UI) creates two Field Instances, one attaching the
       
    52  * field 'subtitle' to the 'node' bundle 'article' and one attaching
       
    53  * the field 'photo' to the 'node' bundle 'article'. When the node
       
    54  * system uses the Field Attach API to load all fields for an Article
       
    55  * node, it passes the node's entity type (which is 'node') and
       
    56  * content type (which is 'article') as the node's bundle.
       
    57  * field_attach_load() then loads the 'subtitle' and 'photo' fields
       
    58  * because they are both attached to the 'node' bundle 'article'.
       
    59  *
       
    60  * Field definitions are represented as an array of key/value pairs.
       
    61  *
       
    62  * array $field:
       
    63  * - id (integer, read-only): The primary identifier of the field. It is
       
    64  *   assigned automatically by field_create_field().
       
    65  * - field_name (string): The name of the field. Each field name is unique
       
    66  *   within Field API. When a field is attached to an entity, the field's data
       
    67  *   is stored in $entity->$field_name. Maximum length is 32 characters.
       
    68  * - type (string): The type of the field, such as 'text' or 'image'. Field
       
    69  *   types are defined by modules that implement hook_field_info().
       
    70  * - entity_types (array): The array of entity types that can hold instances
       
    71  *   of this field. If empty or not specified, the field can have instances
       
    72  *   in any entity type.
       
    73  * - cardinality (integer): The number of values the field can hold. Legal
       
    74  *   values are any positive integer or FIELD_CARDINALITY_UNLIMITED.
       
    75  * - translatable (integer): Whether the field is translatable.
       
    76  * - locked (integer): Whether or not the field is available for editing. If
       
    77  *   TRUE, users can't change field settings or create new instances of the
       
    78  *   field in the UI. Defaults to FALSE.
       
    79  * - module (string, read-only): The name of the module that implements the
       
    80  *   field type.
       
    81  * - active (integer, read-only): TRUE if the module that implements the field
       
    82  *   type is currently enabled, FALSE otherwise.
       
    83  * - deleted (integer, read-only): TRUE if this field has been deleted, FALSE
       
    84  *   otherwise. Deleted fields are ignored by the Field Attach API. This
       
    85  *   property exists because fields can be marked for deletion but only
       
    86  *   actually destroyed by a separate garbage-collection process.
       
    87  * - columns (array, read-only): An array of the Field API columns used to
       
    88  *   store each value of this field. The column list may depend on field
       
    89  *   settings; it is not constant per field type. Field API column
       
    90  *   specifications are exactly like Schema API column specifications but,
       
    91  *   depending on the field storage module in use, the name of the column may
       
    92  *   not represent an actual column in an SQL database.
       
    93  * - indexes (array): An array of indexes on data columns, using the same
       
    94  *   definition format as Schema API index specifications. Only columns that
       
    95  *   appear in the 'columns' setting are allowed. Note that field types can
       
    96  *   specify default indexes, which can be modified or added to when
       
    97  *   creating a field.
       
    98  * - foreign keys: (optional) An associative array of relations, using the same
       
    99  *   structure as the 'foreign keys' definition of hook_schema(). Note,
       
   100  *   however, that the field data is not necessarily stored in SQL. Also, the
       
   101  *   possible usage is limited, as you cannot specify another field as
       
   102  *   related, only existing SQL tables, such as filter formats.
       
   103  * - settings (array): A sub-array of key/value pairs of field-type-specific
       
   104  *   settings. Each field type module defines and documents its own field
       
   105  *   settings.
       
   106  * - storage (array): A sub-array of key/value pairs identifying the storage
       
   107  *   backend to use for the for the field:
       
   108  *   - type (string): The storage backend used by the field. Storage backends
       
   109  *     are defined by modules that implement hook_field_storage_info().
       
   110  *   - module (string, read-only): The name of the module that implements the
       
   111  *     storage backend.
       
   112  *   - active (integer, read-only): TRUE if the module that implements the
       
   113  *     storage backend is currently enabled, FALSE otherwise.
       
   114  *   - settings (array): A sub-array of key/value pairs of settings. Each
       
   115  *     storage backend defines and documents its own settings.
       
   116  *
       
   117  * Field instance definitions are represented as an array of key/value pairs.
       
   118  *
       
   119  * array $instance:
       
   120  * - id (integer, read-only): The primary identifier of this field instance.
       
   121  *   It is assigned automatically by field_create_instance().
       
   122  * - field_id (integer, read-only): The foreign key of the field attached to
       
   123  *   the bundle by this instance. It is populated automatically by
       
   124  *   field_create_instance().
       
   125  * - field_name (string): The name of the field attached to the bundle by this
       
   126  *   instance.
       
   127  * - entity_type (string): The name of the entity type the instance is attached
       
   128  *   to.
       
   129  * - bundle (string): The name of the bundle that the field is attached to.
       
   130  * - label (string): A human-readable label for the field when used with this
       
   131  *   bundle. For example, the label will be the title of Form API elements
       
   132  *   for this instance.
       
   133  * - description (string): A human-readable description for the field when
       
   134  *   used with this bundle. For example, the description will be the help
       
   135  *   text of Form API elements for this instance.
       
   136  * - required (integer): TRUE if a value for this field is required when used
       
   137  *   with this bundle, FALSE otherwise. Currently, required-ness is only
       
   138  *   enforced during Form API operations, not by field_attach_load(),
       
   139  *   field_attach_insert(), or field_attach_update().
       
   140  * - default_value_function (string): The name of the function, if any, that
       
   141  *   will provide a default value.
       
   142  * - default_value (array): If default_value_function is not set, then fixed
       
   143  *   values can be provided.
       
   144  * - deleted (integer, read-only): TRUE if this instance has been deleted,
       
   145  *   FALSE otherwise. Deleted instances are ignored by the Field Attach API.
       
   146  *   This property exists because instances can be marked for deletion but
       
   147  *   only actually destroyed by a separate garbage-collection process.
       
   148  * - settings (array): A sub-array of key/value pairs of field-type-specific
       
   149  *   instance settings. Each field type module defines and documents its own
       
   150  *   instance settings.
       
   151  * - widget (array): A sub-array of key/value pairs identifying the Form API
       
   152  *   input widget for the field when used by this bundle:
       
   153  *   - type (string): The type of the widget, such as text_textfield. Widget
       
   154  *     types are defined by modules that implement hook_field_widget_info().
       
   155  *   - settings (array): A sub-array of key/value pairs of
       
   156  *     widget-type-specific settings. Each field widget type module defines
       
   157  *     and documents its own widget settings.
       
   158  *   - weight (float): The weight of the widget relative to the other elements
       
   159  *     in entity edit forms.
       
   160  *   - module (string, read-only): The name of the module that implements the
       
   161  *     widget type.
       
   162  * - display (array): A sub-array of key/value pairs identifying the way field
       
   163  *   values should be displayed in each of the entity type's view modes, plus
       
   164  *   the 'default' mode. For each view mode, Field UI lets site administrators
       
   165  *   define whether they want to use a dedicated set of display options or the
       
   166  *   'default' options to reduce the number of displays to maintain as they
       
   167  *   add new fields. For nodes, on a fresh install, only the 'teaser' view
       
   168  *   mode is configured to use custom display options, all other view modes
       
   169  *   defined use the 'default' options by default. When programmatically
       
   170  *   adding field instances on nodes, it is therefore recommended to at least
       
   171  *   specify display options for 'default' and 'teaser':
       
   172  *   - default (array): A sub-array of key/value pairs describing the display
       
   173  *     options to be used when the field is being displayed in view modes
       
   174  *     that are not configured to use dedicated display options:
       
   175  *     - label (string): Position of the label. 'inline', 'above' and
       
   176  *       'hidden' are the values recognized by the default 'field' theme
       
   177  *       implementation.
       
   178  *     - type (string): The type of the display formatter, or 'hidden' for
       
   179  *       no display.
       
   180  *     - settings (array): A sub-array of key/value pairs of display
       
   181  *       options specific to the formatter.
       
   182  *     - weight (float): The weight of the field relative to the other entity
       
   183  *       components displayed in this view mode.
       
   184  *     - module (string, read-only): The name of the module which implements
       
   185  *       the display formatter.
       
   186  *   - some_mode: A sub-array of key/value pairs describing the display
       
   187  *     options to be used when the field is being displayed in the 'some_mode'
       
   188  *     view mode. Those options will only be actually applied at run time if
       
   189  *     the view mode is not configured to use default settings for this bundle:
       
   190  *     - ...
       
   191  *   - other_mode:
       
   192  *     - ...
       
   193  *
       
   194  * The (default) render arrays produced for field instances are documented at
       
   195  * field_attach_view().
       
   196  *
       
   197  * Bundles are represented by two strings, an entity type and a bundle name.
       
   198  *
       
   199  * - @link field_types Field Types API @endlink. Defines field types,
       
   200  *   widget types, and display formatters. Field modules use this API
       
   201  *   to provide field types like Text and Node Reference along with the
       
   202  *   associated form elements and display formatters.
       
   203  *
       
   204  * - @link field_crud Field CRUD API @endlink. Create, updates, and
       
   205  *   deletes fields, bundles (a.k.a. "content types"), and instances.
       
   206  *   Modules use this API, often in hook_install(), to create
       
   207  *   custom data structures.
       
   208  *
       
   209  * - @link field_attach Field Attach API @endlink. Connects entity
       
   210  *   types to the Field API. Field Attach API functions load, store,
       
   211  *   generate Form API structures, display, and perform a variety of
       
   212  *   other functions for field data connected to individual entities.
       
   213  *   Fieldable entity types like node and user use this API to make
       
   214  *   themselves fieldable.
       
   215  *
       
   216  * - @link field_info Field Info API @endlink. Exposes information
       
   217  *   about all fields, instances, widgets, and related information
       
   218  *   defined by or with the Field API.
       
   219  *
       
   220  * - @link field_storage Field Storage API @endlink. Provides a
       
   221  *   pluggable back-end storage system for actual field data. The
       
   222  *   default implementation, field_sql_storage.module, stores field data
       
   223  *   in the local SQL database.
       
   224  *
       
   225  * - @link field_purge Field API bulk data deletion @endlink. Cleans
       
   226  *   up after bulk deletion operations such as field_delete_field()
       
   227  *   and field_delete_instance().
       
   228  *
       
   229  * - @link field_language Field language API @endlink. Provides native
       
   230  *   multilingual support for the Field API.
       
   231  */
       
   232 
       
   233 /**
       
   234  * Value for field API indicating a field accepts an unlimited number of values.
       
   235  */
       
   236 define('FIELD_CARDINALITY_UNLIMITED', -1);
       
   237 
       
   238 /**
       
   239  * Value for field API indicating a widget doesn't accept default values.
       
   240  *
       
   241  * @see hook_field_widget_info()
       
   242  */
       
   243 define('FIELD_BEHAVIOR_NONE', 0x0001);
       
   244 
       
   245 /**
       
   246  * Value for field API concerning widget default and multiple value settings.
       
   247  *
       
   248  * @see hook_field_widget_info()
       
   249  *
       
   250  * When used in a widget default context, indicates the widget accepts default
       
   251  * values. When used in a multiple value context for a widget that allows the
       
   252  * input of one single field value, indicates that the widget will be repeated
       
   253  * for each value input.
       
   254  */
       
   255 define('FIELD_BEHAVIOR_DEFAULT', 0x0002);
       
   256 
       
   257 /**
       
   258  * Value for field API indicating a widget can receive several field values.
       
   259  *
       
   260  * @see hook_field_widget_info()
       
   261  */
       
   262 define('FIELD_BEHAVIOR_CUSTOM', 0x0004);
       
   263 
       
   264 /**
       
   265  * Age argument for loading the most recent version of an entity's
       
   266  * field data with field_attach_load().
       
   267  */
       
   268 define('FIELD_LOAD_CURRENT', 'FIELD_LOAD_CURRENT');
       
   269 
       
   270 /**
       
   271  * Age argument for loading the version of an entity's field data
       
   272  * specified in the entity with field_attach_load().
       
   273  */
       
   274 define('FIELD_LOAD_REVISION', 'FIELD_LOAD_REVISION');
       
   275 
       
   276 /**
       
   277  * Exception class thrown by hook_field_update_forbid().
       
   278  */
       
   279 class FieldUpdateForbiddenException extends FieldException {}
       
   280 
       
   281 /**
       
   282  * Implements hook_help().
       
   283  */
       
   284 function field_help($path, $arg) {
       
   285   switch ($path) {
       
   286     case 'admin/help#field':
       
   287       $output = '';
       
   288       $output .= '<h3>' . t('About') . '</h3>';
       
   289       $output .= '<p>' . t('The Field module allows custom data fields to be defined for <em>entity</em> types (entities include content items, comments, user accounts, and taxonomy terms). The Field module takes care of storing, loading, editing, and rendering field data. Most users will not interact with the Field module directly, but will instead use the <a href="@field-ui-help">Field UI module</a> user interface. Module developers can use the Field API to make new entity types "fieldable" and thus allow fields to be attached to them. For more information, see the online handbook entry for <a href="@field">Field module</a>.', array('@field-ui-help' => url('admin/help/field_ui'), '@field' => 'http://drupal.org/documentation/modules/field')) . '</p>';
       
   290       $output .= '<h3>' . t('Uses') . '</h3>';
       
   291       $output .= '<dl>';
       
   292       $output .= '<dt>' . t('Enabling field types') . '</dt>';
       
   293       $output .= '<dd>' . t('The Field module provides the infrastructure for fields and field attachment; the field types and input widgets themselves are provided by additional modules. Some of the modules are required; the optional modules can be enabled from the <a href="@modules">Modules administration page</a>. Drupal core includes the following field type modules: Number (required), Text (required), List (required), Taxonomy (optional), Image (optional), and File (optional); the required Options module provides input widgets for other field modules. Additional fields and widgets may be provided by contributed modules, which you can find in the <a href="@contrib">contributed module section of Drupal.org</a>. Currently enabled field and input widget modules:', array('@modules' => url('admin/modules'), '@contrib' => 'http://drupal.org/project/modules', '@options' => url('admin/help/options')));
       
   294 
       
   295       // Make a list of all widget and field modules currently enabled, in
       
   296       // order by displayed module name (module names are not translated).
       
   297       $items = array();
       
   298       $info = system_get_info('module');
       
   299       $modules = array_merge(module_implements('field_info'), module_implements('field_widget_info'));
       
   300       $modules = array_unique($modules);
       
   301       sort($modules);
       
   302       foreach ($modules as $module) {
       
   303         $display = $info[$module]['name'];
       
   304         if (module_hook($module, 'help')) {
       
   305           $items['items'][] = l($display, 'admin/help/' . $module);
       
   306         }
       
   307         else {
       
   308           $items['items'][] = $display;
       
   309         }
       
   310       }
       
   311       $output .= theme('item_list', $items) . '</dd>';
       
   312       $output .= '<dt>' . t('Managing field data storage') . '</dt>';
       
   313       $output .= '<dd>' . t('Developers of field modules can either use the default <a href="@sql-store">Field SQL storage module</a> to store data for their fields, or a contributed or custom module developed using the <a href="@storage-api">field storage API</a>.', array('@storage-api' => 'http://api.drupal.org/api/group/field_storage/7', '@sql-store' => url('admin/help/field_sql_storage'))) . '</dd>';
       
   314       $output .= '</dl>';
       
   315       return $output;
       
   316   }
       
   317 }
       
   318 
       
   319 /**
       
   320  * Implements hook_permission().
       
   321  */
       
   322 function field_permission() {
       
   323   return array(
       
   324     'administer fields' => array(
       
   325       'title' => t('Administer fields'),
       
   326       'description' => t('Additional permissions are required based on what the fields are attached to (for example, <a href="@url">administer content types</a> to manage fields attached to content).', array(
       
   327         '@url' => '#module-node',
       
   328       )),
       
   329       'restrict access' => TRUE,
       
   330     ),
       
   331   );
       
   332 }
       
   333 
       
   334 /**
       
   335  * Implements hook_theme().
       
   336  */
       
   337 function field_theme() {
       
   338   return array(
       
   339     'field' => array(
       
   340       'render element' => 'element',
       
   341     ),
       
   342     'field_multiple_value_form' => array(
       
   343       'render element' => 'element',
       
   344     ),
       
   345   );
       
   346 }
       
   347 
       
   348 /**
       
   349  * Implements hook_cron().
       
   350  */
       
   351 function field_cron() {
       
   352   // Refresh the 'active' status of fields.
       
   353   field_sync_field_status();
       
   354 
       
   355   // Do a pass of purging on deleted Field API data, if any exists.
       
   356   $limit = variable_get('field_purge_batch_size', 10);
       
   357   field_purge_batch($limit);
       
   358 }
       
   359 
       
   360 /**
       
   361  * Implements hook_system_info_alter().
       
   362  *
       
   363  * Goes through a list of all modules that provide a field type, and makes them
       
   364  * required if there are any active fields of that type.
       
   365  */
       
   366 function field_system_info_alter(&$info, $file, $type) {
       
   367   if ($type == 'module' && module_hook($file->name, 'field_info')) {
       
   368     $fields = field_read_fields(array('module' => $file->name), array('include_deleted' => TRUE));
       
   369     if ($fields) {
       
   370       $info['required'] = TRUE;
       
   371 
       
   372       // Provide an explanation message (only mention pending deletions if there
       
   373       // remains no actual, non-deleted fields)
       
   374       $non_deleted = FALSE;
       
   375       foreach ($fields as $field) {
       
   376         if (empty($field['deleted'])) {
       
   377           $non_deleted = TRUE;
       
   378           break;
       
   379         }
       
   380       }
       
   381       if ($non_deleted) {
       
   382         if (module_exists('field_ui')) {
       
   383           $explanation = t('Field type(s) in use - see <a href="@fields-page">Field list</a>', array('@fields-page' => url('admin/reports/fields')));
       
   384         }
       
   385         else {
       
   386           $explanation = t('Fields type(s) in use');
       
   387         }
       
   388       }
       
   389       else {
       
   390         $explanation = t('Fields pending deletion');
       
   391       }
       
   392       $info['explanation'] = $explanation;
       
   393     }
       
   394   }
       
   395 }
       
   396 
       
   397 /**
       
   398  * Implements hook_flush_caches().
       
   399  */
       
   400 function field_flush_caches() {
       
   401   // Refresh the 'active' status of fields.
       
   402   field_sync_field_status();
       
   403 
       
   404   // Request a flush of our cache table.
       
   405   return array('cache_field');
       
   406 }
       
   407 
       
   408 /**
       
   409  * Implements hook_modules_enabled().
       
   410  */
       
   411 function field_modules_enabled($modules) {
       
   412   // Refresh the 'active' status of fields.
       
   413   field_sync_field_status();
       
   414 }
       
   415 
       
   416 /**
       
   417  * Implements hook_modules_disabled().
       
   418  */
       
   419 function field_modules_disabled($modules) {
       
   420   // Refresh the 'active' status of fields.
       
   421   field_sync_field_status();
       
   422 }
       
   423 
       
   424 /**
       
   425  * Refreshes the 'active' and 'storage_active' columns for fields.
       
   426  */
       
   427 function field_sync_field_status() {
       
   428   // Refresh the 'active' and 'storage_active' columns according to the current
       
   429   // set of enabled modules.
       
   430   $modules = module_list();
       
   431   foreach ($modules as $module_name) {
       
   432     field_associate_fields($module_name);
       
   433   }
       
   434   db_update('field_config')
       
   435     ->fields(array('active' => 0))
       
   436     ->condition('module', $modules, 'NOT IN')
       
   437     ->execute();
       
   438   db_update('field_config')
       
   439     ->fields(array('storage_active' => 0))
       
   440     ->condition('storage_module', $modules, 'NOT IN')
       
   441     ->execute();
       
   442 }
       
   443 
       
   444 /**
       
   445  * Allows a module to update the database for fields and columns it controls.
       
   446  *
       
   447  * @param $module
       
   448  *   The name of the module to update on.
       
   449  */
       
   450 function field_associate_fields($module) {
       
   451   // Associate field types.
       
   452   $field_types = (array) module_invoke($module, 'field_info');
       
   453   if ($field_types) {
       
   454     db_update('field_config')
       
   455       ->fields(array('module' => $module, 'active' => 1))
       
   456       ->condition('type', array_keys($field_types))
       
   457       ->execute();
       
   458   }
       
   459   // Associate storage backends.
       
   460   $storage_types = (array) module_invoke($module, 'field_storage_info');
       
   461   if ($storage_types) {
       
   462     db_update('field_config')
       
   463       ->fields(array('storage_module' => $module, 'storage_active' => 1))
       
   464       ->condition('storage_type', array_keys($storage_types))
       
   465       ->execute();
       
   466   }
       
   467 }
       
   468 
       
   469 /**
       
   470  * Helper function to get the default value for a field on an entity.
       
   471  *
       
   472  * @param $entity_type
       
   473  *   The type of $entity; e.g., 'node' or 'user'.
       
   474  * @param $entity
       
   475  *   The entity for the operation.
       
   476  * @param $field
       
   477  *   The field structure.
       
   478  * @param $instance
       
   479  *   The instance structure.
       
   480  * @param $langcode
       
   481  *   The field language to fill-in with the default value.
       
   482  */
       
   483 function field_get_default_value($entity_type, $entity, $field, $instance, $langcode = NULL) {
       
   484   $items = array();
       
   485   if (!empty($instance['default_value_function'])) {
       
   486     $function = $instance['default_value_function'];
       
   487     if (function_exists($function)) {
       
   488       $items = $function($entity_type, $entity, $field, $instance, $langcode);
       
   489     }
       
   490   }
       
   491   elseif (!empty($instance['default_value'])) {
       
   492     $items = $instance['default_value'];
       
   493   }
       
   494   return $items;
       
   495 }
       
   496 
       
   497 /**
       
   498  * Helper function to filter out empty field values.
       
   499  *
       
   500  * @param $field
       
   501  *   The field definition.
       
   502  * @param $items
       
   503  *   The field values to filter.
       
   504  *
       
   505  * @return
       
   506  *   The array of items without empty field values. The function also renumbers
       
   507  *   the array keys to ensure sequential deltas.
       
   508  */
       
   509 function _field_filter_items($field, $items) {
       
   510   $function = $field['module'] . '_field_is_empty';
       
   511   foreach ((array) $items as $delta => $item) {
       
   512     // Explicitly break if the function is undefined.
       
   513     if ($function($item, $field)) {
       
   514       unset($items[$delta]);
       
   515     }
       
   516   }
       
   517   return array_values($items);
       
   518 }
       
   519 
       
   520 /**
       
   521  * Helper function to sort items in a field according to
       
   522  * user drag-n-drop reordering.
       
   523  */
       
   524 function _field_sort_items($field, $items) {
       
   525   if (($field['cardinality'] > 1 || $field['cardinality'] == FIELD_CARDINALITY_UNLIMITED) && isset($items[0]['_weight'])) {
       
   526     usort($items, '_field_sort_items_helper');
       
   527     foreach ($items as $delta => $item) {
       
   528       if (is_array($items[$delta])) {
       
   529         unset($items[$delta]['_weight']);
       
   530       }
       
   531     }
       
   532   }
       
   533   return $items;
       
   534 }
       
   535 
       
   536 /**
       
   537  * Sort function for items order.
       
   538  * (copied form element_sort(), which acts on #weight keys)
       
   539  */
       
   540 function _field_sort_items_helper($a, $b) {
       
   541   $a_weight = (is_array($a) ? $a['_weight'] : 0);
       
   542   $b_weight = (is_array($b) ? $b['_weight'] : 0);
       
   543   return $a_weight - $b_weight;
       
   544 }
       
   545 
       
   546 /**
       
   547  * Same as above, using ['_weight']['#value']
       
   548  */
       
   549 function _field_sort_items_value_helper($a, $b) {
       
   550   $a_weight = (is_array($a) && isset($a['_weight']['#value']) ? $a['_weight']['#value'] : 0);
       
   551   $b_weight = (is_array($b) && isset($b['_weight']['#value']) ? $b['_weight']['#value'] : 0);
       
   552   return $a_weight - $b_weight;
       
   553 }
       
   554 
       
   555 /**
       
   556  * Gets or sets administratively defined bundle settings.
       
   557  *
       
   558  * @param string $entity_type
       
   559  *   The type of $entity; e.g., 'node' or 'user'.
       
   560  * @param string $bundle
       
   561  *   The bundle name.
       
   562  * @param array|null $settings
       
   563  *   (optional) The settings to store, an associative array with the following
       
   564  *   elements:
       
   565  *   - view_modes: An associative array keyed by view mode, with the following
       
   566  *     key/value pairs:
       
   567  *     - custom_settings: Boolean specifying whether the view mode uses a
       
   568  *       dedicated set of display options (TRUE), or the 'default' options
       
   569  *       (FALSE). Defaults to FALSE.
       
   570  *   - extra_fields: An associative array containing the form and display
       
   571  *     settings for extra fields (also known as pseudo-fields):
       
   572  *     - form: An associative array whose keys are the names of extra fields,
       
   573  *       and whose values are associative arrays with the following elements:
       
   574  *       - weight: The weight of the extra field, determining its position on an
       
   575  *         entity form.
       
   576  *     - display: An associative array whose keys are the names of extra fields,
       
   577  *       and whose values are associative arrays keyed by the name of view
       
   578  *       modes. This array must include an item for the 'default' view mode.
       
   579  *       Each view mode sub-array contains the following elements:
       
   580  *       - weight: The weight of the extra field, determining its position when
       
   581  *         an entity is viewed.
       
   582  *       - visible: TRUE if the extra field is visible, FALSE otherwise.
       
   583  *
       
   584  * @return array|null
       
   585  *   If no $settings are passed, the current settings are returned.
       
   586  */
       
   587 function field_bundle_settings($entity_type, $bundle, $settings = NULL) {
       
   588   if (isset($settings)) {
       
   589     variable_set('field_bundle_settings_' . $entity_type . '__' . $bundle, $settings);
       
   590     field_info_cache_clear();
       
   591   }
       
   592   else {
       
   593     $settings = variable_get('field_bundle_settings_' . $entity_type . '__' . $bundle, array());
       
   594     $settings += array(
       
   595       'view_modes' => array(),
       
   596       'extra_fields' => array(),
       
   597     );
       
   598     $settings['extra_fields'] += array(
       
   599       'form' => array(),
       
   600       'display' => array(),
       
   601     );
       
   602 
       
   603     return $settings;
       
   604   }
       
   605 }
       
   606 
       
   607 /**
       
   608  * Returns view mode settings in a given bundle.
       
   609  *
       
   610  * @param $entity_type
       
   611  *   The type of entity; e.g. 'node' or 'user'.
       
   612  * @param $bundle
       
   613  *   The bundle name to return view mode settings for.
       
   614  *
       
   615  * @return
       
   616  *   An array keyed by view mode, with the following key/value pairs:
       
   617  *   - custom_settings: Boolean specifying whether the view mode uses a
       
   618  *     dedicated set of display options (TRUE), or the 'default' options
       
   619  *     (FALSE). Defaults to FALSE.
       
   620  */
       
   621 function field_view_mode_settings($entity_type, $bundle) {
       
   622   $cache = &drupal_static(__FUNCTION__, array());
       
   623 
       
   624   if (!isset($cache[$entity_type][$bundle])) {
       
   625     $bundle_settings = field_bundle_settings($entity_type, $bundle);
       
   626     $settings = $bundle_settings['view_modes'];
       
   627     // Include view modes for which nothing has been stored yet, but whose
       
   628     // definition in hook_entity_info() specify they should use custom settings
       
   629     // by default.
       
   630     $entity_info = entity_get_info($entity_type);
       
   631     foreach ($entity_info['view modes'] as $view_mode => $view_mode_info) {
       
   632       if (!isset($settings[$view_mode]['custom_settings']) && $view_mode_info['custom settings']) {
       
   633         $settings[$view_mode]['custom_settings'] = TRUE;
       
   634       }
       
   635     }
       
   636     $cache[$entity_type][$bundle] = $settings;
       
   637   }
       
   638 
       
   639   return $cache[$entity_type][$bundle];
       
   640 }
       
   641 
       
   642 /**
       
   643  * Returns the display settings to use for an instance in a given view mode.
       
   644  *
       
   645  * @param $instance
       
   646  *   The field instance being displayed.
       
   647  * @param $view_mode
       
   648  *   The view mode.
       
   649  * @param $entity
       
   650  *   The entity being displayed.
       
   651  *
       
   652  * @return
       
   653  *   The display settings to be used when displaying the field values.
       
   654  */
       
   655 function field_get_display($instance, $view_mode, $entity) {
       
   656   // Check whether the view mode uses custom display settings or the 'default'
       
   657   // mode.
       
   658   $view_mode_settings = field_view_mode_settings($instance['entity_type'], $instance['bundle']);
       
   659   $actual_mode = (!empty($view_mode_settings[$view_mode]['custom_settings']) ? $view_mode : 'default');
       
   660   $display = $instance['display'][$actual_mode];
       
   661 
       
   662   // Let modules alter the display settings.
       
   663   $context = array(
       
   664     'entity_type' => $instance['entity_type'],
       
   665     'field' => field_info_field($instance['field_name']),
       
   666     'instance' => $instance,
       
   667     'entity' => $entity,
       
   668     'view_mode' => $view_mode,
       
   669   );
       
   670   drupal_alter(array('field_display', 'field_display_' . $instance['entity_type']), $display, $context);
       
   671 
       
   672   return $display;
       
   673 }
       
   674 
       
   675 /**
       
   676  * Returns the display settings to use for pseudo-fields in a given view mode.
       
   677  *
       
   678  * @param $entity_type
       
   679  *   The type of $entity; e.g., 'node' or 'user'.
       
   680  * @param $bundle
       
   681  *   The bundle name.
       
   682  * @param $view_mode
       
   683  *   The view mode.
       
   684  *
       
   685  * @return
       
   686  *   The display settings to be used when viewing the bundle's pseudo-fields.
       
   687  */
       
   688 function field_extra_fields_get_display($entity_type, $bundle, $view_mode) {
       
   689   // Check whether the view mode uses custom display settings or the 'default'
       
   690   // mode.
       
   691   $view_mode_settings = field_view_mode_settings($entity_type, $bundle);
       
   692   $actual_mode = (!empty($view_mode_settings[$view_mode]['custom_settings'])) ? $view_mode : 'default';
       
   693   $extra_fields = field_info_extra_fields($entity_type, $bundle, 'display');
       
   694 
       
   695   $displays = array();
       
   696   foreach ($extra_fields as $name => $value) {
       
   697     $displays[$name] = $extra_fields[$name]['display'][$actual_mode];
       
   698   }
       
   699 
       
   700   // Let modules alter the display settings.
       
   701   $context = array(
       
   702     'entity_type' => $entity_type,
       
   703     'bundle' => $bundle,
       
   704     'view_mode' => $view_mode,
       
   705   );
       
   706   drupal_alter('field_extra_fields_display', $displays, $context);
       
   707 
       
   708   return $displays;
       
   709 }
       
   710 
       
   711 /**
       
   712  * Pre-render callback to adjust weights and visibility of non-field elements.
       
   713  */
       
   714 function _field_extra_fields_pre_render($elements) {
       
   715   $entity_type = $elements['#entity_type'];
       
   716   $bundle = $elements['#bundle'];
       
   717 
       
   718   if (isset($elements['#type']) && $elements['#type'] == 'form') {
       
   719     $extra_fields = field_info_extra_fields($entity_type, $bundle, 'form');
       
   720     foreach ($extra_fields as $name => $settings) {
       
   721       if (isset($elements[$name])) {
       
   722         $elements[$name]['#weight'] = $settings['weight'];
       
   723       }
       
   724     }
       
   725   }
       
   726   elseif (isset($elements['#view_mode'])) {
       
   727     $view_mode = $elements['#view_mode'];
       
   728     $extra_fields = field_extra_fields_get_display($entity_type, $bundle, $view_mode);
       
   729     foreach ($extra_fields as $name => $settings) {
       
   730       if (isset($elements[$name])) {
       
   731         $elements[$name]['#weight'] = $settings['weight'];
       
   732         // Visibility: make sure we do not accidentally show a hidden element.
       
   733         $elements[$name]['#access'] = isset($elements[$name]['#access']) ? ($elements[$name]['#access'] && $settings['visible']) : $settings['visible'];
       
   734       }
       
   735     }
       
   736   }
       
   737 
       
   738   return $elements;
       
   739 }
       
   740 
       
   741 /**
       
   742  * Clear the field info and field data caches.
       
   743  */
       
   744 function field_cache_clear() {
       
   745   cache_clear_all('*', 'cache_field', TRUE);
       
   746   field_info_cache_clear();
       
   747 }
       
   748 
       
   749 /**
       
   750  * Like filter_xss_admin(), but with a shorter list of allowed tags.
       
   751  *
       
   752  * Used for items entered by administrators, like field descriptions,
       
   753  * allowed values, where some (mainly inline) mark-up may be desired
       
   754  * (so check_plain() is not acceptable).
       
   755  */
       
   756 function field_filter_xss($string) {
       
   757   return filter_xss($string, _field_filter_xss_allowed_tags());
       
   758 }
       
   759 
       
   760 /**
       
   761  * List of tags allowed by field_filter_xss().
       
   762  */
       
   763 function _field_filter_xss_allowed_tags() {
       
   764   return array('a', 'b', 'big',  'code', 'del', 'em', 'i', 'ins',  'pre', 'q', 'small', 'span', 'strong', 'sub', 'sup', 'tt', 'ol', 'ul', 'li', 'p', 'br', 'img');
       
   765 }
       
   766 
       
   767 /**
       
   768  * Human-readable list of allowed tags, for display in help texts.
       
   769  */
       
   770 function _field_filter_xss_display_allowed_tags() {
       
   771   return '<' . implode('> <', _field_filter_xss_allowed_tags()) . '>';
       
   772 }
       
   773 
       
   774 /**
       
   775  * Returns a renderable array for a single field value.
       
   776  *
       
   777  * @param $entity_type
       
   778  *   The type of $entity; e.g., 'node' or 'user'.
       
   779  * @param $entity
       
   780  *   The entity containing the field to display. Must at least contain the id
       
   781  *   key and the field data to display.
       
   782  * @param $field_name
       
   783  *   The name of the field to display.
       
   784  * @param $item
       
   785  *   The field value to display, as found in
       
   786  *   $entity->field_name[$langcode][$delta].
       
   787  * @param $display
       
   788  *   Can be either the name of a view mode, or an array of display settings.
       
   789  *   See field_view_field() for more information.
       
   790  * @param $langcode
       
   791  *   (Optional) The language of the value in $item. If not provided, the
       
   792  *   current language will be assumed.
       
   793  * @return
       
   794  *   A renderable array for the field value.
       
   795  */
       
   796 function field_view_value($entity_type, $entity, $field_name, $item, $display = array(), $langcode = NULL) {
       
   797   $output = array();
       
   798 
       
   799   if ($field = field_info_field($field_name)) {
       
   800     // Determine the langcode that will be used by language fallback.
       
   801     $langcode = field_language($entity_type, $entity, $field_name, $langcode);
       
   802 
       
   803     // Push the item as the single value for the field, and defer to
       
   804     // field_view_field() to build the render array for the whole field.
       
   805     $clone = clone $entity;
       
   806     $clone->{$field_name}[$langcode] = array($item);
       
   807     $elements = field_view_field($entity_type, $clone, $field_name, $display, $langcode);
       
   808 
       
   809     // Extract the part of the render array we need.
       
   810     $output = isset($elements[0]) ? $elements[0] : array();
       
   811     if (isset($elements['#access'])) {
       
   812       $output['#access'] = $elements['#access'];
       
   813     }
       
   814   }
       
   815 
       
   816   return $output;
       
   817 }
       
   818 
       
   819 /**
       
   820  * Returns a renderable array for the value of a single field in an entity.
       
   821  *
       
   822  * The resulting output is a fully themed field with label and multiple values.
       
   823  *
       
   824  * This function can be used by third-party modules that need to output an
       
   825  * isolated field.
       
   826  * - Do not use inside node (or any other entity) templates; use
       
   827  *   render($content[FIELD_NAME]) instead.
       
   828  * - Do not use to display all fields in an entity; use
       
   829  *   field_attach_prepare_view() and field_attach_view() instead.
       
   830  * - The field_view_value() function can be used to output a single formatted
       
   831  *   field value, without label or wrapping field markup.
       
   832  *
       
   833  * The function takes care of invoking the prepare_view steps. It also respects
       
   834  * field access permissions.
       
   835  *
       
   836  * @param $entity_type
       
   837  *   The type of $entity; e.g., 'node' or 'user'.
       
   838  * @param $entity
       
   839  *   The entity containing the field to display. Must at least contain the id
       
   840  *   key and the field data to display.
       
   841  * @param $field_name
       
   842  *   The name of the field to display.
       
   843  * @param $display
       
   844  *   Can be either:
       
   845  *   - The name of a view mode. The field will be displayed according to the
       
   846  *     display settings specified for this view mode in the $instance
       
   847  *     definition for the field in the entity's bundle.
       
   848  *     If no display settings are found for the view mode, the settings for
       
   849  *     the 'default' view mode will be used.
       
   850  *   - An array of display settings, as found in the 'display' entry of
       
   851  *     $instance definitions. The following key/value pairs are allowed:
       
   852  *     - label: (string) Position of the label. The default 'field' theme
       
   853  *       implementation supports the values 'inline', 'above' and 'hidden'.
       
   854  *       Defaults to 'above'.
       
   855  *     - type: (string) The formatter to use. Defaults to the
       
   856  *       'default_formatter' for the field type, specified in
       
   857  *       hook_field_info(). The default formatter will also be used if the
       
   858  *       requested formatter is not available.
       
   859  *     - settings: (array) Settings specific to the formatter. Defaults to the
       
   860  *       formatter's default settings, specified in
       
   861  *       hook_field_formatter_info().
       
   862  *     - weight: (float) The weight to assign to the renderable element.
       
   863  *       Defaults to 0.
       
   864  * @param $langcode
       
   865  *   (Optional) The language the field values are to be shown in. The site's
       
   866  *   current language fallback logic will be applied no values are available
       
   867  *   for the language. If no language is provided the current language will be
       
   868  *   used.
       
   869  * @return
       
   870  *   A renderable array for the field value.
       
   871  *
       
   872  * @see field_view_value()
       
   873  */
       
   874 function field_view_field($entity_type, $entity, $field_name, $display = array(), $langcode = NULL) {
       
   875   $output = array();
       
   876 
       
   877   if ($field = field_info_field($field_name)) {
       
   878     if (is_array($display)) {
       
   879       // When using custom display settings, fill in default values.
       
   880       $cache = _field_info_field_cache();
       
   881       $display = $cache->prepareInstanceDisplay($display, $field["type"]);
       
   882     }
       
   883 
       
   884     // Hook invocations are done through the _field_invoke() functions in
       
   885     // 'single field' mode, to reuse the language fallback logic.
       
   886     // Determine the actual language to display for the field, given the
       
   887     // languages available in the field data.
       
   888     $display_language = field_language($entity_type, $entity, $field_name, $langcode);
       
   889     $options = array('field_name' => $field_name, 'language' => $display_language);
       
   890     $null = NULL;
       
   891 
       
   892     // Invoke prepare_view steps if needed.
       
   893     if (empty($entity->_field_view_prepared)) {
       
   894       list($id) = entity_extract_ids($entity_type, $entity);
       
   895 
       
   896       // First let the field types do their preparation.
       
   897       _field_invoke_multiple('prepare_view', $entity_type, array($id => $entity), $display, $null, $options);
       
   898       // Then let the formatters do their own specific massaging.
       
   899       _field_invoke_multiple_default('prepare_view', $entity_type, array($id => $entity), $display, $null, $options);
       
   900     }
       
   901 
       
   902     // Build the renderable array.
       
   903     $result = _field_invoke_default('view', $entity_type, $entity, $display, $null, $options);
       
   904 
       
   905     // Invoke hook_field_attach_view_alter() to let other modules alter the
       
   906     // renderable array, as in a full field_attach_view() execution.
       
   907     $context = array(
       
   908       'entity_type' => $entity_type,
       
   909       'entity' => $entity,
       
   910       'view_mode' => '_custom',
       
   911       'display' => $display,
       
   912       'language' => $langcode,
       
   913     );
       
   914     drupal_alter('field_attach_view', $result, $context);
       
   915 
       
   916     if (isset($result[$field_name])) {
       
   917       $output = $result[$field_name];
       
   918     }
       
   919   }
       
   920 
       
   921   return $output;
       
   922 }
       
   923 
       
   924 /**
       
   925  * Returns the field items in the language they currently would be displayed.
       
   926  *
       
   927  * @param $entity_type
       
   928  *   The type of $entity; e.g., 'node' or 'user'.
       
   929  * @param $entity
       
   930  *   The entity containing the data to be displayed.
       
   931  * @param $field_name
       
   932  *   The field to be displayed.
       
   933  * @param $langcode
       
   934  *   (optional) The language code $entity->{$field_name} has to be displayed in.
       
   935  *   Defaults to the current language.
       
   936  *
       
   937  * @return
       
   938  *   An array of field items keyed by delta if available, FALSE otherwise.
       
   939  */
       
   940 function field_get_items($entity_type, $entity, $field_name, $langcode = NULL) {
       
   941   $langcode = field_language($entity_type, $entity, $field_name, $langcode);
       
   942   return isset($entity->{$field_name}[$langcode]) ? $entity->{$field_name}[$langcode] : FALSE;
       
   943 }
       
   944 
       
   945 /**
       
   946  * Determine whether a field has any data.
       
   947  *
       
   948  * @param $field
       
   949  *   A field structure.
       
   950  * @return
       
   951  *   TRUE if the field has data for any entity; FALSE otherwise.
       
   952  */
       
   953 function field_has_data($field) {
       
   954   $query = new EntityFieldQuery();
       
   955   $query = $query->fieldCondition($field)
       
   956     ->range(0, 1)
       
   957     ->count()
       
   958     // Neutralize the 'entity_field_access' query tag added by
       
   959     // field_sql_storage_field_storage_query(). The result cannot depend on the
       
   960     // access grants of the current user.
       
   961     ->addTag('DANGEROUS_ACCESS_CHECK_OPT_OUT');
       
   962 
       
   963   return (bool) $query
       
   964     ->execute() || (bool) $query
       
   965     ->age(FIELD_LOAD_REVISION)
       
   966     ->execute();
       
   967 }
       
   968 
       
   969 /**
       
   970  * Determine whether the user has access to a given field.
       
   971  *
       
   972  * This function does not determine whether access is granted to the entity
       
   973  * itself, only the specific field. Callers are responsible for ensuring that
       
   974  * entity access is also respected. For example, when checking field access for
       
   975  * nodes, check node_access() before checking field_access(), and when checking
       
   976  * field access for entities using the Entity API contributed module,
       
   977  * check entity_access() before checking field_access().
       
   978  *
       
   979  * @param $op
       
   980  *   The operation to be performed. Possible values:
       
   981  *   - 'edit'
       
   982  *   - 'view'
       
   983  * @param $field
       
   984  *   The full field structure array for the field on which the operation is to
       
   985  *   be performed. See field_info_field().
       
   986  * @param $entity_type
       
   987  *   The type of $entity; e.g., 'node' or 'user'.
       
   988  * @param $entity
       
   989  *   (optional) The entity for the operation.
       
   990  * @param $account
       
   991  *   (optional) The account to check, if not given use currently logged in user.
       
   992  *
       
   993  * @return
       
   994  *   TRUE if the operation is allowed;
       
   995  *   FALSE if the operation is denied.
       
   996  */
       
   997 function field_access($op, $field, $entity_type, $entity = NULL, $account = NULL) {
       
   998   global $user;
       
   999 
       
  1000   if (!isset($account)) {
       
  1001     $account = $user;
       
  1002   }
       
  1003 
       
  1004   foreach (module_implements('field_access') as $module) {
       
  1005     $function = $module . '_field_access';
       
  1006     $access = $function($op, $field, $entity_type, $entity, $account);
       
  1007     if ($access === FALSE) {
       
  1008       return FALSE;
       
  1009     }
       
  1010   }
       
  1011   return TRUE;
       
  1012 }
       
  1013 
       
  1014 /**
       
  1015  * Helper function to extract the bundle name of from a bundle object.
       
  1016  *
       
  1017  * @param $entity_type
       
  1018  *   The type of $entity; e.g., 'node' or 'user'.
       
  1019  * @param $bundle
       
  1020  *   The bundle object (or string if bundles for this entity type do not exist
       
  1021  *   as standalone objects).
       
  1022  * @return
       
  1023  *   The bundle name.
       
  1024  */
       
  1025 function field_extract_bundle($entity_type, $bundle) {
       
  1026   if (is_string($bundle)) {
       
  1027     return $bundle;
       
  1028   }
       
  1029 
       
  1030   $info = entity_get_info($entity_type);
       
  1031   if (is_object($bundle) && isset($info['bundle keys']['bundle']) && isset($bundle->{$info['bundle keys']['bundle']})) {
       
  1032     return $bundle->{$info['bundle keys']['bundle']};
       
  1033   }
       
  1034 }
       
  1035 
       
  1036 /**
       
  1037  * Theme preprocess function for theme_field() and field.tpl.php.
       
  1038  *
       
  1039  * @see theme_field()
       
  1040  * @see field.tpl.php
       
  1041  */
       
  1042 function template_preprocess_field(&$variables, $hook) {
       
  1043   $element = $variables['element'];
       
  1044 
       
  1045   // There's some overhead in calling check_plain() so only call it if the label
       
  1046   // variable is being displayed. Otherwise, set it to NULL to avoid PHP
       
  1047   // warnings if a theme implementation accesses the variable even when it's
       
  1048   // supposed to be hidden. If a theme implementation needs to print a hidden
       
  1049   // label, it needs to supply a preprocess function that sets it to the
       
  1050   // sanitized element title or whatever else is wanted in its place.
       
  1051   $variables['label_hidden'] = ($element['#label_display'] == 'hidden');
       
  1052   $variables['label'] = $variables['label_hidden'] ? NULL : check_plain($element['#title']);
       
  1053 
       
  1054   // We want other preprocess functions and the theme implementation to have
       
  1055   // fast access to the field item render arrays. The item render array keys
       
  1056   // (deltas) should always be a subset of the keys in #items, and looping on
       
  1057   // those keys is faster than calling element_children() or looping on all keys
       
  1058   // within $element, since that requires traversal of all element properties.
       
  1059   $variables['items'] = array();
       
  1060   foreach ($element['#items'] as $delta => $item) {
       
  1061     if (!empty($element[$delta])) {
       
  1062       $variables['items'][$delta] = $element[$delta];
       
  1063     }
       
  1064   }
       
  1065 
       
  1066   // Add default CSS classes. Since there can be many fields rendered on a page,
       
  1067   // save some overhead by calling strtr() directly instead of
       
  1068   // drupal_html_class().
       
  1069   $variables['field_name_css'] = strtr($element['#field_name'], '_', '-');
       
  1070   $variables['field_type_css'] = strtr($element['#field_type'], '_', '-');
       
  1071   $variables['classes_array'] = array(
       
  1072     'field',
       
  1073     'field-name-' . $variables['field_name_css'],
       
  1074     'field-type-' . $variables['field_type_css'],
       
  1075     'field-label-' . $element['#label_display'],
       
  1076   );
       
  1077   // Add a "clearfix" class to the wrapper since we float the label and the
       
  1078   // field items in field.css if the label is inline.
       
  1079   if ($element['#label_display'] == 'inline') {
       
  1080     $variables['classes_array'][] = 'clearfix';
       
  1081   }
       
  1082 
       
  1083   // Add specific suggestions that can override the default implementation.
       
  1084   $variables['theme_hook_suggestions'] = array(
       
  1085     'field__' . $element['#field_type'],
       
  1086     'field__' . $element['#field_name'],
       
  1087     'field__' . $element['#bundle'],
       
  1088     'field__' . $element['#field_name'] . '__' . $element['#bundle'],
       
  1089   );
       
  1090 }
       
  1091 
       
  1092 /**
       
  1093  * Theme process function for theme_field() and field.tpl.php.
       
  1094  *
       
  1095  * @see theme_field()
       
  1096  * @see field.tpl.php
       
  1097  */
       
  1098 function template_process_field(&$variables, $hook) {
       
  1099   // The default theme implementation is a function, so template_process() does
       
  1100   // not automatically run, so we need to flatten the classes and attributes
       
  1101   // here. For best performance, only call drupal_attributes() when needed, and
       
  1102   // note that template_preprocess_field() does not initialize the
       
  1103   // *_attributes_array variables.
       
  1104   $variables['classes'] = implode(' ', $variables['classes_array']);
       
  1105   $variables['attributes'] = empty($variables['attributes_array']) ? '' : drupal_attributes($variables['attributes_array']);
       
  1106   $variables['title_attributes'] = empty($variables['title_attributes_array']) ? '' : drupal_attributes($variables['title_attributes_array']);
       
  1107   $variables['content_attributes'] = empty($variables['content_attributes_array']) ? '' : drupal_attributes($variables['content_attributes_array']);
       
  1108   foreach ($variables['items'] as $delta => $item) {
       
  1109     $variables['item_attributes'][$delta] = empty($variables['item_attributes_array'][$delta]) ? '' : drupal_attributes($variables['item_attributes_array'][$delta]);
       
  1110   }
       
  1111 }
       
  1112 /**
       
  1113  * @} End of "defgroup field".
       
  1114  */
       
  1115 
       
  1116 /**
       
  1117  * Returns HTML for a field.
       
  1118  *
       
  1119  * This is the default theme implementation to display the value of a field.
       
  1120  * Theme developers who are comfortable with overriding theme functions may do
       
  1121  * so in order to customize this markup. This function can be overridden with
       
  1122  * varying levels of specificity. For example, for a field named 'body'
       
  1123  * displayed on the 'article' content type, any of the following functions will
       
  1124  * override this default implementation. The first of these functions that
       
  1125  * exists is used:
       
  1126  * - THEMENAME_field__body__article()
       
  1127  * - THEMENAME_field__article()
       
  1128  * - THEMENAME_field__body()
       
  1129  * - THEMENAME_field()
       
  1130  *
       
  1131  * Theme developers who prefer to customize templates instead of overriding
       
  1132  * functions may copy the "field.tpl.php" from the "modules/field/theme" folder
       
  1133  * of the Drupal installation to somewhere within the theme's folder and
       
  1134  * customize it, just like customizing other Drupal templates such as
       
  1135  * page.tpl.php or node.tpl.php. However, it takes longer for the server to
       
  1136  * process templates than to call a function, so for websites with many fields
       
  1137  * displayed on a page, this can result in a noticeable slowdown of the website.
       
  1138  * For these websites, developers are discouraged from placing a field.tpl.php
       
  1139  * file into the theme's folder, but may customize templates for specific
       
  1140  * fields. For example, for a field named 'body' displayed on the 'article'
       
  1141  * content type, any of the following templates will override this default
       
  1142  * implementation. The first of these templates that exists is used:
       
  1143  * - field--body--article.tpl.php
       
  1144  * - field--article.tpl.php
       
  1145  * - field--body.tpl.php
       
  1146  * - field.tpl.php
       
  1147  * So, if the body field on the article content type needs customization, a
       
  1148  * field--body--article.tpl.php file can be added within the theme's folder.
       
  1149  * Because it's a template, it will result in slightly more time needed to
       
  1150  * display that field, but it will not impact other fields, and therefore,
       
  1151  * is unlikely to cause a noticeable change in website performance. A very rough
       
  1152  * guideline is that if a page is being displayed with more than 100 fields and
       
  1153  * they are all themed with a template instead of a function, it can add up to
       
  1154  * 5% to the time it takes to display that page. This is a guideline only and
       
  1155  * the exact performance impact depends on the server configuration and the
       
  1156  * details of the website.
       
  1157  *
       
  1158  * @param $variables
       
  1159  *   An associative array containing:
       
  1160  *   - label_hidden: A boolean indicating to show or hide the field label.
       
  1161  *   - title_attributes: A string containing the attributes for the title.
       
  1162  *   - label: The label for the field.
       
  1163  *   - content_attributes: A string containing the attributes for the content's
       
  1164  *     div.
       
  1165  *   - items: An array of field items.
       
  1166  *   - item_attributes: An array of attributes for each item.
       
  1167  *   - classes: A string containing the classes for the wrapping div.
       
  1168  *   - attributes: A string containing the attributes for the wrapping div.
       
  1169  *
       
  1170  * @see template_preprocess_field()
       
  1171  * @see template_process_field()
       
  1172  * @see field.tpl.php
       
  1173  *
       
  1174  * @ingroup themeable
       
  1175  */
       
  1176 function theme_field($variables) {
       
  1177   $output = '';
       
  1178 
       
  1179   // Render the label, if it's not hidden.
       
  1180   if (!$variables['label_hidden']) {
       
  1181     $output .= '<div class="field-label"' . $variables['title_attributes'] . '>' . $variables['label'] . ':&nbsp;</div>';
       
  1182   }
       
  1183 
       
  1184   // Render the items.
       
  1185   $output .= '<div class="field-items"' . $variables['content_attributes'] . '>';
       
  1186   foreach ($variables['items'] as $delta => $item) {
       
  1187     $classes = 'field-item ' . ($delta % 2 ? 'odd' : 'even');
       
  1188     $output .= '<div class="' . $classes . '"' . $variables['item_attributes'][$delta] . '>' . drupal_render($item) . '</div>';
       
  1189   }
       
  1190   $output .= '</div>';
       
  1191 
       
  1192   // Render the top-level DIV.
       
  1193   $output = '<div class="' . $variables['classes'] . '"' . $variables['attributes'] . '>' . $output . '</div>';
       
  1194 
       
  1195   return $output;
       
  1196 }
       
  1197 
       
  1198 /**
       
  1199  * DEPRECATED: Helper form element validator: integer.
       
  1200  *
       
  1201  * Use element_validate_integer() instead.
       
  1202  *
       
  1203  * @deprecated
       
  1204  * @see element_validate_integer()
       
  1205  */
       
  1206 function _element_validate_integer($element, &$form_state) {
       
  1207   element_validate_integer($element, $form_state);
       
  1208 }
       
  1209 
       
  1210 /**
       
  1211  * DEPRECATED: Helper form element validator: integer > 0.
       
  1212  *
       
  1213  * Use element_validate_integer_positive() instead.
       
  1214  *
       
  1215  * @deprecated
       
  1216  * @see element_validate_integer_positive()
       
  1217  */
       
  1218 function _element_validate_integer_positive($element, &$form_state) {
       
  1219   element_validate_integer_positive($element, $form_state);
       
  1220 }
       
  1221 
       
  1222 /**
       
  1223  * DEPRECATED: Helper form element validator: number.
       
  1224  *
       
  1225  * Use element_validate_number() instead.
       
  1226  *
       
  1227  * @deprecated
       
  1228  * @see element_validate_number()
       
  1229  */
       
  1230 function _element_validate_number($element, &$form_state) {
       
  1231   element_validate_number($element, $form_state);
       
  1232 }