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_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 !link', array('!link' => l(t('Field list'), '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 other entities) 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. $display = _field_info_prepare_instance_display($field, $display);
  822. }
  823. // Hook invocations are done through the _field_invoke() functions in
  824. // 'single field' mode, to reuse the language fallback logic.
  825. // Determine the actual language to display for the field, given the
  826. // languages available in the field data.
  827. $display_language = field_language($entity_type, $entity, $field_name, $langcode);
  828. $options = array('field_name' => $field_name, 'language' => $display_language);
  829. $null = NULL;
  830. // Invoke prepare_view steps if needed.
  831. if (empty($entity->_field_view_prepared)) {
  832. list($id) = entity_extract_ids($entity_type, $entity);
  833. // First let the field types do their preparation.
  834. _field_invoke_multiple('prepare_view', $entity_type, array($id => $entity), $display, $null, $options);
  835. // Then let the formatters do their own specific massaging.
  836. _field_invoke_multiple_default('prepare_view', $entity_type, array($id => $entity), $display, $null, $options);
  837. }
  838. // Build the renderable array.
  839. $result = _field_invoke_default('view', $entity_type, $entity, $display, $null, $options);
  840. // Invoke hook_field_attach_view_alter() to let other modules alter the
  841. // renderable array, as in a full field_attach_view() execution.
  842. $context = array(
  843. 'entity_type' => $entity_type,
  844. 'entity' => $entity,
  845. 'view_mode' => '_custom',
  846. 'display' => $display,
  847. );
  848. drupal_alter('field_attach_view', $result, $context);
  849. if (isset($result[$field_name])) {
  850. $output = $result[$field_name];
  851. }
  852. }
  853. return $output;
  854. }
  855. /**
  856. * Returns the field items in the language they currently would be displayed.
  857. *
  858. * @param $entity_type
  859. * The type of $entity; e.g., 'node' or 'user'.
  860. * @param $entity
  861. * The entity containing the data to be displayed.
  862. * @param $field_name
  863. * The field to be displayed.
  864. * @param $langcode
  865. * (optional) The language code $entity->{$field_name} has to be displayed in.
  866. * Defaults to the current language.
  867. *
  868. * @return
  869. * An array of field items keyed by delta if available, FALSE otherwise.
  870. */
  871. function field_get_items($entity_type, $entity, $field_name, $langcode = NULL) {
  872. $langcode = field_language($entity_type, $entity, $field_name, $langcode);
  873. return isset($entity->{$field_name}[$langcode]) ? $entity->{$field_name}[$langcode] : FALSE;
  874. }
  875. /**
  876. * Determine whether a field has any data.
  877. *
  878. * @param $field
  879. * A field structure.
  880. * @return
  881. * TRUE if the field has data for any entity; FALSE otherwise.
  882. */
  883. function field_has_data($field) {
  884. $query = new EntityFieldQuery();
  885. return (bool) $query
  886. ->fieldCondition($field)
  887. ->range(0, 1)
  888. ->count()
  889. // Neutralize the 'entity_field_access' query tag added by
  890. // field_sql_storage_field_storage_query(). The result cannot depend on the
  891. // access grants of the current user.
  892. ->addTag('DANGEROUS_ACCESS_CHECK_OPT_OUT')
  893. ->execute();
  894. }
  895. /**
  896. * Determine whether the user has access to a given field.
  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_number_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. }