field.module

You are here

  1. 7 modules/field/field.module
  2. 8 core/modules/field/field.module

Attach custom data fields to Drupal entities.

Functions

Namesort descending Description
field_access Determine whether the user has access to a given field.
field_associate_fields Allows a module to update the database for fields and columns it controls.
field_bundle_settings Gets or sets administratively defined bundle settings.
field_cache_clear Clear the field info and field data caches.
field_cron Implements hook_cron().
field_extract_bundle Helper function to extract the bundle name of from a bundle object.
field_extra_fields_get_display Returns the display settings to use for pseudo-fields in a given view mode.
field_filter_xss Like filter_xss_admin(), but with a shorter list of allowed tags.
field_flush_caches Implements hook_flush_caches().
field_get_default_value Helper function to get the default value for a field on an entity.
field_get_display Returns the display settings to use for an instance in a given view mode.
field_get_items Returns the field items in the language they currently would be displayed.
field_has_data Determine whether a field has any data.
field_help Implements hook_help().
field_modules_disabled Implements hook_modules_disabled().
field_modules_enabled Implements hook_modules_enabled().
field_modules_uninstalled Implements hook_modules_uninstalled().
field_sync_field_status Refreshes the 'active' and 'storage_active' columns for fields.
field_system_info_alter Implements hook_system_info_alter().
field_theme Implements hook_theme().
field_view_field Returns a renderable array for the value of a single field in an entity.
field_view_mode_settings Returns view mode settings in a given bundle.
field_view_value Returns a renderable array for a single field value.
template_preprocess_field Theme preprocess function for theme_field() and field.tpl.php.
template_process_field Theme process function for theme_field() and field.tpl.php.
theme_field Returns HTML for a field.
_element_validate_integer DEPRECATED: Helper form element validator: integer.
_element_validate_integer_positive DEPRECATED: Helper form element validator: integer > 0.
_element_validate_number DEPRECATED: Helper form element validator: number.
_field_extra_fields_pre_render Pre-render callback to adjust weights and visibility of non-field elements.
_field_filter_items Helper function to filter out empty field values.
_field_filter_xss_allowed_tags List of tags allowed by field_filter_xss().
_field_filter_xss_display_allowed_tags Human-readable list of allowed tags, for display in help texts.
_field_sort_items Helper function to sort items in a field according to user drag-n-drop reordering.
_field_sort_items_helper Sort function for items order. (copied form element_sort(), which acts on #weight keys)
_field_sort_items_value_helper Same as above, using ['_weight']['#value']

Constants

Namesort descending Description
FIELD_BEHAVIOR_CUSTOM Value for field API indicating a widget can receive several field values.
FIELD_BEHAVIOR_DEFAULT Value for field API concerning widget default and multiple value settings.
FIELD_BEHAVIOR_NONE Value for field API indicating a widget doesn't accept default values.
FIELD_CARDINALITY_UNLIMITED Value for field API indicating a field accepts an unlimited number of values.
FIELD_LOAD_CURRENT Age argument for loading the most recent version of an entity's field data with field_attach_load().
FIELD_LOAD_REVISION Age argument for loading the version of an entity's field data specified in the entity with field_attach_load().

Classes

Namesort descending Description
FieldException Base class for all exceptions thrown by Field API functions.
FieldUpdateForbiddenException Exception class thrown by hook_field_update_forbid().

File

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