field.module 49 KB

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