cms/drupal/modules/field/field.info.inc
changeset 541 e756a8c72c3d
equal deleted inserted replaced
540:07239de796bb 541:e756a8c72c3d
       
     1 <?php
       
     2 
       
     3 /**
       
     4  * @file
       
     5  * Field Info API, providing information about available fields and field types.
       
     6  */
       
     7 
       
     8 /**
       
     9  * Retrieves the FieldInfo object for the current request.
       
    10  *
       
    11  * @return FieldInfo
       
    12  *   An instance of the FieldInfo class.
       
    13  */
       
    14 function _field_info_field_cache() {
       
    15   // Use the advanced drupal_static() pattern, since this is called very often.
       
    16   static $drupal_static_fast;
       
    17 
       
    18   if (!isset($drupal_static_fast)) {
       
    19     $drupal_static_fast['field_info_field_cache'] = &drupal_static(__FUNCTION__);
       
    20   }
       
    21   $field_info = &$drupal_static_fast['field_info_field_cache'];
       
    22 
       
    23   if (!isset($field_info)) {
       
    24     // @todo The registry should save the need for an explicit include, but not
       
    25     // a couple upgrade tests (DisabledNodeTypeTestCase,
       
    26     // FilterFormatUpgradePathTestCase...) break in a strange way without it.
       
    27     include_once dirname(__FILE__) . '/field.info.class.inc';
       
    28     $field_info = new FieldInfo();
       
    29   }
       
    30 
       
    31   return $field_info;
       
    32 }
       
    33 
       
    34 /**
       
    35  * @defgroup field_info Field Info API
       
    36  * @{
       
    37  * Obtain information about Field API configuration.
       
    38  *
       
    39  * The Field Info API exposes information about field types, fields,
       
    40  * instances, bundles, widget types, display formatters, behaviors,
       
    41  * and settings defined by or with the Field API.
       
    42  *
       
    43  * See @link field Field API @endlink for information about the other parts of
       
    44  * the Field API.
       
    45  */
       
    46 
       
    47 /**
       
    48  * Clears the field info cache without clearing the field data cache.
       
    49  *
       
    50  * This is useful when deleted fields or instances are purged.  We
       
    51  * need to remove the purged records, but no actual field data items
       
    52  * are affected.
       
    53  */
       
    54 function field_info_cache_clear() {
       
    55   drupal_static_reset('field_view_mode_settings');
       
    56   drupal_static_reset('field_available_languages');
       
    57 
       
    58   // @todo: Remove this when field_attach_*_bundle() bundle management
       
    59   // functions are moved to the entity API.
       
    60   entity_info_cache_clear();
       
    61 
       
    62   _field_info_collate_types(TRUE);
       
    63   _field_info_field_cache()->flush();
       
    64 }
       
    65 
       
    66 /**
       
    67  * Collates all information on existing fields and instances.
       
    68  *
       
    69  * Deprecated. This function is kept to ensure backwards compatibility, but has
       
    70  * a serious performance impact, and should be absolutely avoided.
       
    71  * See http://drupal.org/node/1915646.
       
    72  *
       
    73  * Use the regular field_info_*() API functions to access the information, or
       
    74  * field_info_cache_clear() to clear the cached data.
       
    75  */
       
    76 function _field_info_collate_fields($reset = FALSE) {
       
    77   if ($reset) {
       
    78     _field_info_field_cache()->flush();
       
    79     return;
       
    80   }
       
    81 
       
    82   $cache = _field_info_field_cache();
       
    83 
       
    84   // Collect fields, and build the array of IDs keyed by field_name.
       
    85   $fields = $cache->getFields();
       
    86   $field_ids = array();
       
    87   foreach ($fields as $id => $field) {
       
    88     if (!$field['deleted']) {
       
    89       $field_ids[$field['field_name']] = $id;
       
    90     }
       
    91   }
       
    92 
       
    93   // Collect extra fields for all entity types.
       
    94   $extra_fields = array();
       
    95   foreach (field_info_bundles() as $entity_type => $bundles) {
       
    96     foreach ($bundles as $bundle => $info) {
       
    97       $extra_fields[$entity_type][$bundle] = $cache->getBundleExtraFields($entity_type, $bundle);
       
    98     }
       
    99   }
       
   100 
       
   101   return array(
       
   102     'fields' => $fields,
       
   103     'field_ids' => $field_ids,
       
   104     'instances' => $cache->getInstances(),
       
   105     'extra_fields' => $extra_fields,
       
   106   );
       
   107 }
       
   108 
       
   109 /**
       
   110  * Collates all information on field types, widget types and related structures.
       
   111  *
       
   112  * @param $reset
       
   113  *   If TRUE, clear the cache. The information will be rebuilt from the database
       
   114  *   next time it is needed. Defaults to FALSE.
       
   115  *
       
   116  * @return
       
   117  *   If $reset is TRUE, nothing.
       
   118  *   If $reset is FALSE, an array containing the following elements:
       
   119  *   - 'field types': Array of hook_field_info() results, keyed by field_type.
       
   120  *     Each element has the following components: label, description, settings,
       
   121  *     instance_settings, default_widget, default_formatter, and behaviors
       
   122  *     from hook_field_info(), as well as module, giving the module that exposes
       
   123  *     the field type.
       
   124  *   - 'widget types': Array of hook_field_widget_info() results, keyed by
       
   125  *     widget_type. Each element has the following components: label, field
       
   126  *     types, settings, weight, and behaviors from hook_field_widget_info(),
       
   127  *     as well as module, giving the module that exposes the widget type.
       
   128  *   - 'formatter types': Array of hook_field_formatter_info() results, keyed by
       
   129  *     formatter_type. Each element has the following components: label, field
       
   130  *     types, and behaviors from hook_field_formatter_info(), as well as
       
   131  *     module, giving the module that exposes the formatter type.
       
   132  *   - 'storage types': Array of hook_field_storage_info() results, keyed by
       
   133  *     storage type names. Each element has the following components: label,
       
   134  *     description, and settings from hook_field_storage_info(), as well as
       
   135  *     module, giving the module that exposes the storage type.
       
   136  *   - 'fieldable types': Array of hook_entity_info() results, keyed by
       
   137  *     entity_type. Each element has the following components: name, id key,
       
   138  *     revision key, bundle key, cacheable, and bundles from hook_entity_info(),
       
   139  *     as well as module, giving the module that exposes the entity type.
       
   140  */
       
   141 function _field_info_collate_types($reset = FALSE) {
       
   142   global $language;
       
   143   static $info;
       
   144 
       
   145   // The _info() hooks invoked below include translated strings, so each
       
   146   // language is cached separately.
       
   147   $langcode = $language->language;
       
   148 
       
   149   if ($reset) {
       
   150     $info = NULL;
       
   151     // Clear all languages.
       
   152     cache_clear_all('field_info_types:', 'cache_field', TRUE);
       
   153     return;
       
   154   }
       
   155 
       
   156   if (!isset($info)) {
       
   157     if ($cached = cache_get("field_info_types:$langcode", 'cache_field')) {
       
   158       $info = $cached->data;
       
   159     }
       
   160     else {
       
   161       $info = array(
       
   162         'field types' => array(),
       
   163         'widget types' => array(),
       
   164         'formatter types' => array(),
       
   165         'storage types' => array(),
       
   166       );
       
   167 
       
   168       // Populate field types.
       
   169       foreach (module_implements('field_info') as $module) {
       
   170         $field_types = (array) module_invoke($module, 'field_info');
       
   171         foreach ($field_types as $name => $field_info) {
       
   172           // Provide defaults.
       
   173           $field_info += array(
       
   174             'settings' => array(),
       
   175             'instance_settings' => array(),
       
   176           );
       
   177           $info['field types'][$name] = $field_info;
       
   178           $info['field types'][$name]['module'] = $module;
       
   179         }
       
   180       }
       
   181       drupal_alter('field_info', $info['field types']);
       
   182 
       
   183       // Populate widget types.
       
   184       foreach (module_implements('field_widget_info') as $module) {
       
   185         $widget_types = (array) module_invoke($module, 'field_widget_info');
       
   186         foreach ($widget_types as $name => $widget_info) {
       
   187           // Provide defaults.
       
   188           $widget_info += array(
       
   189             'settings' => array(),
       
   190           );
       
   191           $info['widget types'][$name] = $widget_info;
       
   192           $info['widget types'][$name]['module'] = $module;
       
   193         }
       
   194       }
       
   195       drupal_alter('field_widget_info', $info['widget types']);
       
   196       uasort($info['widget types'], 'drupal_sort_weight');
       
   197 
       
   198       // Populate formatter types.
       
   199       foreach (module_implements('field_formatter_info') as $module) {
       
   200         $formatter_types = (array) module_invoke($module, 'field_formatter_info');
       
   201         foreach ($formatter_types as $name => $formatter_info) {
       
   202           // Provide defaults.
       
   203           $formatter_info += array(
       
   204             'settings' => array(),
       
   205           );
       
   206           $info['formatter types'][$name] = $formatter_info;
       
   207           $info['formatter types'][$name]['module'] = $module;
       
   208         }
       
   209       }
       
   210       drupal_alter('field_formatter_info', $info['formatter types']);
       
   211 
       
   212       // Populate storage types.
       
   213       foreach (module_implements('field_storage_info') as $module) {
       
   214         $storage_types = (array) module_invoke($module, 'field_storage_info');
       
   215         foreach ($storage_types as $name => $storage_info) {
       
   216           // Provide defaults.
       
   217           $storage_info += array(
       
   218             'settings' => array(),
       
   219           );
       
   220           $info['storage types'][$name] = $storage_info;
       
   221           $info['storage types'][$name]['module'] = $module;
       
   222         }
       
   223       }
       
   224       drupal_alter('field_storage_info', $info['storage types']);
       
   225 
       
   226       // Set the cache if we can acquire a lock.
       
   227       if (lock_acquire("field_info_types:$langcode")) {
       
   228         cache_set("field_info_types:$langcode", $info, 'cache_field');
       
   229         lock_release("field_info_types:$langcode");
       
   230       }
       
   231     }
       
   232   }
       
   233 
       
   234   return $info;
       
   235 }
       
   236 
       
   237 /**
       
   238  * Prepares a field definition for the current run-time context.
       
   239  *
       
   240  * The functionality has moved to the FieldInfo class. This function is kept as
       
   241  * a backwards-compatibility layer. See http://drupal.org/node/1915646.
       
   242  *
       
   243  * @see FieldInfo::prepareField()
       
   244  */
       
   245 function _field_info_prepare_field($field) {
       
   246   $cache = _field_info_field_cache();
       
   247   return $cache->prepareField($field);
       
   248 }
       
   249 
       
   250 /**
       
   251  * Prepares an instance definition for the current run-time context.
       
   252  *
       
   253  * The functionality has moved to the FieldInfo class. This function is kept as
       
   254  * a backwards-compatibility layer. See http://drupal.org/node/1915646.
       
   255  *
       
   256  * @see FieldInfo::prepareInstance()
       
   257  */
       
   258 function _field_info_prepare_instance($instance, $field) {
       
   259   $cache = _field_info_field_cache();
       
   260   return $cache->prepareInstance($instance, $field['type']);
       
   261 }
       
   262 
       
   263 /**
       
   264  * Adapts display specifications to the current run-time context.
       
   265  *
       
   266  * The functionality has moved to the FieldInfo class. This function is kept as
       
   267  * a backwards-compatibility layer. See http://drupal.org/node/1915646.
       
   268  *
       
   269  * @see FieldInfo::prepareInstanceDisplay()
       
   270  */
       
   271 function _field_info_prepare_instance_display($field, $display) {
       
   272   $cache = _field_info_field_cache();
       
   273   return $cache->prepareInstanceDisplay($display, $field['type']);
       
   274 }
       
   275 
       
   276 /**
       
   277  * Prepares widget specifications for the current run-time context.
       
   278  *
       
   279  * The functionality has moved to the FieldInfo class. This function is kept as
       
   280  * a backwards-compatibility layer. See http://drupal.org/node/1915646.
       
   281  *
       
   282  * @see FieldInfo::prepareInstanceWidget()
       
   283  */
       
   284 function _field_info_prepare_instance_widget($field, $widget) {
       
   285   $cache = _field_info_field_cache();
       
   286   return $cache->prepareInstanceWidget($widget, $field['type']);
       
   287 }
       
   288 
       
   289 /**
       
   290  * Prepares 'extra fields' for the current run-time context.
       
   291  *
       
   292  * The functionality has moved to the FieldInfo class. This function is kept as
       
   293  * a backwards-compatibility layer. See http://drupal.org/node/1915646.
       
   294  *
       
   295  * @see FieldInfo::prepareExtraFields()
       
   296  */
       
   297 function _field_info_prepare_extra_fields($extra_fields, $entity_type, $bundle) {
       
   298   $cache = _field_info_field_cache();
       
   299   return $cache->prepareExtraFields($extra_fields, $entity_type, $bundle);
       
   300 }
       
   301 
       
   302 /**
       
   303  * Determines the behavior of a widget with respect to an operation.
       
   304  *
       
   305  * @param $op
       
   306  *   The name of the operation. Currently supported: 'default value',
       
   307  *   'multiple values'.
       
   308  * @param $instance
       
   309  *   The field instance array.
       
   310  *
       
   311  * @return
       
   312  *   One of these values:
       
   313  *   - FIELD_BEHAVIOR_NONE: Do nothing for this operation.
       
   314  *   - FIELD_BEHAVIOR_CUSTOM: Use the widget's callback function.
       
   315  *   - FIELD_BEHAVIOR_DEFAULT: Use field.module default behavior.
       
   316  */
       
   317 function field_behaviors_widget($op, $instance) {
       
   318   $info = field_info_widget_types($instance['widget']['type']);
       
   319   return isset($info['behaviors'][$op]) ? $info['behaviors'][$op] : FIELD_BEHAVIOR_DEFAULT;
       
   320 }
       
   321 
       
   322 /**
       
   323  * Returns information about field types from hook_field_info().
       
   324  *
       
   325  * @param $field_type
       
   326  *   (optional) A field type name. If omitted, all field types will be
       
   327  *   returned.
       
   328  *
       
   329  * @return
       
   330  *   Either a field type description, as provided by hook_field_info(), or an
       
   331  *   array of all existing field types, keyed by field type name.
       
   332  */
       
   333 function field_info_field_types($field_type = NULL) {
       
   334   $info = _field_info_collate_types();
       
   335   $field_types = $info['field types'];
       
   336   if ($field_type) {
       
   337     if (isset($field_types[$field_type])) {
       
   338       return $field_types[$field_type];
       
   339     }
       
   340   }
       
   341   else {
       
   342     return $field_types;
       
   343   }
       
   344 }
       
   345 
       
   346 /**
       
   347  * Returns information about field widgets from hook_field_widget_info().
       
   348  *
       
   349  * @param $widget_type
       
   350  *   (optional) A widget type name. If omitted, all widget types will be
       
   351  *   returned.
       
   352  *
       
   353  * @return
       
   354  *   Either a single widget type description, as provided by
       
   355  *   hook_field_widget_info(), or an array of all existing widget types, keyed
       
   356  *   by widget type name.
       
   357  */
       
   358 function field_info_widget_types($widget_type = NULL) {
       
   359   $info = _field_info_collate_types();
       
   360   $widget_types = $info['widget types'];
       
   361   if ($widget_type) {
       
   362     if (isset($widget_types[$widget_type])) {
       
   363       return $widget_types[$widget_type];
       
   364     }
       
   365   }
       
   366   else {
       
   367     return $widget_types;
       
   368   }
       
   369 }
       
   370 
       
   371 /**
       
   372  * Returns information about field formatters from hook_field_formatter_info().
       
   373  *
       
   374  * @param $formatter_type
       
   375  *   (optional) A formatter type name. If omitted, all formatter types will be
       
   376  *   returned.
       
   377  *
       
   378  * @return
       
   379  *   Either a single formatter type description, as provided by
       
   380  *   hook_field_formatter_info(), or an array of all existing formatter types,
       
   381  *   keyed by formatter type name.
       
   382  */
       
   383 function field_info_formatter_types($formatter_type = NULL) {
       
   384   $info = _field_info_collate_types();
       
   385   $formatter_types = $info['formatter types'];
       
   386   if ($formatter_type) {
       
   387     if (isset($formatter_types[$formatter_type])) {
       
   388       return $formatter_types[$formatter_type];
       
   389     }
       
   390   }
       
   391   else {
       
   392     return $formatter_types;
       
   393   }
       
   394 }
       
   395 
       
   396 /**
       
   397  * Returns information about field storage from hook_field_storage_info().
       
   398  *
       
   399  * @param $storage_type
       
   400  *   (optional) A storage type name. If omitted, all storage types will be
       
   401  *   returned.
       
   402  *
       
   403  * @return
       
   404  *   Either a storage type description, as provided by
       
   405  *   hook_field_storage_info(), or an array of all existing storage types,
       
   406  *   keyed by storage type name.
       
   407  */
       
   408 function field_info_storage_types($storage_type = NULL) {
       
   409   $info = _field_info_collate_types();
       
   410   $storage_types = $info['storage types'];
       
   411   if ($storage_type) {
       
   412     if (isset($storage_types[$storage_type])) {
       
   413       return $storage_types[$storage_type];
       
   414     }
       
   415   }
       
   416   else {
       
   417     return $storage_types;
       
   418   }
       
   419 }
       
   420 
       
   421 /**
       
   422  * Returns information about existing bundles.
       
   423  *
       
   424  * @param $entity_type
       
   425  *   The type of entity; e.g. 'node' or 'user'.
       
   426  *
       
   427  * @return
       
   428  *   An array of bundles for the $entity_type keyed by bundle name,
       
   429  *   or, if no $entity_type was provided, the array of all existing bundles,
       
   430  *   keyed by entity type.
       
   431  */
       
   432 function field_info_bundles($entity_type = NULL) {
       
   433   $info = entity_get_info();
       
   434 
       
   435   if ($entity_type) {
       
   436     return isset($info[$entity_type]['bundles']) ? $info[$entity_type]['bundles'] : array();
       
   437   }
       
   438 
       
   439   $bundles = array();
       
   440   foreach ($info as $type => $entity_info) {
       
   441     $bundles[$type] = $entity_info['bundles'];
       
   442   }
       
   443   return $bundles;
       
   444 }
       
   445 
       
   446 /**
       
   447  * Returns a lightweight map of fields across bundles.
       
   448  *
       
   449  * The function only returns active, non deleted fields.
       
   450  *
       
   451  * @return
       
   452  *   An array keyed by field name. Each value is an array with two entries:
       
   453  *   - type: The field type.
       
   454  *   - bundles: The bundles in which the field appears, as an array with entity
       
   455  *     types as keys and the array of bundle names as values.
       
   456  * Example:
       
   457  * @code
       
   458  * array(
       
   459  *   'body' => array(
       
   460  *     'bundles' => array(
       
   461  *       'node' => array('page', 'article'),
       
   462  *     ),
       
   463  *     'type' => 'text_with_summary',
       
   464  *   ),
       
   465  * );
       
   466  * @endcode
       
   467  */
       
   468 function field_info_field_map() {
       
   469   $cache = _field_info_field_cache();
       
   470   return $cache->getFieldMap();
       
   471 }
       
   472 
       
   473 /**
       
   474  * Returns all field definitions.
       
   475  *
       
   476  * Use of this function should be avoided when possible, since it loads and
       
   477  * statically caches a potentially large array of information. Use
       
   478  * field_info_field_map() instead.
       
   479  *
       
   480  * When iterating over the fields present in a given bundle after a call to
       
   481  * field_info_instances($entity_type, $bundle), it is recommended to use
       
   482  * field_info_field() on each individual field instead.
       
   483  *
       
   484  * @return
       
   485  *   An array of field definitions, keyed by field name. Each field has an
       
   486  *   additional property, 'bundles', which is an array of all the bundles to
       
   487  *   which this field belongs keyed by entity type.
       
   488  *
       
   489  * @see field_info_field_map()
       
   490  */
       
   491 function field_info_fields() {
       
   492   $cache = _field_info_field_cache();
       
   493   $info = $cache->getFields();
       
   494 
       
   495   $fields = array();
       
   496   foreach ($info as $key => $field) {
       
   497     if (!$field['deleted']) {
       
   498       $fields[$field['field_name']] = $field;
       
   499     }
       
   500   }
       
   501 
       
   502   return $fields;
       
   503 }
       
   504 
       
   505 /**
       
   506  * Returns data about an individual field, given a field name.
       
   507  *
       
   508  * @param $field_name
       
   509  *   The name of the field to retrieve. $field_name can only refer to a
       
   510  *   non-deleted, active field. For deleted fields, use
       
   511  *   field_info_field_by_id(). To retrieve information about inactive fields,
       
   512  *   use field_read_fields().
       
   513  *
       
   514  * @return
       
   515  *   The field array, as returned by field_read_fields(), with an
       
   516  *   additional element 'bundles', whose value is an array of all the bundles
       
   517  *   this field belongs to keyed by entity type. NULL if the field was not
       
   518  *   found.
       
   519  *
       
   520  * @see field_info_field_by_id()
       
   521  */
       
   522 function field_info_field($field_name) {
       
   523   $cache = _field_info_field_cache();
       
   524   return $cache->getField($field_name);
       
   525 }
       
   526 
       
   527 /**
       
   528  * Returns data about an individual field, given a field ID.
       
   529  *
       
   530  * @param $field_id
       
   531  *   The id of the field to retrieve. $field_id can refer to a
       
   532  *   deleted field, but not an inactive one.
       
   533  *
       
   534  * @return
       
   535  *   The field array, as returned by field_read_fields(), with an
       
   536  *   additional element 'bundles', whose value is an array of all the bundles
       
   537  *   this field belongs to.
       
   538  *
       
   539  * @see field_info_field()
       
   540  */
       
   541 function field_info_field_by_id($field_id) {
       
   542   $cache = _field_info_field_cache();
       
   543   return $cache->getFieldById($field_id);
       
   544 }
       
   545 
       
   546 /**
       
   547  * Returns the same data as field_info_field_by_id() for every field.
       
   548  *
       
   549  * Use of this function should be avoided when possible, since it loads and
       
   550  * statically caches a potentially large array of information.
       
   551  *
       
   552  * When iterating over the fields present in a given bundle after a call to
       
   553  * field_info_instances($entity_type, $bundle), it is recommended to use
       
   554  * field_info_field() on each individual field instead.
       
   555  *
       
   556  * @return
       
   557  *   An array, each key is a field ID and the values are field arrays as
       
   558  *   returned by field_read_fields(), with an additional element 'bundles',
       
   559  *   whose value is an array of all the bundle this field belongs to.
       
   560  *
       
   561  * @see field_info_field()
       
   562  * @see field_info_field_by_id()
       
   563  */
       
   564 function field_info_field_by_ids() {
       
   565   $cache = _field_info_field_cache();
       
   566   return $cache->getFields();
       
   567 }
       
   568 
       
   569 /**
       
   570  * Retrieves information about field instances.
       
   571  *
       
   572  * Use of this function to retrieve instances across separate bundles (i.e.
       
   573  * when the $bundle parameter is NULL) should be avoided when possible, since
       
   574  * it loads and statically caches a potentially large array of information. Use
       
   575  * field_info_field_map() instead.
       
   576  *
       
   577  * When retrieving the instances of a specific bundle (i.e. when both
       
   578  * $entity_type and $bundle_name are provided), the function also populates a
       
   579  * static cache with the corresponding field definitions, allowing fast
       
   580  * retrieval of field_info_field() later in the request.
       
   581  *
       
   582  * @param $entity_type
       
   583  *   (optional) The entity type for which to return instances.
       
   584  * @param $bundle_name
       
   585  *   (optional) The bundle name for which to return instances. If $entity_type
       
   586  *   is NULL, the $bundle_name parameter is ignored.
       
   587  *
       
   588  * @return
       
   589  *   If $entity_type is not set, return all instances keyed by entity type and
       
   590  *   bundle name. If $entity_type is set, return all instances for that entity
       
   591  *   type, keyed by bundle name. If $entity_type and $bundle_name are set, return
       
   592  *   all instances for that bundle.
       
   593  *
       
   594  * @see field_info_field_map()
       
   595  */
       
   596 function field_info_instances($entity_type = NULL, $bundle_name = NULL) {
       
   597   $cache = _field_info_field_cache();
       
   598 
       
   599   if (!isset($entity_type)) {
       
   600     return $cache->getInstances();
       
   601   }
       
   602   if (!isset($bundle_name)) {
       
   603     return $cache->getInstances($entity_type);
       
   604   }
       
   605 
       
   606   return $cache->getBundleInstances($entity_type, $bundle_name);
       
   607 }
       
   608 
       
   609 /**
       
   610  * Returns an array of instance data for a specific field and bundle.
       
   611  *
       
   612  * The function populates a static cache with all fields and instances used in
       
   613  * the bundle, allowing fast retrieval of field_info_field() or
       
   614  * field_info_instance() later in the request.
       
   615  *
       
   616  * @param $entity_type
       
   617  *   The entity type for the instance.
       
   618  * @param $field_name
       
   619  *   The field name for the instance.
       
   620  * @param $bundle_name
       
   621  *   The bundle name for the instance.
       
   622  *
       
   623  * @return
       
   624  *   An associative array of instance data for the specific field and bundle;
       
   625  *   NULL if the instance does not exist.
       
   626  */
       
   627 function field_info_instance($entity_type, $field_name, $bundle_name) {
       
   628   $cache = _field_info_field_cache();
       
   629   $info = $cache->getBundleInstances($entity_type, $bundle_name);
       
   630   if (isset($info[$field_name])) {
       
   631     return $info[$field_name];
       
   632   }
       
   633 }
       
   634 
       
   635 /**
       
   636  * Returns a list and settings of pseudo-field elements in a given bundle.
       
   637  *
       
   638  * If $context is 'form', an array with the following structure:
       
   639  * @code
       
   640  *   array(
       
   641  *     'name_of_pseudo_field_component' => array(
       
   642  *       'label' => The human readable name of the component,
       
   643  *       'description' => A short description of the component content,
       
   644  *       'weight' => The weight of the component in edit forms,
       
   645  *     ),
       
   646  *     'name_of_other_pseudo_field_component' => array(
       
   647  *       // ...
       
   648  *     ),
       
   649  *   );
       
   650  * @endcode
       
   651  *
       
   652  * If $context is 'display', an array with the following structure:
       
   653  * @code
       
   654  *   array(
       
   655  *     'name_of_pseudo_field_component' => array(
       
   656  *       'label' => The human readable name of the component,
       
   657  *       'description' => A short description of the component content,
       
   658  *       // One entry per view mode, including the 'default' mode:
       
   659  *       'display' => array(
       
   660  *         'default' => array(
       
   661  *           'weight' => The weight of the component in displayed entities in
       
   662  *             this view mode,
       
   663  *           'visible' => TRUE if the component is visible, FALSE if hidden, in
       
   664  *             displayed entities in this view mode,
       
   665  *         ),
       
   666  *         'teaser' => array(
       
   667  *           // ...
       
   668  *         ),
       
   669  *       ),
       
   670  *     ),
       
   671  *     'name_of_other_pseudo_field_component' => array(
       
   672  *       // ...
       
   673  *     ),
       
   674  *   );
       
   675  * @endcode
       
   676  *
       
   677  * @param $entity_type
       
   678  *   The type of entity; e.g. 'node' or 'user'.
       
   679  * @param $bundle
       
   680  *   The bundle name.
       
   681  * @param $context
       
   682  *   The context for which the list of pseudo-fields is requested. Either
       
   683  *   'form' or 'display'.
       
   684  *
       
   685  * @return
       
   686  *   The array of pseudo-field elements in the bundle.
       
   687  */
       
   688 function field_info_extra_fields($entity_type, $bundle, $context) {
       
   689   $cache = _field_info_field_cache();
       
   690   $info = $cache->getBundleExtraFields($entity_type, $bundle);
       
   691 
       
   692   return isset($info[$context]) ? $info[$context] : array();
       
   693 }
       
   694 
       
   695 /**
       
   696  * Returns the maximum weight of all the components in an entity.
       
   697  *
       
   698  * This includes fields, 'extra_fields', and other components added by
       
   699  * third-party modules (e.g. field_group).
       
   700  *
       
   701  * @param $entity_type
       
   702  *   The type of entity; e.g. 'node' or 'user'.
       
   703  * @param $bundle
       
   704  *   The bundle name.
       
   705  * @param $context
       
   706  *   The context for which the maximum weight is requested. Either 'form', or
       
   707  *   the name of a view mode.
       
   708  * @return
       
   709  *   The maximum weight of the entity's components, or NULL if no components
       
   710  *   were found.
       
   711  */
       
   712 function field_info_max_weight($entity_type, $bundle, $context) {
       
   713   $weights = array();
       
   714 
       
   715   // Collect weights for fields.
       
   716   foreach (field_info_instances($entity_type, $bundle) as $instance) {
       
   717     if ($context == 'form') {
       
   718       $weights[] = $instance['widget']['weight'];
       
   719     }
       
   720     elseif (isset($instance['display'][$context]['weight'])) {
       
   721       $weights[] = $instance['display'][$context]['weight'];
       
   722     }
       
   723   }
       
   724   // Collect weights for extra fields.
       
   725   foreach (field_info_extra_fields($entity_type, $bundle, $context) as $extra) {
       
   726     $weights[] = $extra['weight'];
       
   727   }
       
   728 
       
   729   // Let other modules feedback about their own additions.
       
   730   $weights = array_merge($weights, module_invoke_all('field_info_max_weight', $entity_type, $bundle, $context));
       
   731   $max_weight = $weights ? max($weights) : NULL;
       
   732 
       
   733   return $max_weight;
       
   734 }
       
   735 
       
   736 /**
       
   737  * Returns a field type's default settings.
       
   738  *
       
   739  * @param $type
       
   740  *   A field type name.
       
   741  *
       
   742  * @return
       
   743  *   The field type's default settings, as provided by hook_field_info(), or an
       
   744  *   empty array if type or settings are not defined.
       
   745  */
       
   746 function field_info_field_settings($type) {
       
   747   $info = field_info_field_types($type);
       
   748   return isset($info['settings']) ? $info['settings'] : array();
       
   749 }
       
   750 
       
   751 /**
       
   752  * Returns a field type's default instance settings.
       
   753  *
       
   754  * @param $type
       
   755  *   A field type name.
       
   756  *
       
   757  * @return
       
   758  *   The field type's default instance settings, as provided by
       
   759  *   hook_field_info(), or an empty array if type or settings are not defined.
       
   760  */
       
   761 function field_info_instance_settings($type) {
       
   762   $info = field_info_field_types($type);
       
   763   return isset($info['instance_settings']) ? $info['instance_settings'] : array();
       
   764 }
       
   765 
       
   766 /**
       
   767  * Returns a field widget's default settings.
       
   768  *
       
   769  * @param $type
       
   770  *   A widget type name.
       
   771  *
       
   772  * @return
       
   773  *   The widget type's default settings, as provided by
       
   774  *   hook_field_widget_info(), or an empty array if type or settings are
       
   775  *   undefined.
       
   776  */
       
   777 function field_info_widget_settings($type) {
       
   778   $info = field_info_widget_types($type);
       
   779   return isset($info['settings']) ? $info['settings'] : array();
       
   780 }
       
   781 
       
   782 /**
       
   783  * Returns a field formatter's default settings.
       
   784  *
       
   785  * @param $type
       
   786  *   A field formatter type name.
       
   787  *
       
   788  * @return
       
   789  *   The formatter type's default settings, as provided by
       
   790  *   hook_field_formatter_info(), or an empty array if type or settings are
       
   791  *   undefined.
       
   792  */
       
   793 function field_info_formatter_settings($type) {
       
   794   $info = field_info_formatter_types($type);
       
   795   return isset($info['settings']) ? $info['settings'] : array();
       
   796 }
       
   797 
       
   798 /**
       
   799  * Returns a field storage type's default settings.
       
   800  *
       
   801  * @param $type
       
   802  *   A field storage type name.
       
   803  *
       
   804  * @return
       
   805  *   The storage type's default settings, as provided by
       
   806  *   hook_field_storage_info(), or an empty array if type or settings are
       
   807  *   undefined.
       
   808  */
       
   809 function field_info_storage_settings($type) {
       
   810   $info = field_info_storage_types($type);
       
   811   return isset($info['settings']) ? $info['settings'] : array();
       
   812 }
       
   813 
       
   814 /**
       
   815  * @} End of "defgroup field_info".
       
   816  */