field.module 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216
  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_system_info_alter().
  331. *
  332. * Goes through a list of all modules that provide a field type, and makes them
  333. * required if there are any active fields of that type.
  334. */
  335. function field_system_info_alter(&$info, $file, $type) {
  336. if ($type == 'module' && module_hook($file->name, 'field_info')) {
  337. $fields = field_read_fields(array('module' => $file->name), array('include_deleted' => TRUE));
  338. if ($fields) {
  339. $info['required'] = TRUE;
  340. // Provide an explanation message (only mention pending deletions if there
  341. // remains no actual, non-deleted fields)
  342. $non_deleted = FALSE;
  343. foreach ($fields as $field) {
  344. if (empty($field['deleted'])) {
  345. $non_deleted = TRUE;
  346. break;
  347. }
  348. }
  349. if ($non_deleted) {
  350. if (module_exists('field_ui')) {
  351. $explanation = t('Field type(s) in use - see <a href="@fields-page">Field list</a>', array('@fields-page' => url('admin/reports/fields')));
  352. }
  353. else {
  354. $explanation = t('Fields type(s) in use');
  355. }
  356. }
  357. else {
  358. $explanation = t('Fields pending deletion');
  359. }
  360. $info['explanation'] = $explanation;
  361. }
  362. }
  363. }
  364. /**
  365. * Implements hook_flush_caches().
  366. */
  367. function field_flush_caches() {
  368. // Refresh the 'active' status of fields.
  369. field_sync_field_status();
  370. // Request a flush of our cache table.
  371. return array('cache_field');
  372. }
  373. /**
  374. * Implements hook_modules_enabled().
  375. */
  376. function field_modules_enabled($modules) {
  377. // Refresh the 'active' status of fields.
  378. field_sync_field_status();
  379. }
  380. /**
  381. * Implements hook_modules_disabled().
  382. */
  383. function field_modules_disabled($modules) {
  384. // Refresh the 'active' status of fields.
  385. field_sync_field_status();
  386. }
  387. /**
  388. * Refreshes the 'active' and 'storage_active' columns for fields.
  389. */
  390. function field_sync_field_status() {
  391. // Refresh the 'active' and 'storage_active' columns according to the current
  392. // set of enabled modules.
  393. $modules = module_list();
  394. foreach ($modules as $module_name) {
  395. field_associate_fields($module_name);
  396. }
  397. db_update('field_config')
  398. ->fields(array('active' => 0))
  399. ->condition('module', $modules, 'NOT IN')
  400. ->execute();
  401. db_update('field_config')
  402. ->fields(array('storage_active' => 0))
  403. ->condition('storage_module', $modules, 'NOT IN')
  404. ->execute();
  405. }
  406. /**
  407. * Allows a module to update the database for fields and columns it controls.
  408. *
  409. * @param $module
  410. * The name of the module to update on.
  411. */
  412. function field_associate_fields($module) {
  413. // Associate field types.
  414. $field_types = (array) module_invoke($module, 'field_info');
  415. if ($field_types) {
  416. db_update('field_config')
  417. ->fields(array('module' => $module, 'active' => 1))
  418. ->condition('type', array_keys($field_types))
  419. ->execute();
  420. }
  421. // Associate storage backends.
  422. $storage_types = (array) module_invoke($module, 'field_storage_info');
  423. if ($storage_types) {
  424. db_update('field_config')
  425. ->fields(array('storage_module' => $module, 'storage_active' => 1))
  426. ->condition('storage_type', array_keys($storage_types))
  427. ->execute();
  428. }
  429. }
  430. /**
  431. * Helper function to get the default value for a field on an entity.
  432. *
  433. * @param $entity_type
  434. * The type of $entity; e.g., 'node' or 'user'.
  435. * @param $entity
  436. * The entity for the operation.
  437. * @param $field
  438. * The field structure.
  439. * @param $instance
  440. * The instance structure.
  441. * @param $langcode
  442. * The field language to fill-in with the default value.
  443. */
  444. function field_get_default_value($entity_type, $entity, $field, $instance, $langcode = NULL) {
  445. $items = array();
  446. if (!empty($instance['default_value_function'])) {
  447. $function = $instance['default_value_function'];
  448. if (function_exists($function)) {
  449. $items = $function($entity_type, $entity, $field, $instance, $langcode);
  450. }
  451. }
  452. elseif (!empty($instance['default_value'])) {
  453. $items = $instance['default_value'];
  454. }
  455. return $items;
  456. }
  457. /**
  458. * Helper function to filter out empty field values.
  459. *
  460. * @param $field
  461. * The field definition.
  462. * @param $items
  463. * The field values to filter.
  464. *
  465. * @return
  466. * The array of items without empty field values. The function also renumbers
  467. * the array keys to ensure sequential deltas.
  468. */
  469. function _field_filter_items($field, $items) {
  470. $function = $field['module'] . '_field_is_empty';
  471. foreach ((array) $items as $delta => $item) {
  472. // Explicitly break if the function is undefined.
  473. if ($function($item, $field)) {
  474. unset($items[$delta]);
  475. }
  476. }
  477. return array_values($items);
  478. }
  479. /**
  480. * Helper function to sort items in a field according to
  481. * user drag-n-drop reordering.
  482. */
  483. function _field_sort_items($field, $items) {
  484. if (($field['cardinality'] > 1 || $field['cardinality'] == FIELD_CARDINALITY_UNLIMITED) && isset($items[0]['_weight'])) {
  485. usort($items, '_field_sort_items_helper');
  486. foreach ($items as $delta => $item) {
  487. if (is_array($items[$delta])) {
  488. unset($items[$delta]['_weight']);
  489. }
  490. }
  491. }
  492. return $items;
  493. }
  494. /**
  495. * Sort function for items order.
  496. * (copied form element_sort(), which acts on #weight keys)
  497. */
  498. function _field_sort_items_helper($a, $b) {
  499. $a_weight = (is_array($a) ? $a['_weight'] : 0);
  500. $b_weight = (is_array($b) ? $b['_weight'] : 0);
  501. return $a_weight - $b_weight;
  502. }
  503. /**
  504. * Same as above, using ['_weight']['#value']
  505. */
  506. function _field_sort_items_value_helper($a, $b) {
  507. $a_weight = (is_array($a) && isset($a['_weight']['#value']) ? $a['_weight']['#value'] : 0);
  508. $b_weight = (is_array($b) && isset($b['_weight']['#value']) ? $b['_weight']['#value'] : 0);
  509. return $a_weight - $b_weight;
  510. }
  511. /**
  512. * Gets or sets administratively defined bundle settings.
  513. *
  514. * @param string $entity_type
  515. * The type of $entity; e.g., 'node' or 'user'.
  516. * @param string $bundle
  517. * The bundle name.
  518. * @param array|null $settings
  519. * (optional) The settings to store, an associative array with the following
  520. * elements:
  521. * - view_modes: An associative array keyed by view mode, with the following
  522. * key/value pairs:
  523. * - custom_settings: Boolean specifying whether the view mode uses a
  524. * dedicated set of display options (TRUE), or the 'default' options
  525. * (FALSE). Defaults to FALSE.
  526. * - extra_fields: An associative array containing the form and display
  527. * settings for extra fields (also known as pseudo-fields):
  528. * - form: An associative array whose keys are the names of extra fields,
  529. * and whose values are associative arrays with the following elements:
  530. * - weight: The weight of the extra field, determining its position on an
  531. * entity form.
  532. * - display: An associative array whose keys are the names of extra fields,
  533. * and whose values are associative arrays keyed by the name of view
  534. * modes. This array must include an item for the 'default' view mode.
  535. * Each view mode sub-array contains the following elements:
  536. * - weight: The weight of the extra field, determining its position when
  537. * an entity is viewed.
  538. * - visible: TRUE if the extra field is visible, FALSE otherwise.
  539. *
  540. * @return array|null
  541. * If no $settings are passed, the current settings are returned.
  542. */
  543. function field_bundle_settings($entity_type, $bundle, $settings = NULL) {
  544. if (isset($settings)) {
  545. variable_set('field_bundle_settings_' . $entity_type . '__' . $bundle, $settings);
  546. field_info_cache_clear();
  547. }
  548. else {
  549. $settings = variable_get('field_bundle_settings_' . $entity_type . '__' . $bundle, array());
  550. $settings += array(
  551. 'view_modes' => array(),
  552. 'extra_fields' => array(),
  553. );
  554. $settings['extra_fields'] += array(
  555. 'form' => array(),
  556. 'display' => array(),
  557. );
  558. return $settings;
  559. }
  560. }
  561. /**
  562. * Returns view mode settings in a given bundle.
  563. *
  564. * @param $entity_type
  565. * The type of entity; e.g. 'node' or 'user'.
  566. * @param $bundle
  567. * The bundle name to return view mode settings for.
  568. *
  569. * @return
  570. * An array keyed by view mode, with the following key/value pairs:
  571. * - custom_settings: Boolean specifying whether the view mode uses a
  572. * dedicated set of display options (TRUE), or the 'default' options
  573. * (FALSE). Defaults to FALSE.
  574. */
  575. function field_view_mode_settings($entity_type, $bundle) {
  576. $cache = &drupal_static(__FUNCTION__, array());
  577. if (!isset($cache[$entity_type][$bundle])) {
  578. $bundle_settings = field_bundle_settings($entity_type, $bundle);
  579. $settings = $bundle_settings['view_modes'];
  580. // Include view modes for which nothing has been stored yet, but whose
  581. // definition in hook_entity_info() specify they should use custom settings
  582. // by default.
  583. $entity_info = entity_get_info($entity_type);
  584. foreach ($entity_info['view modes'] as $view_mode => $view_mode_info) {
  585. if (!isset($settings[$view_mode]['custom_settings']) && $view_mode_info['custom settings']) {
  586. $settings[$view_mode]['custom_settings'] = TRUE;
  587. }
  588. }
  589. $cache[$entity_type][$bundle] = $settings;
  590. }
  591. return $cache[$entity_type][$bundle];
  592. }
  593. /**
  594. * Returns the display settings to use for an instance in a given view mode.
  595. *
  596. * @param $instance
  597. * The field instance being displayed.
  598. * @param $view_mode
  599. * The view mode.
  600. * @param $entity
  601. * The entity being displayed.
  602. *
  603. * @return
  604. * The display settings to be used when displaying the field values.
  605. */
  606. function field_get_display($instance, $view_mode, $entity) {
  607. // Check whether the view mode uses custom display settings or the 'default'
  608. // mode.
  609. $view_mode_settings = field_view_mode_settings($instance['entity_type'], $instance['bundle']);
  610. $actual_mode = (!empty($view_mode_settings[$view_mode]['custom_settings']) ? $view_mode : 'default');
  611. $display = $instance['display'][$actual_mode];
  612. // Let modules alter the display settings.
  613. $context = array(
  614. 'entity_type' => $instance['entity_type'],
  615. 'field' => field_info_field($instance['field_name']),
  616. 'instance' => $instance,
  617. 'entity' => $entity,
  618. 'view_mode' => $view_mode,
  619. );
  620. drupal_alter(array('field_display', 'field_display_' . $instance['entity_type']), $display, $context);
  621. return $display;
  622. }
  623. /**
  624. * Returns the display settings to use for pseudo-fields in a given view mode.
  625. *
  626. * @param $entity_type
  627. * The type of $entity; e.g., 'node' or 'user'.
  628. * @param $bundle
  629. * The bundle name.
  630. * @param $view_mode
  631. * The view mode.
  632. *
  633. * @return
  634. * The display settings to be used when viewing the bundle's pseudo-fields.
  635. */
  636. function field_extra_fields_get_display($entity_type, $bundle, $view_mode) {
  637. // Check whether the view mode uses custom display settings or the 'default'
  638. // mode.
  639. $view_mode_settings = field_view_mode_settings($entity_type, $bundle);
  640. $actual_mode = (!empty($view_mode_settings[$view_mode]['custom_settings'])) ? $view_mode : 'default';
  641. $extra_fields = field_info_extra_fields($entity_type, $bundle, 'display');
  642. $displays = array();
  643. foreach ($extra_fields as $name => $value) {
  644. $displays[$name] = $extra_fields[$name]['display'][$actual_mode];
  645. }
  646. // Let modules alter the display settings.
  647. $context = array(
  648. 'entity_type' => $entity_type,
  649. 'bundle' => $bundle,
  650. 'view_mode' => $view_mode,
  651. );
  652. drupal_alter('field_extra_fields_display', $displays, $context);
  653. return $displays;
  654. }
  655. /**
  656. * Pre-render callback to adjust weights and visibility of non-field elements.
  657. */
  658. function _field_extra_fields_pre_render($elements) {
  659. $entity_type = $elements['#entity_type'];
  660. $bundle = $elements['#bundle'];
  661. if (isset($elements['#type']) && $elements['#type'] == 'form') {
  662. $extra_fields = field_info_extra_fields($entity_type, $bundle, 'form');
  663. foreach ($extra_fields as $name => $settings) {
  664. if (isset($elements[$name])) {
  665. $elements[$name]['#weight'] = $settings['weight'];
  666. }
  667. }
  668. }
  669. elseif (isset($elements['#view_mode'])) {
  670. $view_mode = $elements['#view_mode'];
  671. $extra_fields = field_extra_fields_get_display($entity_type, $bundle, $view_mode);
  672. foreach ($extra_fields as $name => $settings) {
  673. if (isset($elements[$name])) {
  674. $elements[$name]['#weight'] = $settings['weight'];
  675. // Visibility: make sure we do not accidentally show a hidden element.
  676. $elements[$name]['#access'] = isset($elements[$name]['#access']) ? ($elements[$name]['#access'] && $settings['visible']) : $settings['visible'];
  677. }
  678. }
  679. }
  680. return $elements;
  681. }
  682. /**
  683. * Clear the field info and field data caches.
  684. */
  685. function field_cache_clear() {
  686. cache_clear_all('*', 'cache_field', TRUE);
  687. field_info_cache_clear();
  688. }
  689. /**
  690. * Like filter_xss_admin(), but with a shorter list of allowed tags.
  691. *
  692. * Used for items entered by administrators, like field descriptions,
  693. * allowed values, where some (mainly inline) mark-up may be desired
  694. * (so check_plain() is not acceptable).
  695. */
  696. function field_filter_xss($string) {
  697. return filter_xss($string, _field_filter_xss_allowed_tags());
  698. }
  699. /**
  700. * List of tags allowed by field_filter_xss().
  701. */
  702. function _field_filter_xss_allowed_tags() {
  703. return array('a', 'b', 'big', 'code', 'del', 'em', 'i', 'ins', 'pre', 'q', 'small', 'span', 'strong', 'sub', 'sup', 'tt', 'ol', 'ul', 'li', 'p', 'br', 'img');
  704. }
  705. /**
  706. * Human-readable list of allowed tags, for display in help texts.
  707. */
  708. function _field_filter_xss_display_allowed_tags() {
  709. return '<' . implode('> <', _field_filter_xss_allowed_tags()) . '>';
  710. }
  711. /**
  712. * Returns a renderable array for a single field value.
  713. *
  714. * @param $entity_type
  715. * The type of $entity; e.g., 'node' or 'user'.
  716. * @param $entity
  717. * The entity containing the field to display. Must at least contain the id
  718. * key and the field data to display.
  719. * @param $field_name
  720. * The name of the field to display.
  721. * @param $item
  722. * The field value to display, as found in
  723. * $entity->field_name[$langcode][$delta].
  724. * @param $display
  725. * Can be either the name of a view mode, or an array of display settings.
  726. * See field_view_field() for more information.
  727. * @param $langcode
  728. * (Optional) The language of the value in $item. If not provided, the
  729. * current language will be assumed.
  730. * @return
  731. * A renderable array for the field value.
  732. */
  733. function field_view_value($entity_type, $entity, $field_name, $item, $display = array(), $langcode = NULL) {
  734. $output = array();
  735. if ($field = field_info_field($field_name)) {
  736. // Determine the langcode that will be used by language fallback.
  737. $langcode = field_language($entity_type, $entity, $field_name, $langcode);
  738. // Push the item as the single value for the field, and defer to
  739. // field_view_field() to build the render array for the whole field.
  740. $clone = clone $entity;
  741. $clone->{$field_name}[$langcode] = array($item);
  742. $elements = field_view_field($entity_type, $clone, $field_name, $display, $langcode);
  743. // Extract the part of the render array we need.
  744. $output = isset($elements[0]) ? $elements[0] : array();
  745. if (isset($elements['#access'])) {
  746. $output['#access'] = $elements['#access'];
  747. }
  748. }
  749. return $output;
  750. }
  751. /**
  752. * Returns a renderable array for the value of a single field in an entity.
  753. *
  754. * The resulting output is a fully themed field with label and multiple values.
  755. *
  756. * This function can be used by third-party modules that need to output an
  757. * isolated field.
  758. * - Do not use inside node (or any other entity) templates; use
  759. * render($content[FIELD_NAME]) instead.
  760. * - Do not use to display all fields in an entity; use
  761. * field_attach_prepare_view() and field_attach_view() instead.
  762. * - The field_view_value() function can be used to output a single formatted
  763. * field value, without label or wrapping field markup.
  764. *
  765. * The function takes care of invoking the prepare_view steps. It also respects
  766. * field access permissions.
  767. *
  768. * @param $entity_type
  769. * The type of $entity; e.g., 'node' or 'user'.
  770. * @param $entity
  771. * The entity containing the field to display. Must at least contain the id
  772. * key and the field data to display.
  773. * @param $field_name
  774. * The name of the field to display.
  775. * @param $display
  776. * Can be either:
  777. * - The name of a view mode. The field will be displayed according to the
  778. * display settings specified for this view mode in the $instance
  779. * definition for the field in the entity's bundle.
  780. * If no display settings are found for the view mode, the settings for
  781. * the 'default' view mode will be used.
  782. * - An array of display settings, as found in the 'display' entry of
  783. * $instance definitions. The following key/value pairs are allowed:
  784. * - label: (string) Position of the label. The default 'field' theme
  785. * implementation supports the values 'inline', 'above' and 'hidden'.
  786. * Defaults to 'above'.
  787. * - type: (string) The formatter to use. Defaults to the
  788. * 'default_formatter' for the field type, specified in
  789. * hook_field_info(). The default formatter will also be used if the
  790. * requested formatter is not available.
  791. * - settings: (array) Settings specific to the formatter. Defaults to the
  792. * formatter's default settings, specified in
  793. * hook_field_formatter_info().
  794. * - weight: (float) The weight to assign to the renderable element.
  795. * Defaults to 0.
  796. * @param $langcode
  797. * (Optional) The language the field values are to be shown in. The site's
  798. * current language fallback logic will be applied no values are available
  799. * for the language. If no language is provided the current language will be
  800. * used.
  801. * @return
  802. * A renderable array for the field value.
  803. *
  804. * @see field_view_value()
  805. */
  806. function field_view_field($entity_type, $entity, $field_name, $display = array(), $langcode = NULL) {
  807. $output = array();
  808. if ($field = field_info_field($field_name)) {
  809. if (is_array($display)) {
  810. // When using custom display settings, fill in default values.
  811. $cache = _field_info_field_cache();
  812. $display = $cache->prepareInstanceDisplay($display, $field["type"]);
  813. }
  814. // Hook invocations are done through the _field_invoke() functions in
  815. // 'single field' mode, to reuse the language fallback logic.
  816. // Determine the actual language to display for the field, given the
  817. // languages available in the field data.
  818. $display_language = field_language($entity_type, $entity, $field_name, $langcode);
  819. $options = array('field_name' => $field_name, 'language' => $display_language);
  820. $null = NULL;
  821. // Invoke prepare_view steps if needed.
  822. if (empty($entity->_field_view_prepared)) {
  823. list($id) = entity_extract_ids($entity_type, $entity);
  824. // First let the field types do their preparation.
  825. _field_invoke_multiple('prepare_view', $entity_type, array($id => $entity), $display, $null, $options);
  826. // Then let the formatters do their own specific massaging.
  827. _field_invoke_multiple_default('prepare_view', $entity_type, array($id => $entity), $display, $null, $options);
  828. }
  829. // Build the renderable array.
  830. $result = _field_invoke_default('view', $entity_type, $entity, $display, $null, $options);
  831. // Invoke hook_field_attach_view_alter() to let other modules alter the
  832. // renderable array, as in a full field_attach_view() execution.
  833. $context = array(
  834. 'entity_type' => $entity_type,
  835. 'entity' => $entity,
  836. 'view_mode' => '_custom',
  837. 'display' => $display,
  838. );
  839. drupal_alter('field_attach_view', $result, $context);
  840. if (isset($result[$field_name])) {
  841. $output = $result[$field_name];
  842. }
  843. }
  844. return $output;
  845. }
  846. /**
  847. * Returns the field items in the language they currently would be displayed.
  848. *
  849. * @param $entity_type
  850. * The type of $entity; e.g., 'node' or 'user'.
  851. * @param $entity
  852. * The entity containing the data to be displayed.
  853. * @param $field_name
  854. * The field to be displayed.
  855. * @param $langcode
  856. * (optional) The language code $entity->{$field_name} has to be displayed in.
  857. * Defaults to the current language.
  858. *
  859. * @return
  860. * An array of field items keyed by delta if available, FALSE otherwise.
  861. */
  862. function field_get_items($entity_type, $entity, $field_name, $langcode = NULL) {
  863. $langcode = field_language($entity_type, $entity, $field_name, $langcode);
  864. return isset($entity->{$field_name}[$langcode]) ? $entity->{$field_name}[$langcode] : FALSE;
  865. }
  866. /**
  867. * Determine whether a field has any data.
  868. *
  869. * @param $field
  870. * A field structure.
  871. * @return
  872. * TRUE if the field has data for any entity; FALSE otherwise.
  873. */
  874. function field_has_data($field) {
  875. $query = new EntityFieldQuery();
  876. $query = $query->fieldCondition($field)
  877. ->range(0, 1)
  878. ->count()
  879. // Neutralize the 'entity_field_access' query tag added by
  880. // field_sql_storage_field_storage_query(). The result cannot depend on the
  881. // access grants of the current user.
  882. ->addTag('DANGEROUS_ACCESS_CHECK_OPT_OUT');
  883. return (bool) $query
  884. ->execute() || (bool) $query
  885. ->age(FIELD_LOAD_REVISION)
  886. ->execute();
  887. }
  888. /**
  889. * Determine whether the user has access to a given field.
  890. *
  891. * This function does not determine whether access is granted to the entity
  892. * itself, only the specific field. Callers are responsible for ensuring that
  893. * entity access is also respected. For example, when checking field access for
  894. * nodes, check node_access() before checking field_access(), and when checking
  895. * field access for entities using the Entity API contributed module,
  896. * check entity_access() before checking field_access().
  897. *
  898. * @param $op
  899. * The operation to be performed. Possible values:
  900. * - 'edit'
  901. * - 'view'
  902. * @param $field
  903. * The full field structure array for the field on which the operation is to
  904. * be performed. See field_info_field().
  905. * @param $entity_type
  906. * The type of $entity; e.g., 'node' or 'user'.
  907. * @param $entity
  908. * (optional) The entity for the operation.
  909. * @param $account
  910. * (optional) The account to check, if not given use currently logged in user.
  911. *
  912. * @return
  913. * TRUE if the operation is allowed;
  914. * FALSE if the operation is denied.
  915. */
  916. function field_access($op, $field, $entity_type, $entity = NULL, $account = NULL) {
  917. global $user;
  918. if (!isset($account)) {
  919. $account = $user;
  920. }
  921. foreach (module_implements('field_access') as $module) {
  922. $function = $module . '_field_access';
  923. $access = $function($op, $field, $entity_type, $entity, $account);
  924. if ($access === FALSE) {
  925. return FALSE;
  926. }
  927. }
  928. return TRUE;
  929. }
  930. /**
  931. * Helper function to extract the bundle name of from a bundle object.
  932. *
  933. * @param $entity_type
  934. * The type of $entity; e.g., 'node' or 'user'.
  935. * @param $bundle
  936. * The bundle object (or string if bundles for this entity type do not exist
  937. * as standalone objects).
  938. * @return
  939. * The bundle name.
  940. */
  941. function field_extract_bundle($entity_type, $bundle) {
  942. if (is_string($bundle)) {
  943. return $bundle;
  944. }
  945. $info = entity_get_info($entity_type);
  946. if (is_object($bundle) && isset($info['bundle keys']['bundle']) && isset($bundle->{$info['bundle keys']['bundle']})) {
  947. return $bundle->{$info['bundle keys']['bundle']};
  948. }
  949. }
  950. /**
  951. * Theme preprocess function for theme_field() and field.tpl.php.
  952. *
  953. * @see theme_field()
  954. * @see field.tpl.php
  955. */
  956. function template_preprocess_field(&$variables, $hook) {
  957. $element = $variables['element'];
  958. // There's some overhead in calling check_plain() so only call it if the label
  959. // variable is being displayed. Otherwise, set it to NULL to avoid PHP
  960. // warnings if a theme implementation accesses the variable even when it's
  961. // supposed to be hidden. If a theme implementation needs to print a hidden
  962. // label, it needs to supply a preprocess function that sets it to the
  963. // sanitized element title or whatever else is wanted in its place.
  964. $variables['label_hidden'] = ($element['#label_display'] == 'hidden');
  965. $variables['label'] = $variables['label_hidden'] ? NULL : check_plain($element['#title']);
  966. // We want other preprocess functions and the theme implementation to have
  967. // fast access to the field item render arrays. The item render array keys
  968. // (deltas) should always be a subset of the keys in #items, and looping on
  969. // those keys is faster than calling element_children() or looping on all keys
  970. // within $element, since that requires traversal of all element properties.
  971. $variables['items'] = array();
  972. foreach ($element['#items'] as $delta => $item) {
  973. if (!empty($element[$delta])) {
  974. $variables['items'][$delta] = $element[$delta];
  975. }
  976. }
  977. // Add default CSS classes. Since there can be many fields rendered on a page,
  978. // save some overhead by calling strtr() directly instead of
  979. // drupal_html_class().
  980. $variables['field_name_css'] = strtr($element['#field_name'], '_', '-');
  981. $variables['field_type_css'] = strtr($element['#field_type'], '_', '-');
  982. $variables['classes_array'] = array(
  983. 'field',
  984. 'field-name-' . $variables['field_name_css'],
  985. 'field-type-' . $variables['field_type_css'],
  986. 'field-label-' . $element['#label_display'],
  987. );
  988. // Add a "clearfix" class to the wrapper since we float the label and the
  989. // field items in field.css if the label is inline.
  990. if ($element['#label_display'] == 'inline') {
  991. $variables['classes_array'][] = 'clearfix';
  992. }
  993. // Add specific suggestions that can override the default implementation.
  994. $variables['theme_hook_suggestions'] = array(
  995. 'field__' . $element['#field_type'],
  996. 'field__' . $element['#field_name'],
  997. 'field__' . $element['#bundle'],
  998. 'field__' . $element['#field_name'] . '__' . $element['#bundle'],
  999. );
  1000. }
  1001. /**
  1002. * Theme process function for theme_field() and field.tpl.php.
  1003. *
  1004. * @see theme_field()
  1005. * @see field.tpl.php
  1006. */
  1007. function template_process_field(&$variables, $hook) {
  1008. // The default theme implementation is a function, so template_process() does
  1009. // not automatically run, so we need to flatten the classes and attributes
  1010. // here. For best performance, only call drupal_attributes() when needed, and
  1011. // note that template_preprocess_field() does not initialize the
  1012. // *_attributes_array variables.
  1013. $variables['classes'] = implode(' ', $variables['classes_array']);
  1014. $variables['attributes'] = empty($variables['attributes_array']) ? '' : drupal_attributes($variables['attributes_array']);
  1015. $variables['title_attributes'] = empty($variables['title_attributes_array']) ? '' : drupal_attributes($variables['title_attributes_array']);
  1016. $variables['content_attributes'] = empty($variables['content_attributes_array']) ? '' : drupal_attributes($variables['content_attributes_array']);
  1017. foreach ($variables['items'] as $delta => $item) {
  1018. $variables['item_attributes'][$delta] = empty($variables['item_attributes_array'][$delta]) ? '' : drupal_attributes($variables['item_attributes_array'][$delta]);
  1019. }
  1020. }
  1021. /**
  1022. * @} End of "defgroup field".
  1023. */
  1024. /**
  1025. * Returns HTML for a field.
  1026. *
  1027. * This is the default theme implementation to display the value of a field.
  1028. * Theme developers who are comfortable with overriding theme functions may do
  1029. * so in order to customize this markup. This function can be overridden with
  1030. * varying levels of specificity. For example, for a field named 'body'
  1031. * displayed on the 'article' content type, any of the following functions will
  1032. * override this default implementation. The first of these functions that
  1033. * exists is used:
  1034. * - THEMENAME_field__body__article()
  1035. * - THEMENAME_field__article()
  1036. * - THEMENAME_field__body()
  1037. * - THEMENAME_field()
  1038. *
  1039. * Theme developers who prefer to customize templates instead of overriding
  1040. * functions may copy the "field.tpl.php" from the "modules/field/theme" folder
  1041. * of the Drupal installation to somewhere within the theme's folder and
  1042. * customize it, just like customizing other Drupal templates such as
  1043. * page.tpl.php or node.tpl.php. However, it takes longer for the server to
  1044. * process templates than to call a function, so for websites with many fields
  1045. * displayed on a page, this can result in a noticeable slowdown of the website.
  1046. * For these websites, developers are discouraged from placing a field.tpl.php
  1047. * file into the theme's folder, but may customize templates for specific
  1048. * fields. For example, for a field named 'body' displayed on the 'article'
  1049. * content type, any of the following templates will override this default
  1050. * implementation. The first of these templates that exists is used:
  1051. * - field--body--article.tpl.php
  1052. * - field--article.tpl.php
  1053. * - field--body.tpl.php
  1054. * - field.tpl.php
  1055. * So, if the body field on the article content type needs customization, a
  1056. * field--body--article.tpl.php file can be added within the theme's folder.
  1057. * Because it's a template, it will result in slightly more time needed to
  1058. * display that field, but it will not impact other fields, and therefore,
  1059. * is unlikely to cause a noticeable change in website performance. A very rough
  1060. * guideline is that if a page is being displayed with more than 100 fields and
  1061. * they are all themed with a template instead of a function, it can add up to
  1062. * 5% to the time it takes to display that page. This is a guideline only and
  1063. * the exact performance impact depends on the server configuration and the
  1064. * details of the website.
  1065. *
  1066. * @param $variables
  1067. * An associative array containing:
  1068. * - label_hidden: A boolean indicating to show or hide the field label.
  1069. * - title_attributes: A string containing the attributes for the title.
  1070. * - label: The label for the field.
  1071. * - content_attributes: A string containing the attributes for the content's
  1072. * div.
  1073. * - items: An array of field items.
  1074. * - item_attributes: An array of attributes for each item.
  1075. * - classes: A string containing the classes for the wrapping div.
  1076. * - attributes: A string containing the attributes for the wrapping div.
  1077. *
  1078. * @see template_preprocess_field()
  1079. * @see template_process_field()
  1080. * @see field.tpl.php
  1081. *
  1082. * @ingroup themeable
  1083. */
  1084. function theme_field($variables) {
  1085. $output = '';
  1086. // Render the label, if it's not hidden.
  1087. if (!$variables['label_hidden']) {
  1088. $output .= '<div class="field-label"' . $variables['title_attributes'] . '>' . $variables['label'] . ':&nbsp;</div>';
  1089. }
  1090. // Render the items.
  1091. $output .= '<div class="field-items"' . $variables['content_attributes'] . '>';
  1092. foreach ($variables['items'] as $delta => $item) {
  1093. $classes = 'field-item ' . ($delta % 2 ? 'odd' : 'even');
  1094. $output .= '<div class="' . $classes . '"' . $variables['item_attributes'][$delta] . '>' . drupal_render($item) . '</div>';
  1095. }
  1096. $output .= '</div>';
  1097. // Render the top-level DIV.
  1098. $output = '<div class="' . $variables['classes'] . '"' . $variables['attributes'] . '>' . $output . '</div>';
  1099. return $output;
  1100. }
  1101. /**
  1102. * DEPRECATED: Helper form element validator: integer.
  1103. *
  1104. * Use element_validate_integer() instead.
  1105. *
  1106. * @deprecated
  1107. * @see element_validate_integer()
  1108. */
  1109. function _element_validate_integer($element, &$form_state) {
  1110. element_validate_integer($element, $form_state);
  1111. }
  1112. /**
  1113. * DEPRECATED: Helper form element validator: integer > 0.
  1114. *
  1115. * Use element_validate_integer_positive() instead.
  1116. *
  1117. * @deprecated
  1118. * @see element_validate_integer_positive()
  1119. */
  1120. function _element_validate_integer_positive($element, &$form_state) {
  1121. element_validate_integer_positive($element, $form_state);
  1122. }
  1123. /**
  1124. * DEPRECATED: Helper form element validator: number.
  1125. *
  1126. * Use element_validate_number() instead.
  1127. *
  1128. * @deprecated
  1129. * @see element_validate_number()
  1130. */
  1131. function _element_validate_number($element, &$form_state) {
  1132. element_validate_number($element, $form_state);
  1133. }