field.api.php 97 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743
  1. <?php
  2. /**
  3. * @file
  4. * Hooks provided by the Field module.
  5. */
  6. /**
  7. * @addtogroup hooks
  8. * @{
  9. */
  10. /**
  11. * Exposes "pseudo-field" components on fieldable entities.
  12. *
  13. * Field UI's "Manage fields" and "Manage display" pages let users re-order
  14. * fields, but also non-field components. For nodes, these include the title,
  15. * poll choices, and other elements exposed by modules through hook_form() or
  16. * hook_form_alter().
  17. *
  18. * Fieldable entities or modules that want to have their components supported
  19. * should expose them using this hook. The user-defined settings (weight,
  20. * visible) are automatically applied on rendered forms and displayed
  21. * entities in a #pre_render callback added by field_attach_form() and
  22. * field_attach_view().
  23. *
  24. * @see _field_extra_fields_pre_render()
  25. * @see hook_field_extra_fields_alter()
  26. *
  27. * @return
  28. * A nested array of 'pseudo-field' elements. Each list is nested within the
  29. * following keys: entity type, bundle name, context (either 'form' or
  30. * 'display'). The keys are the name of the elements as appearing in the
  31. * renderable array (either the entity form or the displayed entity). The
  32. * value is an associative array:
  33. * - label: The human readable name of the element.
  34. * - description: A short description of the element contents.
  35. * - weight: The default weight of the element.
  36. * - edit: (optional) String containing markup (normally a link) used as the
  37. * element's 'edit' operation in the administration interface. Only for
  38. * 'form' context.
  39. * - delete: (optional) String containing markup (normally a link) used as the
  40. * element's 'delete' operation in the administration interface. Only for
  41. * 'form' context.
  42. *
  43. * @ingroup field_types
  44. */
  45. function hook_field_extra_fields() {
  46. $extra['node']['poll'] = array(
  47. 'form' => array(
  48. 'choice_wrapper' => array(
  49. 'label' => t('Poll choices'),
  50. 'description' => t('Poll choices'),
  51. 'weight' => -4,
  52. ),
  53. 'settings' => array(
  54. 'label' => t('Poll settings'),
  55. 'description' => t('Poll module settings'),
  56. 'weight' => -3,
  57. ),
  58. ),
  59. 'display' => array(
  60. 'poll_view_voting' => array(
  61. 'label' => t('Poll vote'),
  62. 'description' => t('Poll vote'),
  63. 'weight' => 0,
  64. ),
  65. 'poll_view_results' => array(
  66. 'label' => t('Poll results'),
  67. 'description' => t('Poll results'),
  68. 'weight' => 0,
  69. ),
  70. )
  71. );
  72. return $extra;
  73. }
  74. /**
  75. * Alter "pseudo-field" components on fieldable entities.
  76. *
  77. * @param $info
  78. * The associative array of 'pseudo-field' components.
  79. *
  80. * @see hook_field_extra_fields()
  81. *
  82. * @ingroup field_types
  83. */
  84. function hook_field_extra_fields_alter(&$info) {
  85. // Force node title to always be at the top of the list by default.
  86. foreach (node_type_get_types() as $bundle) {
  87. if (isset($info['node'][$bundle->type]['form']['title'])) {
  88. $info['node'][$bundle->type]['form']['title']['weight'] = -20;
  89. }
  90. }
  91. }
  92. /**
  93. * @defgroup field_types Field Types API
  94. * @{
  95. * Define field types.
  96. *
  97. * In the Field API, each field has a type, which determines what kind of data
  98. * (integer, string, date, etc.) the field can hold, which settings it provides,
  99. * and so on. The data type(s) accepted by a field are defined in
  100. * hook_field_schema(); other basic properties of a field are defined in
  101. * hook_field_info(). The other hooks below are called by the Field Attach API
  102. * to perform field-type-specific actions.
  103. *
  104. * The Field Types API also defines two kinds of pluggable handlers: widgets
  105. * and formatters. @link field_widget Widgets @endlink specify how the field
  106. * appears in edit forms, while @link field_formatter formatters @endlink
  107. * specify how the field appears in displayed entities.
  108. *
  109. * A third kind of pluggable handlers, storage backends, is defined by the
  110. * @link field_storage Field Storage API @endlink.
  111. *
  112. * See @link field Field API @endlink for information about the other parts of
  113. * the Field API.
  114. */
  115. /**
  116. * Define Field API field types.
  117. *
  118. * Along with this hook, you also need to implement other hooks. See
  119. * @link field_types Field Types API @endlink for more information.
  120. *
  121. * @return
  122. * An array whose keys are field type names and whose values are arrays
  123. * describing the field type, with the following key/value pairs:
  124. * - label: The human-readable name of the field type.
  125. * - description: A short description for the field type.
  126. * - settings: An array whose keys are the names of the settings available
  127. * for the field type, and whose values are the default values for those
  128. * settings.
  129. * - instance_settings: An array whose keys are the names of the settings
  130. * available for instances of the field type, and whose values are the
  131. * default values for those settings. Instance-level settings can have
  132. * different values on each field instance, and thus allow greater
  133. * flexibility than field-level settings. It is recommended to put settings
  134. * at the instance level whenever possible. Notable exceptions: settings
  135. * acting on the schema definition, or settings that Views needs to use
  136. * across field instances (for example, the list of allowed values).
  137. * - default_widget: The machine name of the default widget to be used by
  138. * instances of this field type, when no widget is specified in the
  139. * instance definition. This widget must be available whenever the field
  140. * type is available (i.e. provided by the field type module, or by a module
  141. * the field type module depends on).
  142. * - default_formatter: The machine name of the default formatter to be used
  143. * by instances of this field type, when no formatter is specified in the
  144. * instance definition. This formatter must be available whenever the field
  145. * type is available (i.e. provided by the field type module, or by a module
  146. * the field type module depends on).
  147. * - no_ui: (optional) A boolean specifying that users should not be allowed
  148. * to create fields and instances of this field type through the UI. Such
  149. * fields can only be created programmatically with field_create_field()
  150. * and field_create_instance(). Defaults to FALSE.
  151. *
  152. * @see hook_field_info_alter()
  153. */
  154. function hook_field_info() {
  155. return array(
  156. 'text' => array(
  157. 'label' => t('Text'),
  158. 'description' => t('This field stores varchar text in the database.'),
  159. 'settings' => array('max_length' => 255),
  160. 'instance_settings' => array('text_processing' => 0),
  161. 'default_widget' => 'text_textfield',
  162. 'default_formatter' => 'text_default',
  163. ),
  164. 'text_long' => array(
  165. 'label' => t('Long text'),
  166. 'description' => t('This field stores long text in the database.'),
  167. 'settings' => array('max_length' => ''),
  168. 'instance_settings' => array('text_processing' => 0),
  169. 'default_widget' => 'text_textarea',
  170. 'default_formatter' => 'text_default',
  171. ),
  172. 'text_with_summary' => array(
  173. 'label' => t('Long text and summary'),
  174. 'description' => t('This field stores long text in the database along with optional summary text.'),
  175. 'settings' => array('max_length' => ''),
  176. 'instance_settings' => array('text_processing' => 1, 'display_summary' => 0),
  177. 'default_widget' => 'text_textarea_with_summary',
  178. 'default_formatter' => 'text_summary_or_trimmed',
  179. ),
  180. );
  181. }
  182. /**
  183. * Perform alterations on Field API field types.
  184. *
  185. * @param $info
  186. * Array of information on field types exposed by hook_field_info()
  187. * implementations.
  188. */
  189. function hook_field_info_alter(&$info) {
  190. // Add a setting to all field types.
  191. foreach ($info as $field_type => $field_type_info) {
  192. $info[$field_type]['settings'] += array(
  193. 'mymodule_additional_setting' => 'default value',
  194. );
  195. }
  196. // Change the default widget for fields of type 'foo'.
  197. if (isset($info['foo'])) {
  198. $info['foo']['default widget'] = 'mymodule_widget';
  199. }
  200. }
  201. /**
  202. * Define the Field API schema for a field structure.
  203. *
  204. * This is invoked when a field is created, in order to obtain the database
  205. * schema from the module that defines the field's type.
  206. *
  207. * This hook must be defined in the module's .install file for it to be detected
  208. * during installation and upgrade.
  209. *
  210. * @param $field
  211. * A field structure.
  212. *
  213. * @return
  214. * An associative array with the following keys:
  215. * - columns: An array of Schema API column specifications, keyed by column
  216. * name. This specifies what comprises a value for a given field. For
  217. * example, a value for a number field is simply 'value', while a value for
  218. * a formatted text field is the combination of 'value' and 'format'. It is
  219. * recommended to avoid having the column definitions depend on field
  220. * settings when possible. No assumptions should be made on how storage
  221. * engines internally use the original column name to structure their
  222. * storage.
  223. * - indexes: (optional) An array of Schema API indexes definitions. Only
  224. * columns that appear in the 'columns' array are allowed. Those indexes
  225. * will be used as default indexes. Callers of field_create_field() can
  226. * specify additional indexes, or, at their own risk, modify the default
  227. * indexes specified by the field-type module. Some storage engines might
  228. * not support indexes.
  229. * - foreign keys: (optional) An array of Schema API foreign keys
  230. * definitions.
  231. */
  232. function hook_field_schema($field) {
  233. if ($field['type'] == 'text_long') {
  234. $columns = array(
  235. 'value' => array(
  236. 'type' => 'text',
  237. 'size' => 'big',
  238. 'not null' => FALSE,
  239. ),
  240. );
  241. }
  242. else {
  243. $columns = array(
  244. 'value' => array(
  245. 'type' => 'varchar',
  246. 'length' => $field['settings']['max_length'],
  247. 'not null' => FALSE,
  248. ),
  249. );
  250. }
  251. $columns += array(
  252. 'format' => array(
  253. 'type' => 'varchar',
  254. 'length' => 255,
  255. 'not null' => FALSE,
  256. ),
  257. );
  258. return array(
  259. 'columns' => $columns,
  260. 'indexes' => array(
  261. 'format' => array('format'),
  262. ),
  263. 'foreign keys' => array(
  264. 'format' => array(
  265. 'table' => 'filter_format',
  266. 'columns' => array('format' => 'format'),
  267. ),
  268. ),
  269. );
  270. }
  271. /**
  272. * Define custom load behavior for this module's field types.
  273. *
  274. * Unlike most other field hooks, this hook operates on multiple entities. The
  275. * $entities, $instances and $items parameters are arrays keyed by entity ID.
  276. * For performance reasons, information for all available entity should be
  277. * loaded in a single query where possible.
  278. *
  279. * Note that the changes made to the field values get cached by the field cache
  280. * for subsequent loads. You should never use this hook to load fieldable
  281. * entities, since this is likely to cause infinite recursions when
  282. * hook_field_load() is run on those as well. Use
  283. * hook_field_formatter_prepare_view() instead.
  284. *
  285. * Make changes or additions to field values by altering the $items parameter by
  286. * reference. There is no return value.
  287. *
  288. * @param $entity_type
  289. * The type of $entity.
  290. * @param $entities
  291. * Array of entities being loaded, keyed by entity ID.
  292. * @param $field
  293. * The field structure for the operation.
  294. * @param $instances
  295. * Array of instance structures for $field for each entity, keyed by entity
  296. * ID.
  297. * @param $langcode
  298. * The language code associated with $items.
  299. * @param $items
  300. * Array of field values already loaded for the entities, keyed by entity ID.
  301. * Store your changes in this parameter (passed by reference).
  302. * @param $age
  303. * FIELD_LOAD_CURRENT to load the most recent revision for all fields, or
  304. * FIELD_LOAD_REVISION to load the version indicated by each entity.
  305. */
  306. function hook_field_load($entity_type, $entities, $field, $instances, $langcode, &$items, $age) {
  307. // Sample code from text.module: precompute sanitized strings so they are
  308. // stored in the field cache.
  309. foreach ($entities as $id => $entity) {
  310. foreach ($items[$id] as $delta => $item) {
  311. // Only process items with a cacheable format, the rest will be handled
  312. // by formatters if needed.
  313. if (empty($instances[$id]['settings']['text_processing']) || filter_format_allowcache($item['format'])) {
  314. $items[$id][$delta]['safe_value'] = isset($item['value']) ? _text_sanitize($instances[$id], $langcode, $item, 'value') : '';
  315. if ($field['type'] == 'text_with_summary') {
  316. $items[$id][$delta]['safe_summary'] = isset($item['summary']) ? _text_sanitize($instances[$id], $langcode, $item, 'summary') : '';
  317. }
  318. }
  319. }
  320. }
  321. }
  322. /**
  323. * Prepare field values prior to display.
  324. *
  325. * This hook is invoked before the field values are handed to formatters
  326. * for display, and runs before the formatters' own
  327. * hook_field_formatter_prepare_view().
  328. *
  329. * Unlike most other field hooks, this hook operates on multiple entities. The
  330. * $entities, $instances and $items parameters are arrays keyed by entity ID.
  331. * For performance reasons, information for all available entities should be
  332. * loaded in a single query where possible.
  333. *
  334. * Make changes or additions to field values by altering the $items parameter by
  335. * reference. There is no return value.
  336. *
  337. * @param $entity_type
  338. * The type of $entity.
  339. * @param $entities
  340. * Array of entities being displayed, keyed by entity ID.
  341. * @param $field
  342. * The field structure for the operation.
  343. * @param $instances
  344. * Array of instance structures for $field for each entity, keyed by entity
  345. * ID.
  346. * @param $langcode
  347. * The language associated to $items.
  348. * @param $items
  349. * $entity->{$field['field_name']}, or an empty array if unset.
  350. */
  351. function hook_field_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items) {
  352. // Sample code from image.module: if there are no images specified at all,
  353. // use the default image.
  354. foreach ($entities as $id => $entity) {
  355. if (empty($items[$id]) && $field['settings']['default_image']) {
  356. if ($file = file_load($field['settings']['default_image'])) {
  357. $items[$id][0] = (array) $file + array(
  358. 'is_default' => TRUE,
  359. 'alt' => '',
  360. 'title' => '',
  361. );
  362. }
  363. }
  364. }
  365. }
  366. /**
  367. * Validate this module's field data.
  368. *
  369. * If there are validation problems, add to the $errors array (passed by
  370. * reference). There is no return value.
  371. *
  372. * @param $entity_type
  373. * The type of $entity.
  374. * @param $entity
  375. * The entity for the operation.
  376. * @param $field
  377. * The field structure for the operation.
  378. * @param $instance
  379. * The instance structure for $field on $entity's bundle.
  380. * @param $langcode
  381. * The language associated with $items.
  382. * @param $items
  383. * $entity->{$field['field_name']}[$langcode], or an empty array if unset.
  384. * @param $errors
  385. * The array of errors (keyed by field name, language code, and delta) that
  386. * have already been reported for the entity. The function should add its
  387. * errors to this array. Each error is an associative array with the following
  388. * keys and values:
  389. * - error: An error code (should be a string prefixed with the module name).
  390. * - message: The human readable message to be displayed.
  391. */
  392. function hook_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
  393. foreach ($items as $delta => $item) {
  394. if (!empty($item['value'])) {
  395. if (!empty($field['settings']['max_length']) && drupal_strlen($item['value']) > $field['settings']['max_length']) {
  396. $errors[$field['field_name']][$langcode][$delta][] = array(
  397. 'error' => 'text_max_length',
  398. 'message' => t('%name: the value may not be longer than %max characters.', array('%name' => $instance['label'], '%max' => $field['settings']['max_length'])),
  399. );
  400. }
  401. }
  402. }
  403. }
  404. /**
  405. * Define custom presave behavior for this module's field types.
  406. *
  407. * Make changes or additions to field values by altering the $items parameter by
  408. * reference. There is no return value.
  409. *
  410. * @param $entity_type
  411. * The type of $entity.
  412. * @param $entity
  413. * The entity for the operation.
  414. * @param $field
  415. * The field structure for the operation.
  416. * @param $instance
  417. * The instance structure for $field on $entity's bundle.
  418. * @param $langcode
  419. * The language associated with $items.
  420. * @param $items
  421. * $entity->{$field['field_name']}[$langcode], or an empty array if unset.
  422. */
  423. function hook_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
  424. if ($field['type'] == 'number_decimal') {
  425. // Let PHP round the value to ensure consistent behavior across storage
  426. // backends.
  427. foreach ($items as $delta => $item) {
  428. if (isset($item['value'])) {
  429. $items[$delta]['value'] = round($item['value'], $field['settings']['scale']);
  430. }
  431. }
  432. }
  433. }
  434. /**
  435. * Define custom insert behavior for this module's field data.
  436. *
  437. * This hook is invoked from field_attach_insert() on the module that defines a
  438. * field, during the process of inserting an entity object (node, taxonomy term,
  439. * etc.). It is invoked just before the data for this field on the particular
  440. * entity object is inserted into field storage. Only field modules that are
  441. * storing or tracking information outside the standard field storage mechanism
  442. * need to implement this hook.
  443. *
  444. * @param $entity_type
  445. * The type of $entity.
  446. * @param $entity
  447. * The entity for the operation.
  448. * @param $field
  449. * The field structure for the operation.
  450. * @param $instance
  451. * The instance structure for $field on $entity's bundle.
  452. * @param $langcode
  453. * The language associated with $items.
  454. * @param $items
  455. * $entity->{$field['field_name']}[$langcode], or an empty array if unset.
  456. *
  457. * @see hook_field_update()
  458. * @see hook_field_delete()
  459. */
  460. function hook_field_insert($entity_type, $entity, $field, $instance, $langcode, &$items) {
  461. if (variable_get('taxonomy_maintain_index_table', TRUE) && $field['storage']['type'] == 'field_sql_storage' && $entity_type == 'node' && $entity->status) {
  462. $query = db_insert('taxonomy_index')->fields(array('nid', 'tid', 'sticky', 'created', ));
  463. foreach ($items as $item) {
  464. $query->values(array(
  465. 'nid' => $entity->nid,
  466. 'tid' => $item['tid'],
  467. 'sticky' => $entity->sticky,
  468. 'created' => $entity->created,
  469. ));
  470. }
  471. $query->execute();
  472. }
  473. }
  474. /**
  475. * Define custom update behavior for this module's field data.
  476. *
  477. * This hook is invoked from field_attach_update() on the module that defines a
  478. * field, during the process of updating an entity object (node, taxonomy term,
  479. * etc.). It is invoked just before the data for this field on the particular
  480. * entity object is updated into field storage. Only field modules that are
  481. * storing or tracking information outside the standard field storage mechanism
  482. * need to implement this hook.
  483. *
  484. * @param $entity_type
  485. * The type of $entity.
  486. * @param $entity
  487. * The entity for the operation.
  488. * @param $field
  489. * The field structure for the operation.
  490. * @param $instance
  491. * The instance structure for $field on $entity's bundle.
  492. * @param $langcode
  493. * The language associated with $items.
  494. * @param $items
  495. * $entity->{$field['field_name']}[$langcode], or an empty array if unset.
  496. *
  497. * @see hook_field_insert()
  498. * @see hook_field_delete()
  499. */
  500. function hook_field_update($entity_type, $entity, $field, $instance, $langcode, &$items) {
  501. if (variable_get('taxonomy_maintain_index_table', TRUE) && $field['storage']['type'] == 'field_sql_storage' && $entity_type == 'node') {
  502. $first_call = &drupal_static(__FUNCTION__, array());
  503. // We don't maintain data for old revisions, so clear all previous values
  504. // from the table. Since this hook runs once per field, per object, make
  505. // sure we only wipe values once.
  506. if (!isset($first_call[$entity->nid])) {
  507. $first_call[$entity->nid] = FALSE;
  508. db_delete('taxonomy_index')->condition('nid', $entity->nid)->execute();
  509. }
  510. // Only save data to the table if the node is published.
  511. if ($entity->status) {
  512. $query = db_insert('taxonomy_index')->fields(array('nid', 'tid', 'sticky', 'created'));
  513. foreach ($items as $item) {
  514. $query->values(array(
  515. 'nid' => $entity->nid,
  516. 'tid' => $item['tid'],
  517. 'sticky' => $entity->sticky,
  518. 'created' => $entity->created,
  519. ));
  520. }
  521. $query->execute();
  522. }
  523. }
  524. }
  525. /**
  526. * Update the storage information for a field.
  527. *
  528. * This is invoked on the field's storage module from field_update_field(),
  529. * before the new field information is saved to the database. The field storage
  530. * module should update its storage tables to agree with the new field
  531. * information. If there is a problem, the field storage module should throw an
  532. * exception.
  533. *
  534. * @param $field
  535. * The updated field structure to be saved.
  536. * @param $prior_field
  537. * The previously-saved field structure.
  538. * @param $has_data
  539. * TRUE if the field has data in storage currently.
  540. */
  541. function hook_field_storage_update_field($field, $prior_field, $has_data) {
  542. if (!$has_data) {
  543. // There is no data. Re-create the tables completely.
  544. $prior_schema = _field_sql_storage_schema($prior_field);
  545. foreach ($prior_schema as $name => $table) {
  546. db_drop_table($name, $table);
  547. }
  548. $schema = _field_sql_storage_schema($field);
  549. foreach ($schema as $name => $table) {
  550. db_create_table($name, $table);
  551. }
  552. }
  553. else {
  554. // There is data. See field_sql_storage_field_storage_update_field() for
  555. // an example of what to do to modify the schema in place, preserving the
  556. // old data as much as possible.
  557. }
  558. drupal_get_schema(NULL, TRUE);
  559. }
  560. /**
  561. * Define custom delete behavior for this module's field data.
  562. *
  563. * This hook is invoked from field_attach_delete() on the module that defines a
  564. * field, during the process of deleting an entity object (node, taxonomy term,
  565. * etc.). It is invoked just before the data for this field on the particular
  566. * entity object is deleted from field storage. Only field modules that are
  567. * storing or tracking information outside the standard field storage mechanism
  568. * need to implement this hook.
  569. *
  570. * @param $entity_type
  571. * The type of $entity.
  572. * @param $entity
  573. * The entity for the operation.
  574. * @param $field
  575. * The field structure for the operation.
  576. * @param $instance
  577. * The instance structure for $field on $entity's bundle.
  578. * @param $langcode
  579. * The language associated with $items.
  580. * @param $items
  581. * $entity->{$field['field_name']}[$langcode], or an empty array if unset.
  582. *
  583. * @see hook_field_insert()
  584. * @see hook_field_update()
  585. */
  586. function hook_field_delete($entity_type, $entity, $field, $instance, $langcode, &$items) {
  587. list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
  588. foreach ($items as $delta => $item) {
  589. // For hook_file_references(), remember that this is being deleted.
  590. $item['file_field_name'] = $field['field_name'];
  591. // Pass in the ID of the object that is being removed so all references can
  592. // be counted in hook_file_references().
  593. $item['file_field_type'] = $entity_type;
  594. $item['file_field_id'] = $id;
  595. file_field_delete_file($item, $field, $entity_type, $id);
  596. }
  597. }
  598. /**
  599. * Define custom revision delete behavior for this module's field types.
  600. *
  601. * This hook is invoked just before the data is deleted from field storage
  602. * in field_attach_delete_revision(), and will only be called for fieldable
  603. * types that are versioned.
  604. *
  605. * @param $entity_type
  606. * The type of $entity.
  607. * @param $entity
  608. * The entity for the operation.
  609. * @param $field
  610. * The field structure for the operation.
  611. * @param $instance
  612. * The instance structure for $field on $entity's bundle.
  613. * @param $langcode
  614. * The language associated with $items.
  615. * @param $items
  616. * $entity->{$field['field_name']}[$langcode], or an empty array if unset.
  617. */
  618. function hook_field_delete_revision($entity_type, $entity, $field, $instance, $langcode, &$items) {
  619. list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
  620. foreach ($items as $delta => $item) {
  621. // For hook_file_references, remember that this file is being deleted.
  622. $item['file_field_name'] = $field['field_name'];
  623. if (file_field_delete_file($item, $field, $entity_type, $id)) {
  624. $items[$delta] = NULL;
  625. }
  626. }
  627. }
  628. /**
  629. * Define custom prepare_translation behavior for this module's field types.
  630. *
  631. * @param $entity_type
  632. * The type of $entity.
  633. * @param $entity
  634. * The entity for the operation.
  635. * @param $field
  636. * The field structure for the operation.
  637. * @param $instance
  638. * The instance structure for $field on $entity's bundle.
  639. * @param $langcode
  640. * The language associated to $items.
  641. * @param $items
  642. * $entity->{$field['field_name']}[$langcode], or an empty array if unset.
  643. * @param $source_entity
  644. * The source entity from which field values are being copied.
  645. * @param $source_langcode
  646. * The source language from which field values are being copied.
  647. *
  648. * @ingroup field_language
  649. */
  650. function hook_field_prepare_translation($entity_type, $entity, $field, $instance, $langcode, &$items, $source_entity, $source_langcode) {
  651. // If the translating user is not permitted to use the assigned text format,
  652. // we must not expose the source values.
  653. $field_name = $field['field_name'];
  654. $formats = filter_formats();
  655. $format_id = $source_entity->{$field_name}[$source_langcode][0]['format'];
  656. if (!filter_access($formats[$format_id])) {
  657. $items = array();
  658. }
  659. }
  660. /**
  661. * Define what constitutes an empty item for a field type.
  662. *
  663. * @param $item
  664. * An item that may or may not be empty.
  665. * @param $field
  666. * The field to which $item belongs.
  667. *
  668. * @return
  669. * TRUE if $field's type considers $item not to contain any data;
  670. * FALSE otherwise.
  671. */
  672. function hook_field_is_empty($item, $field) {
  673. if (empty($item['value']) && (string) $item['value'] !== '0') {
  674. return TRUE;
  675. }
  676. return FALSE;
  677. }
  678. /**
  679. * @} End of "defgroup field_types".
  680. */
  681. /**
  682. * @defgroup field_widget Field Widget API
  683. * @{
  684. * Define Field API widget types.
  685. *
  686. * Field API widgets specify how fields are displayed in edit forms. Fields of a
  687. * given @link field_types field type @endlink may be edited using more than one
  688. * widget. In this case, the Field UI module allows the site builder to choose
  689. * which widget to use. Widget types are defined by implementing
  690. * hook_field_widget_info().
  691. *
  692. * Widgets are @link forms_api_reference.html Form API @endlink elements with
  693. * additional processing capabilities. Widget hooks are typically called by the
  694. * Field Attach API during the creation of the field form structure with
  695. * field_attach_form().
  696. *
  697. * @see field
  698. * @see field_types
  699. * @see field_formatter
  700. */
  701. /**
  702. * Expose Field API widget types.
  703. *
  704. * @return
  705. * An array describing the widget types implemented by the module.
  706. * The keys are widget type names. To avoid name clashes, widget type
  707. * names should be prefixed with the name of the module that exposes them.
  708. * The values are arrays describing the widget type, with the following
  709. * key/value pairs:
  710. * - label: The human-readable name of the widget type.
  711. * - description: A short description for the widget type.
  712. * - field types: An array of field types the widget supports.
  713. * - settings: An array whose keys are the names of the settings available
  714. * for the widget type, and whose values are the default values for those
  715. * settings.
  716. * - behaviors: (optional) An array describing behaviors of the widget, with
  717. * the following elements:
  718. * - multiple values: One of the following constants:
  719. * - FIELD_BEHAVIOR_DEFAULT: (default) If the widget allows the input of
  720. * one single field value (most common case). The widget will be
  721. * repeated for each value input.
  722. * - FIELD_BEHAVIOR_CUSTOM: If one single copy of the widget can receive
  723. * several field values. Examples: checkboxes, multiple select,
  724. * comma-separated textfield.
  725. * - default value: One of the following constants:
  726. * - FIELD_BEHAVIOR_DEFAULT: (default) If the widget accepts default
  727. * values.
  728. * - FIELD_BEHAVIOR_NONE: if the widget does not support default values.
  729. * - weight: (optional) An integer to determine the weight of this widget
  730. * relative to other widgets in the Field UI when selecting a widget for a
  731. * given field instance.
  732. *
  733. * @see hook_field_widget_info_alter()
  734. * @see hook_field_widget_form()
  735. * @see hook_field_widget_form_alter()
  736. * @see hook_field_widget_WIDGET_TYPE_form_alter()
  737. * @see hook_field_widget_error()
  738. * @see hook_field_widget_settings_form()
  739. */
  740. function hook_field_widget_info() {
  741. return array(
  742. 'text_textfield' => array(
  743. 'label' => t('Text field'),
  744. 'field types' => array('text'),
  745. 'settings' => array('size' => 60),
  746. 'behaviors' => array(
  747. 'multiple values' => FIELD_BEHAVIOR_DEFAULT,
  748. 'default value' => FIELD_BEHAVIOR_DEFAULT,
  749. ),
  750. ),
  751. 'text_textarea' => array(
  752. 'label' => t('Text area (multiple rows)'),
  753. 'field types' => array('text_long'),
  754. 'settings' => array('rows' => 5),
  755. 'behaviors' => array(
  756. 'multiple values' => FIELD_BEHAVIOR_DEFAULT,
  757. 'default value' => FIELD_BEHAVIOR_DEFAULT,
  758. ),
  759. ),
  760. 'text_textarea_with_summary' => array(
  761. 'label' => t('Text area with a summary'),
  762. 'field types' => array('text_with_summary'),
  763. 'settings' => array('rows' => 20, 'summary_rows' => 5),
  764. 'behaviors' => array(
  765. 'multiple values' => FIELD_BEHAVIOR_DEFAULT,
  766. 'default value' => FIELD_BEHAVIOR_DEFAULT,
  767. ),
  768. // As an advanced widget, force it to sink to the bottom of the choices.
  769. 'weight' => 2,
  770. ),
  771. );
  772. }
  773. /**
  774. * Perform alterations on Field API widget types.
  775. *
  776. * @param $info
  777. * Array of informations on widget types exposed by hook_field_widget_info()
  778. * implementations.
  779. */
  780. function hook_field_widget_info_alter(&$info) {
  781. // Add a setting to a widget type.
  782. $info['text_textfield']['settings'] += array(
  783. 'mymodule_additional_setting' => 'default value',
  784. );
  785. // Let a new field type re-use an existing widget.
  786. $info['options_select']['field types'][] = 'my_field_type';
  787. }
  788. /**
  789. * Return the form for a single field widget.
  790. *
  791. * Field widget form elements should be based on the passed-in $element, which
  792. * contains the base form element properties derived from the field
  793. * configuration.
  794. *
  795. * Field API will set the weight, field name and delta values for each form
  796. * element. If there are multiple values for this field, the Field API will
  797. * invoke this hook as many times as needed.
  798. *
  799. * Note that, depending on the context in which the widget is being included
  800. * (regular entity form, field configuration form, advanced search form...),
  801. * the values for $field and $instance might be different from the "official"
  802. * definitions returned by field_info_field() and field_info_instance().
  803. * Examples: mono-value widget even if the field is multi-valued, non-required
  804. * widget even if the field is 'required'...
  805. *
  806. * Therefore, the FAPI element callbacks (such as #process, #element_validate,
  807. * #value_callback...) used by the widget cannot use the field_info_field()
  808. * or field_info_instance() functions to retrieve the $field or $instance
  809. * definitions they should operate on. The field_widget_field() and
  810. * field_widget_instance() functions should be used instead to fetch the
  811. * current working definitions from $form_state, where Field API stores them.
  812. *
  813. * Alternatively, hook_field_widget_form() can extract the needed specific
  814. * properties from $field and $instance and set them as ad-hoc
  815. * $element['#custom'] properties, for later use by its element callbacks.
  816. *
  817. * Other modules may alter the form element provided by this function using
  818. * hook_field_widget_form_alter().
  819. *
  820. * @param $form
  821. * The form structure where widgets are being attached to. This might be a
  822. * full form structure, or a sub-element of a larger form.
  823. * @param $form_state
  824. * An associative array containing the current state of the form.
  825. * @param $field
  826. * The field structure.
  827. * @param $instance
  828. * The field instance.
  829. * @param $langcode
  830. * The language associated with $items.
  831. * @param $items
  832. * Array of default values for this field.
  833. * @param $delta
  834. * The order of this item in the array of subelements (0, 1, 2, etc).
  835. * @param $element
  836. * A form element array containing basic properties for the widget:
  837. * - #entity_type: The name of the entity the field is attached to.
  838. * - #bundle: The name of the field bundle the field is contained in.
  839. * - #field_name: The name of the field.
  840. * - #language: The language the field is being edited in.
  841. * - #field_parents: The 'parents' space for the field in the form. Most
  842. * widgets can simply overlook this property. This identifies the
  843. * location where the field values are placed within
  844. * $form_state['values'], and is used to access processing information
  845. * for the field through the field_form_get_state() and
  846. * field_form_set_state() functions.
  847. * - #columns: A list of field storage columns of the field.
  848. * - #title: The sanitized element label for the field instance, ready for
  849. * output.
  850. * - #description: The sanitized element description for the field instance,
  851. * ready for output.
  852. * - #required: A Boolean indicating whether the element value is required;
  853. * for required multiple value fields, only the first widget's values are
  854. * required.
  855. * - #delta: The order of this item in the array of subelements; see $delta
  856. * above.
  857. *
  858. * @return
  859. * The form elements for a single widget for this field.
  860. *
  861. * @see field_widget_field()
  862. * @see field_widget_instance()
  863. * @see hook_field_widget_form_alter()
  864. * @see hook_field_widget_WIDGET_TYPE_form_alter()
  865. */
  866. function hook_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  867. $element += array(
  868. '#type' => $instance['widget']['type'],
  869. '#default_value' => isset($items[$delta]) ? $items[$delta] : '',
  870. );
  871. return array('value' => $element);
  872. }
  873. /**
  874. * Alter forms for field widgets provided by other modules.
  875. *
  876. * @param $element
  877. * The field widget form element as constructed by hook_field_widget_form().
  878. * @param $form_state
  879. * An associative array containing the current state of the form.
  880. * @param $context
  881. * An associative array containing the following key-value pairs, matching the
  882. * arguments received by hook_field_widget_form():
  883. * - form: The form structure to which widgets are being attached. This may be
  884. * a full form structure, or a sub-element of a larger form.
  885. * - field: The field structure.
  886. * - instance: The field instance structure.
  887. * - langcode: The language associated with $items.
  888. * - items: Array of default values for this field.
  889. * - delta: The order of this item in the array of subelements (0, 1, 2, etc).
  890. *
  891. * @see hook_field_widget_form()
  892. * @see hook_field_widget_WIDGET_TYPE_form_alter()
  893. */
  894. function hook_field_widget_form_alter(&$element, &$form_state, $context) {
  895. // Add a css class to widget form elements for all fields of type mytype.
  896. if ($context['field']['type'] == 'mytype') {
  897. // Be sure not to overwrite existing attributes.
  898. $element['#attributes']['class'][] = 'myclass';
  899. }
  900. }
  901. /**
  902. * Alter widget forms for a specific widget provided by another module.
  903. *
  904. * Modules can implement hook_field_widget_WIDGET_TYPE_form_alter() to modify a
  905. * specific widget form, rather than using hook_field_widget_form_alter() and
  906. * checking the widget type.
  907. *
  908. * @param $element
  909. * The field widget form element as constructed by hook_field_widget_form().
  910. * @param $form_state
  911. * An associative array containing the current state of the form.
  912. * @param $context
  913. * An associative array containing the following key-value pairs, matching the
  914. * arguments received by hook_field_widget_form():
  915. * - "form": The form structure where widgets are being attached to. This
  916. * might be a full form structure, or a sub-element of a larger form.
  917. * - "field": The field structure.
  918. * - "instance": The field instance structure.
  919. * - "langcode": The language associated with $items.
  920. * - "items": Array of default values for this field.
  921. * - "delta": The order of this item in the array of subelements (0, 1, 2,
  922. * etc).
  923. *
  924. * @see hook_field_widget_form()
  925. * @see hook_field_widget_form_alter()
  926. */
  927. function hook_field_widget_WIDGET_TYPE_form_alter(&$element, &$form_state, $context) {
  928. // Code here will only act on widgets of type WIDGET_TYPE. For example,
  929. // hook_field_widget_mymodule_autocomplete_form_alter() will only act on
  930. // widgets of type 'mymodule_autocomplete'.
  931. $element['#autocomplete_path'] = 'mymodule/autocomplete_path';
  932. }
  933. /**
  934. * Alters the widget properties of a field instance before it gets displayed.
  935. *
  936. * Note that instead of hook_field_widget_properties_alter(), which is called
  937. * for all fields on all entity types,
  938. * hook_field_widget_properties_ENTITY_TYPE_alter() may be used to alter widget
  939. * properties for fields on a specific entity type only.
  940. *
  941. * This hook is called once per field per added or edit entity. If the result
  942. * of the hook involves reading from the database, it is highly recommended to
  943. * statically cache the information.
  944. *
  945. * @param $widget
  946. * The instance's widget properties.
  947. * @param $context
  948. * An associative array containing:
  949. * - entity_type: The entity type; e.g., 'node' or 'user'.
  950. * - entity: The entity object.
  951. * - field: The field that the widget belongs to.
  952. * - instance: The instance of the field.
  953. *
  954. * @see hook_field_widget_properties_ENTITY_TYPE_alter()
  955. */
  956. function hook_field_widget_properties_alter(&$widget, $context) {
  957. // Change a widget's type according to the time of day.
  958. $field = $context['field'];
  959. if ($context['entity_type'] == 'node' && $field['field_name'] == 'field_foo') {
  960. $time = date('H');
  961. $widget['type'] = $time < 12 ? 'widget_am' : 'widget_pm';
  962. }
  963. }
  964. /**
  965. * Flag a field-level validation error.
  966. *
  967. * @param $element
  968. * An array containing the form element for the widget. The error needs to be
  969. * flagged on the right sub-element, according to the widget's internal
  970. * structure.
  971. * @param $error
  972. * An associative array with the following key-value pairs, as returned by
  973. * hook_field_validate():
  974. * - error: the error code. Complex widgets might need to report different
  975. * errors to different form elements inside the widget.
  976. * - message: the human readable message to be displayed.
  977. * @param $form
  978. * The form structure where field elements are attached to. This might be a
  979. * full form structure, or a sub-element of a larger form.
  980. * @param $form_state
  981. * An associative array containing the current state of the form.
  982. */
  983. function hook_field_widget_error($element, $error, $form, &$form_state) {
  984. form_error($element, $error['message']);
  985. }
  986. /**
  987. * @} End of "defgroup field_widget".
  988. */
  989. /**
  990. * @defgroup field_formatter Field Formatter API
  991. * @{
  992. * Define Field API formatter types.
  993. *
  994. * Field API formatters specify how fields are displayed when the entity to
  995. * which the field is attached is displayed. Fields of a given
  996. * @link field_types field type @endlink may be displayed using more than one
  997. * formatter. In this case, the Field UI module allows the site builder to
  998. * choose which formatter to use. Field formatters are defined by implementing
  999. * hook_field_formatter_info().
  1000. *
  1001. * @see field
  1002. * @see field_types
  1003. * @see field_widget
  1004. */
  1005. /**
  1006. * Expose Field API formatter types.
  1007. *
  1008. * Formatters handle the display of field values. Formatter hooks are typically
  1009. * called by the Field Attach API field_attach_prepare_view() and
  1010. * field_attach_view() functions.
  1011. *
  1012. * @return
  1013. * An array describing the formatter types implemented by the module.
  1014. * The keys are formatter type names. To avoid name clashes, formatter type
  1015. * names should be prefixed with the name of the module that exposes them.
  1016. * The values are arrays describing the formatter type, with the following
  1017. * key/value pairs:
  1018. * - label: The human-readable name of the formatter type.
  1019. * - description: A short description for the formatter type.
  1020. * - field types: An array of field types the formatter supports.
  1021. * - settings: An array whose keys are the names of the settings available
  1022. * for the formatter type, and whose values are the default values for
  1023. * those settings.
  1024. *
  1025. * @see hook_field_formatter_info_alter()
  1026. * @see hook_field_formatter_view()
  1027. * @see hook_field_formatter_prepare_view()
  1028. */
  1029. function hook_field_formatter_info() {
  1030. return array(
  1031. 'text_default' => array(
  1032. 'label' => t('Default'),
  1033. 'field types' => array('text', 'text_long', 'text_with_summary'),
  1034. ),
  1035. 'text_plain' => array(
  1036. 'label' => t('Plain text'),
  1037. 'field types' => array('text', 'text_long', 'text_with_summary'),
  1038. ),
  1039. // The text_trimmed formatter displays the trimmed version of the
  1040. // full element of the field. It is intended to be used with text
  1041. // and text_long fields. It also works with text_with_summary
  1042. // fields though the text_summary_or_trimmed formatter makes more
  1043. // sense for that field type.
  1044. 'text_trimmed' => array(
  1045. 'label' => t('Trimmed'),
  1046. 'field types' => array('text', 'text_long', 'text_with_summary'),
  1047. ),
  1048. // The 'summary or trimmed' field formatter for text_with_summary
  1049. // fields displays returns the summary element of the field or, if
  1050. // the summary is empty, the trimmed version of the full element
  1051. // of the field.
  1052. 'text_summary_or_trimmed' => array(
  1053. 'label' => t('Summary or trimmed'),
  1054. 'field types' => array('text_with_summary'),
  1055. ),
  1056. );
  1057. }
  1058. /**
  1059. * Perform alterations on Field API formatter types.
  1060. *
  1061. * @param $info
  1062. * An array of information on formatter types exposed by
  1063. * hook_field_formatter_info() implementations.
  1064. */
  1065. function hook_field_formatter_info_alter(&$info) {
  1066. // Add a setting to a formatter type.
  1067. $info['text_default']['settings'] += array(
  1068. 'mymodule_additional_setting' => 'default value',
  1069. );
  1070. // Let a new field type re-use an existing formatter.
  1071. $info['text_default']['field types'][] = 'my_field_type';
  1072. }
  1073. /**
  1074. * Allow formatters to load information for field values being displayed.
  1075. *
  1076. * This should be used when a formatter needs to load additional information
  1077. * from the database in order to render a field, for example a reference field
  1078. * which displays properties of the referenced entities such as name or type.
  1079. *
  1080. * This hook is called after the field type's own hook_field_prepare_view().
  1081. *
  1082. * Unlike most other field hooks, this hook operates on multiple entities. The
  1083. * $entities, $instances and $items parameters are arrays keyed by entity ID.
  1084. * For performance reasons, information for all available entities should be
  1085. * loaded in a single query where possible.
  1086. *
  1087. * @param $entity_type
  1088. * The type of $entity.
  1089. * @param $entities
  1090. * Array of entities being displayed, keyed by entity ID.
  1091. * @param $field
  1092. * The field structure for the operation.
  1093. * @param $instances
  1094. * Array of instance structures for $field for each entity, keyed by entity
  1095. * ID.
  1096. * @param $langcode
  1097. * The language the field values are to be shown in. If no language is
  1098. * provided the current language is used.
  1099. * @param $items
  1100. * Array of field values for the entities, keyed by entity ID.
  1101. * @param $displays
  1102. * Array of display settings to use for each entity, keyed by entity ID.
  1103. *
  1104. * @return
  1105. * Changes or additions to field values are done by altering the $items
  1106. * parameter by reference.
  1107. */
  1108. function hook_field_formatter_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items, $displays) {
  1109. $tids = array();
  1110. // Collect every possible term attached to any of the fieldable entities.
  1111. foreach ($entities as $id => $entity) {
  1112. foreach ($items[$id] as $delta => $item) {
  1113. // Force the array key to prevent duplicates.
  1114. $tids[$item['tid']] = $item['tid'];
  1115. }
  1116. }
  1117. if ($tids) {
  1118. $terms = taxonomy_term_load_multiple($tids);
  1119. // Iterate through the fieldable entities again to attach the loaded term
  1120. // data.
  1121. foreach ($entities as $id => $entity) {
  1122. $rekey = FALSE;
  1123. foreach ($items[$id] as $delta => $item) {
  1124. // Check whether the taxonomy term field instance value could be loaded.
  1125. if (isset($terms[$item['tid']])) {
  1126. // Replace the instance value with the term data.
  1127. $items[$id][$delta]['taxonomy_term'] = $terms[$item['tid']];
  1128. }
  1129. // Otherwise, unset the instance value, since the term does not exist.
  1130. else {
  1131. unset($items[$id][$delta]);
  1132. $rekey = TRUE;
  1133. }
  1134. }
  1135. if ($rekey) {
  1136. // Rekey the items array.
  1137. $items[$id] = array_values($items[$id]);
  1138. }
  1139. }
  1140. }
  1141. }
  1142. /**
  1143. * Build a renderable array for a field value.
  1144. *
  1145. * @param $entity_type
  1146. * The type of $entity.
  1147. * @param $entity
  1148. * The entity being displayed.
  1149. * @param $field
  1150. * The field structure.
  1151. * @param $instance
  1152. * The field instance.
  1153. * @param $langcode
  1154. * The language associated with $items.
  1155. * @param $items
  1156. * Array of values for this field.
  1157. * @param $display
  1158. * The display settings to use, as found in the 'display' entry of instance
  1159. * definitions. The array notably contains the following keys and values;
  1160. * - type: The name of the formatter to use.
  1161. * - settings: The array of formatter settings.
  1162. *
  1163. * @return
  1164. * A renderable array for the $items, as an array of child elements keyed
  1165. * by numeric indexes starting from 0.
  1166. */
  1167. function hook_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  1168. $element = array();
  1169. $settings = $display['settings'];
  1170. switch ($display['type']) {
  1171. case 'sample_field_formatter_simple':
  1172. // Common case: each value is displayed individually in a sub-element
  1173. // keyed by delta. The field.tpl.php template specifies the markup
  1174. // wrapping each value.
  1175. foreach ($items as $delta => $item) {
  1176. $element[$delta] = array('#markup' => $settings['some_setting'] . $item['value']);
  1177. }
  1178. break;
  1179. case 'sample_field_formatter_themeable':
  1180. // More elaborate formatters can defer to a theme function for easier
  1181. // customization.
  1182. foreach ($items as $delta => $item) {
  1183. $element[$delta] = array(
  1184. '#theme' => 'mymodule_theme_sample_field_formatter_themeable',
  1185. '#data' => $item['value'],
  1186. '#some_setting' => $settings['some_setting'],
  1187. );
  1188. }
  1189. break;
  1190. case 'sample_field_formatter_combined':
  1191. // Some formatters might need to display all values within a single piece
  1192. // of markup.
  1193. $rows = array();
  1194. foreach ($items as $delta => $item) {
  1195. $rows[] = array($delta, $item['value']);
  1196. }
  1197. $element[0] = array(
  1198. '#theme' => 'table',
  1199. '#header' => array(t('Delta'), t('Value')),
  1200. '#rows' => $rows,
  1201. );
  1202. break;
  1203. }
  1204. return $element;
  1205. }
  1206. /**
  1207. * @} End of "defgroup field_formatter".
  1208. */
  1209. /**
  1210. * @addtogroup field_attach
  1211. * @{
  1212. */
  1213. /**
  1214. * Act on field_attach_form().
  1215. *
  1216. * This hook is invoked after the field module has performed the operation.
  1217. * Implementing modules should alter the $form or $form_state parameters.
  1218. *
  1219. * @param $entity_type
  1220. * The type of $entity; for example, 'node' or 'user'.
  1221. * @param $entity
  1222. * The entity for which an edit form is being built.
  1223. * @param $form
  1224. * The form structure where field elements are attached to. This might be a
  1225. * full form structure, or a sub-element of a larger form. The
  1226. * $form['#parents'] property can be used to identify the corresponding part
  1227. * of $form_state['values']. Hook implementations that need to act on the
  1228. * top-level properties of the global form (like #submit, #validate...) can
  1229. * add a #process callback to the array received in the $form parameter, and
  1230. * act on the $complete_form parameter in the process callback.
  1231. * @param $form_state
  1232. * An associative array containing the current state of the form.
  1233. * @param $langcode
  1234. * The language the field values are going to be entered in. If no language
  1235. * is provided the default site language will be used.
  1236. */
  1237. function hook_field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode) {
  1238. // Add a checkbox allowing a given field to be emptied.
  1239. // See hook_field_attach_submit() for the corresponding processing code.
  1240. $form['empty_field_foo'] = array(
  1241. '#type' => 'checkbox',
  1242. '#title' => t("Empty the 'field_foo' field"),
  1243. );
  1244. }
  1245. /**
  1246. * Act on field_attach_load().
  1247. *
  1248. * This hook is invoked after the field module has performed the operation.
  1249. *
  1250. * Unlike other field_attach hooks, this hook accounts for 'multiple loads'.
  1251. * Instead of the usual $entity parameter, it accepts an array of entities,
  1252. * indexed by entity ID. For performance reasons, information for all available
  1253. * entities should be loaded in a single query where possible.
  1254. *
  1255. * The changes made to the entities' field values get cached by the field cache
  1256. * for subsequent loads.
  1257. *
  1258. * See field_attach_load() for details and arguments.
  1259. */
  1260. function hook_field_attach_load($entity_type, $entities, $age, $options) {
  1261. // @todo Needs function body.
  1262. }
  1263. /**
  1264. * Act on field_attach_validate().
  1265. *
  1266. * This hook is invoked after the field module has performed the operation.
  1267. *
  1268. * See field_attach_validate() for details and arguments.
  1269. *
  1270. * @param $entity_type
  1271. * The type of $entity; e.g., 'node' or 'user'.
  1272. * @param $entity
  1273. * The entity with fields to validate.
  1274. * @param array $errors
  1275. * An associative array of errors keyed by field_name, language, delta.
  1276. */
  1277. function hook_field_attach_validate($entity_type, $entity, &$errors) {
  1278. // @todo Needs function body.
  1279. }
  1280. /**
  1281. * Act on field_attach_submit().
  1282. *
  1283. * This hook is invoked after the field module has performed the operation.
  1284. *
  1285. * @param $entity_type
  1286. * The type of $entity; for example, 'node' or 'user'.
  1287. * @param $entity
  1288. * The entity for which an edit form is being submitted. The incoming form
  1289. * values have been extracted as field values of the $entity object.
  1290. * @param $form
  1291. * The form structure where field elements are attached to. This might be a
  1292. * full form structure, or a sub-part of a larger form. The $form['#parents']
  1293. * property can be used to identify the corresponding part of
  1294. * $form_state['values'].
  1295. * @param $form_state
  1296. * An associative array containing the current state of the form.
  1297. */
  1298. function hook_field_attach_submit($entity_type, $entity, $form, &$form_state) {
  1299. // Sample case of an 'Empty the field' checkbox added on the form, allowing
  1300. // a given field to be emptied.
  1301. $values = drupal_array_get_nested_value($form_state['values'], $form['#parents']);
  1302. if (!empty($values['empty_field_foo'])) {
  1303. unset($entity->field_foo);
  1304. }
  1305. }
  1306. /**
  1307. * Act on field_attach_presave().
  1308. *
  1309. * This hook is invoked after the field module has performed the operation.
  1310. *
  1311. * See field_attach_presave() for details and arguments.
  1312. */
  1313. function hook_field_attach_presave($entity_type, $entity) {
  1314. // @todo Needs function body.
  1315. }
  1316. /**
  1317. * Act on field_attach_insert().
  1318. *
  1319. * This hook is invoked after the field module has performed the operation.
  1320. *
  1321. * See field_attach_insert() for details and arguments.
  1322. */
  1323. function hook_field_attach_insert($entity_type, $entity) {
  1324. // @todo Needs function body.
  1325. }
  1326. /**
  1327. * Act on field_attach_update().
  1328. *
  1329. * This hook is invoked after the field module has performed the operation.
  1330. *
  1331. * See field_attach_update() for details and arguments.
  1332. */
  1333. function hook_field_attach_update($entity_type, $entity) {
  1334. // @todo Needs function body.
  1335. }
  1336. /**
  1337. * Alter field_attach_preprocess() variables.
  1338. *
  1339. * This hook is invoked while preprocessing the field.tpl.php template file
  1340. * in field_attach_preprocess().
  1341. *
  1342. * @param $variables
  1343. * The variables array is passed by reference and will be populated with field
  1344. * values.
  1345. * @param $context
  1346. * An associative array containing:
  1347. * - entity_type: The type of $entity; for example, 'node' or 'user'.
  1348. * - entity: The entity with fields to render.
  1349. * - element: The structured array containing the values ready for rendering.
  1350. */
  1351. function hook_field_attach_preprocess_alter(&$variables, $context) {
  1352. // @todo Needs function body.
  1353. }
  1354. /**
  1355. * Act on field_attach_delete().
  1356. *
  1357. * This hook is invoked after the field module has performed the operation.
  1358. *
  1359. * See field_attach_delete() for details and arguments.
  1360. */
  1361. function hook_field_attach_delete($entity_type, $entity) {
  1362. // @todo Needs function body.
  1363. }
  1364. /**
  1365. * Act on field_attach_delete_revision().
  1366. *
  1367. * This hook is invoked after the field module has performed the operation.
  1368. *
  1369. * See field_attach_delete_revision() for details and arguments.
  1370. */
  1371. function hook_field_attach_delete_revision($entity_type, $entity) {
  1372. // @todo Needs function body.
  1373. }
  1374. /**
  1375. * Act on field_purge_data().
  1376. *
  1377. * This hook is invoked in field_purge_data() and allows modules to act on
  1378. * purging data from a single field pseudo-entity. For example, if a module
  1379. * relates data in the field with its own data, it may purge its own data
  1380. * during this process as well.
  1381. *
  1382. * @param $entity_type
  1383. * The type of $entity; for example, 'node' or 'user'.
  1384. * @param $entity
  1385. * The pseudo-entity whose field data is being purged.
  1386. * @param $field
  1387. * The (possibly deleted) field whose data is being purged.
  1388. * @param $instance
  1389. * The deleted field instance whose data is being purged.
  1390. *
  1391. * @see @link field_purge Field API bulk data deletion @endlink
  1392. * @see field_purge_data()
  1393. */
  1394. function hook_field_attach_purge($entity_type, $entity, $field, $instance) {
  1395. // find the corresponding data in mymodule and purge it
  1396. if ($entity_type == 'node' && $field->field_name == 'my_field_name') {
  1397. mymodule_remove_mydata($entity->nid);
  1398. }
  1399. }
  1400. /**
  1401. * Perform alterations on field_attach_view() or field_view_field().
  1402. *
  1403. * This hook is invoked after the field module has performed the operation.
  1404. *
  1405. * @param $output
  1406. * The structured content array tree for all of the entity's fields.
  1407. * @param $context
  1408. * An associative array containing:
  1409. * - entity_type: The type of $entity; for example, 'node' or 'user'.
  1410. * - entity: The entity with fields to render.
  1411. * - view_mode: View mode; for example, 'full' or 'teaser'.
  1412. * - display: Either a view mode string or an array of display settings. If
  1413. * this hook is being invoked from field_attach_view(), the 'display'
  1414. * element is set to the view mode string. If this hook is being invoked
  1415. * from field_view_field(), this element is set to the $display argument
  1416. * and the view_mode element is set to '_custom'. See field_view_field()
  1417. * for more information on what its $display argument contains.
  1418. * - language: The language code used for rendering.
  1419. */
  1420. function hook_field_attach_view_alter(&$output, $context) {
  1421. // Append RDF term mappings on displayed taxonomy links.
  1422. foreach (element_children($output) as $field_name) {
  1423. $element = &$output[$field_name];
  1424. if ($element['#field_type'] == 'taxonomy_term_reference' && $element['#formatter'] == 'taxonomy_term_reference_link') {
  1425. foreach ($element['#items'] as $delta => $item) {
  1426. $term = $item['taxonomy_term'];
  1427. if (!empty($term->rdf_mapping['rdftype'])) {
  1428. $element[$delta]['#options']['attributes']['typeof'] = $term->rdf_mapping['rdftype'];
  1429. }
  1430. if (!empty($term->rdf_mapping['name']['predicates'])) {
  1431. $element[$delta]['#options']['attributes']['property'] = $term->rdf_mapping['name']['predicates'];
  1432. }
  1433. }
  1434. }
  1435. }
  1436. }
  1437. /**
  1438. * Perform alterations on field_attach_prepare_translation().
  1439. *
  1440. * This hook is invoked after the field module has performed the operation.
  1441. *
  1442. * @param $entity
  1443. * The entity being prepared for translation.
  1444. * @param $context
  1445. * An associative array containing:
  1446. * - entity_type: The type of $entity; e.g. 'node' or 'user'.
  1447. * - langcode: The language the entity has to be translated in.
  1448. * - source_entity: The entity holding the field values to be translated.
  1449. * - source_langcode: The source language from which translate.
  1450. */
  1451. function hook_field_attach_prepare_translation_alter(&$entity, $context) {
  1452. if ($context['entity_type'] == 'custom_entity_type') {
  1453. $entity->custom_field = $context['source_entity']->custom_field;
  1454. }
  1455. }
  1456. /**
  1457. * Perform alterations on field_language() values.
  1458. *
  1459. * This hook is invoked to alter the array of display languages for the given
  1460. * entity.
  1461. *
  1462. * @param $display_language
  1463. * A reference to an array of language codes keyed by field name.
  1464. * @param $context
  1465. * An associative array containing:
  1466. * - entity_type: The type of the entity to be displayed.
  1467. * - entity: The entity with fields to render.
  1468. * - langcode: The language code $entity has to be displayed in.
  1469. *
  1470. * @ingroup field_language
  1471. */
  1472. function hook_field_language_alter(&$display_language, $context) {
  1473. // Do not apply core language fallback rules if they are disabled or if Locale
  1474. // is not registered as a translation handler.
  1475. if (variable_get('locale_field_language_fallback', TRUE) && field_has_translation_handler($context['entity_type'], 'locale')) {
  1476. locale_field_language_fallback($display_language, $context['entity'], $context['language']);
  1477. }
  1478. }
  1479. /**
  1480. * Alter field_available_languages() values.
  1481. *
  1482. * This hook is invoked from field_available_languages() to allow modules to
  1483. * alter the array of available languages for the given field.
  1484. *
  1485. * @param $languages
  1486. * A reference to an array of language codes to be made available.
  1487. * @param $context
  1488. * An associative array containing:
  1489. * - entity_type: The type of the entity the field is attached to.
  1490. * - field: A field data structure.
  1491. *
  1492. * @ingroup field_language
  1493. */
  1494. function hook_field_available_languages_alter(&$languages, $context) {
  1495. // Add an unavailable language.
  1496. $languages[] = 'xx';
  1497. // Remove an available language.
  1498. $index = array_search('yy', $languages);
  1499. unset($languages[$index]);
  1500. }
  1501. /**
  1502. * Act on field_attach_create_bundle().
  1503. *
  1504. * This hook is invoked after the field module has performed the operation.
  1505. *
  1506. * See field_attach_create_bundle() for details and arguments.
  1507. */
  1508. function hook_field_attach_create_bundle($entity_type, $bundle) {
  1509. // When a new bundle is created, the menu needs to be rebuilt to add the
  1510. // Field UI menu item tabs.
  1511. variable_set('menu_rebuild_needed', TRUE);
  1512. }
  1513. /**
  1514. * Act on field_attach_rename_bundle().
  1515. *
  1516. * This hook is invoked after the field module has performed the operation.
  1517. *
  1518. * See field_attach_rename_bundle() for details and arguments.
  1519. */
  1520. function hook_field_attach_rename_bundle($entity_type, $bundle_old, $bundle_new) {
  1521. // Update the extra weights variable with new information.
  1522. if ($bundle_old !== $bundle_new) {
  1523. $extra_weights = variable_get('field_extra_weights', array());
  1524. if (isset($info[$entity_type][$bundle_old])) {
  1525. $extra_weights[$entity_type][$bundle_new] = $extra_weights[$entity_type][$bundle_old];
  1526. unset($extra_weights[$entity_type][$bundle_old]);
  1527. variable_set('field_extra_weights', $extra_weights);
  1528. }
  1529. }
  1530. }
  1531. /**
  1532. * Act on field_attach_delete_bundle.
  1533. *
  1534. * This hook is invoked after the field module has performed the operation.
  1535. *
  1536. * @param $entity_type
  1537. * The type of entity; for example, 'node' or 'user'.
  1538. * @param $bundle
  1539. * The name of the bundle that was just deleted.
  1540. * @param $instances
  1541. * An array of all instances that existed for the bundle before it was
  1542. * deleted.
  1543. */
  1544. function hook_field_attach_delete_bundle($entity_type, $bundle, $instances) {
  1545. // Remove the extra weights variable information for this bundle.
  1546. $extra_weights = variable_get('field_extra_weights', array());
  1547. if (isset($extra_weights[$entity_type][$bundle])) {
  1548. unset($extra_weights[$entity_type][$bundle]);
  1549. variable_set('field_extra_weights', $extra_weights);
  1550. }
  1551. }
  1552. /**
  1553. * @} End of "addtogroup field_attach".
  1554. */
  1555. /**
  1556. * @addtogroup field_storage
  1557. * @{
  1558. */
  1559. /**
  1560. * Expose Field API storage backends.
  1561. *
  1562. * @return
  1563. * An array describing the storage backends implemented by the module.
  1564. * The keys are storage backend names. To avoid name clashes, storage backend
  1565. * names should be prefixed with the name of the module that exposes them.
  1566. * The values are arrays describing the storage backend, with the following
  1567. * key/value pairs:
  1568. * - label: The human-readable name of the storage backend.
  1569. * - description: A short description for the storage backend.
  1570. * - settings: An array whose keys are the names of the settings available
  1571. * for the storage backend, and whose values are the default values for
  1572. * those settings.
  1573. */
  1574. function hook_field_storage_info() {
  1575. return array(
  1576. 'field_sql_storage' => array(
  1577. 'label' => t('Default SQL storage'),
  1578. 'description' => t('Stores fields in the local SQL database, using per-field tables.'),
  1579. 'settings' => array(),
  1580. ),
  1581. );
  1582. }
  1583. /**
  1584. * Perform alterations on Field API storage types.
  1585. *
  1586. * @param $info
  1587. * Array of informations on storage types exposed by
  1588. * hook_field_field_storage_info() implementations.
  1589. */
  1590. function hook_field_storage_info_alter(&$info) {
  1591. // Add a setting to a storage type.
  1592. $info['field_sql_storage']['settings'] += array(
  1593. 'mymodule_additional_setting' => 'default value',
  1594. );
  1595. }
  1596. /**
  1597. * Reveal the internal details about the storage for a field.
  1598. *
  1599. * For example, an SQL storage module might return the Schema API structure for
  1600. * the table. A key/value storage module might return the server name,
  1601. * authentication credentials, and bin name.
  1602. *
  1603. * Field storage modules are not obligated to implement this hook. Modules
  1604. * that rely on these details must only use them for read operations.
  1605. *
  1606. * @param $field
  1607. * A field structure.
  1608. *
  1609. * @return
  1610. * An array of details.
  1611. * - The first dimension is a store type (sql, solr, etc).
  1612. * - The second dimension indicates the age of the values in the store
  1613. * FIELD_LOAD_CURRENT or FIELD_LOAD_REVISION.
  1614. * - Other dimensions are specific to the field storage module.
  1615. *
  1616. * @see hook_field_storage_details_alter()
  1617. */
  1618. function hook_field_storage_details($field) {
  1619. $details = array();
  1620. // Add field columns.
  1621. foreach ((array) $field['columns'] as $column_name => $attributes) {
  1622. $real_name = _field_sql_storage_columnname($field['field_name'], $column_name);
  1623. $columns[$column_name] = $real_name;
  1624. }
  1625. return array(
  1626. 'sql' => array(
  1627. FIELD_LOAD_CURRENT => array(
  1628. _field_sql_storage_tablename($field) => $columns,
  1629. ),
  1630. FIELD_LOAD_REVISION => array(
  1631. _field_sql_storage_revision_tablename($field) => $columns,
  1632. ),
  1633. ),
  1634. );
  1635. }
  1636. /**
  1637. * Perform alterations on Field API storage details.
  1638. *
  1639. * @param $details
  1640. * An array of storage details for fields as exposed by
  1641. * hook_field_storage_details() implementations.
  1642. * @param $field
  1643. * A field structure.
  1644. *
  1645. * @see hook_field_storage_details()
  1646. */
  1647. function hook_field_storage_details_alter(&$details, $field) {
  1648. if ($field['field_name'] == 'field_of_interest') {
  1649. $columns = array();
  1650. foreach ((array) $field['columns'] as $column_name => $attributes) {
  1651. $columns[$column_name] = $column_name;
  1652. }
  1653. $details['drupal_variables'] = array(
  1654. FIELD_LOAD_CURRENT => array(
  1655. 'moon' => $columns,
  1656. ),
  1657. FIELD_LOAD_REVISION => array(
  1658. 'mars' => $columns,
  1659. ),
  1660. );
  1661. }
  1662. }
  1663. /**
  1664. * Load field data for a set of entities.
  1665. *
  1666. * This hook is invoked from field_attach_load() to ask the field storage
  1667. * module to load field data.
  1668. *
  1669. * Modules implementing this hook should load field values and add them to
  1670. * objects in $entities. Fields with no values should be added as empty
  1671. * arrays.
  1672. *
  1673. * @param $entity_type
  1674. * The type of entity, such as 'node' or 'user'.
  1675. * @param $entities
  1676. * The array of entity objects to add fields to, keyed by entity ID.
  1677. * @param $age
  1678. * FIELD_LOAD_CURRENT to load the most recent revision for all fields, or
  1679. * FIELD_LOAD_REVISION to load the version indicated by each entity.
  1680. * @param $fields
  1681. * An array listing the fields to be loaded. The keys of the array are field
  1682. * IDs, and the values of the array are the entity IDs (or revision IDs,
  1683. * depending on the $age parameter) to add each field to.
  1684. * @param $options
  1685. * An associative array of additional options, with the following keys:
  1686. * - deleted: If TRUE, deleted fields should be loaded as well as
  1687. * non-deleted fields. If unset or FALSE, only non-deleted fields should be
  1688. * loaded.
  1689. */
  1690. function hook_field_storage_load($entity_type, $entities, $age, $fields, $options) {
  1691. $load_current = $age == FIELD_LOAD_CURRENT;
  1692. foreach ($fields as $field_id => $ids) {
  1693. // By the time this hook runs, the relevant field definitions have been
  1694. // populated and cached in FieldInfo, so calling field_info_field_by_id()
  1695. // on each field individually is more efficient than loading all fields in
  1696. // memory upfront with field_info_field_by_ids().
  1697. $field = field_info_field_by_id($field_id);
  1698. $field_name = $field['field_name'];
  1699. $table = $load_current ? _field_sql_storage_tablename($field) : _field_sql_storage_revision_tablename($field);
  1700. $query = db_select($table, 't')
  1701. ->fields('t')
  1702. ->condition('entity_type', $entity_type)
  1703. ->condition($load_current ? 'entity_id' : 'revision_id', $ids, 'IN')
  1704. ->condition('language', field_available_languages($entity_type, $field), 'IN')
  1705. ->orderBy('delta');
  1706. if (empty($options['deleted'])) {
  1707. $query->condition('deleted', 0);
  1708. }
  1709. $results = $query->execute();
  1710. $delta_count = array();
  1711. foreach ($results as $row) {
  1712. if (!isset($delta_count[$row->entity_id][$row->language])) {
  1713. $delta_count[$row->entity_id][$row->language] = 0;
  1714. }
  1715. if ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED || $delta_count[$row->entity_id][$row->language] < $field['cardinality']) {
  1716. $item = array();
  1717. // For each column declared by the field, populate the item
  1718. // from the prefixed database column.
  1719. foreach ($field['columns'] as $column => $attributes) {
  1720. $column_name = _field_sql_storage_columnname($field_name, $column);
  1721. $item[$column] = $row->$column_name;
  1722. }
  1723. // Add the item to the field values for the entity.
  1724. $entities[$row->entity_id]->{$field_name}[$row->language][] = $item;
  1725. $delta_count[$row->entity_id][$row->language]++;
  1726. }
  1727. }
  1728. }
  1729. }
  1730. /**
  1731. * Write field data for an entity.
  1732. *
  1733. * This hook is invoked from field_attach_insert() and field_attach_update(),
  1734. * to ask the field storage module to save field data.
  1735. *
  1736. * @param $entity_type
  1737. * The entity type of entity, such as 'node' or 'user'.
  1738. * @param $entity
  1739. * The entity on which to operate.
  1740. * @param $op
  1741. * FIELD_STORAGE_UPDATE when updating an existing entity,
  1742. * FIELD_STORAGE_INSERT when inserting a new entity.
  1743. * @param $fields
  1744. * An array listing the fields to be written. The keys and values of the
  1745. * array are field IDs.
  1746. */
  1747. function hook_field_storage_write($entity_type, $entity, $op, $fields) {
  1748. list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
  1749. if (!isset($vid)) {
  1750. $vid = $id;
  1751. }
  1752. foreach ($fields as $field_id) {
  1753. $field = field_info_field_by_id($field_id);
  1754. $field_name = $field['field_name'];
  1755. $table_name = _field_sql_storage_tablename($field);
  1756. $revision_name = _field_sql_storage_revision_tablename($field);
  1757. $all_languages = field_available_languages($entity_type, $field);
  1758. $field_languages = array_intersect($all_languages, array_keys((array) $entity->$field_name));
  1759. // Delete and insert, rather than update, in case a value was added.
  1760. if ($op == FIELD_STORAGE_UPDATE) {
  1761. // Delete languages present in the incoming $entity->$field_name.
  1762. // Delete all languages if $entity->$field_name is empty.
  1763. $languages = !empty($entity->$field_name) ? $field_languages : $all_languages;
  1764. if ($languages) {
  1765. db_delete($table_name)
  1766. ->condition('entity_type', $entity_type)
  1767. ->condition('entity_id', $id)
  1768. ->condition('language', $languages, 'IN')
  1769. ->execute();
  1770. db_delete($revision_name)
  1771. ->condition('entity_type', $entity_type)
  1772. ->condition('entity_id', $id)
  1773. ->condition('revision_id', $vid)
  1774. ->condition('language', $languages, 'IN')
  1775. ->execute();
  1776. }
  1777. }
  1778. // Prepare the multi-insert query.
  1779. $do_insert = FALSE;
  1780. $columns = array('entity_type', 'entity_id', 'revision_id', 'bundle', 'delta', 'language');
  1781. foreach ($field['columns'] as $column => $attributes) {
  1782. $columns[] = _field_sql_storage_columnname($field_name, $column);
  1783. }
  1784. $query = db_insert($table_name)->fields($columns);
  1785. $revision_query = db_insert($revision_name)->fields($columns);
  1786. foreach ($field_languages as $langcode) {
  1787. $items = (array) $entity->{$field_name}[$langcode];
  1788. $delta_count = 0;
  1789. foreach ($items as $delta => $item) {
  1790. // We now know we have someting to insert.
  1791. $do_insert = TRUE;
  1792. $record = array(
  1793. 'entity_type' => $entity_type,
  1794. 'entity_id' => $id,
  1795. 'revision_id' => $vid,
  1796. 'bundle' => $bundle,
  1797. 'delta' => $delta,
  1798. 'language' => $langcode,
  1799. );
  1800. foreach ($field['columns'] as $column => $attributes) {
  1801. $record[_field_sql_storage_columnname($field_name, $column)] = isset($item[$column]) ? $item[$column] : NULL;
  1802. }
  1803. $query->values($record);
  1804. if (isset($vid)) {
  1805. $revision_query->values($record);
  1806. }
  1807. if ($field['cardinality'] != FIELD_CARDINALITY_UNLIMITED && ++$delta_count == $field['cardinality']) {
  1808. break;
  1809. }
  1810. }
  1811. }
  1812. // Execute the query if we have values to insert.
  1813. if ($do_insert) {
  1814. $query->execute();
  1815. $revision_query->execute();
  1816. }
  1817. }
  1818. }
  1819. /**
  1820. * Delete all field data for an entity.
  1821. *
  1822. * This hook is invoked from field_attach_delete() to ask the field storage
  1823. * module to delete field data.
  1824. *
  1825. * @param $entity_type
  1826. * The entity type of entity, such as 'node' or 'user'.
  1827. * @param $entity
  1828. * The entity on which to operate.
  1829. * @param $fields
  1830. * An array listing the fields to delete. The keys and values of the
  1831. * array are field IDs.
  1832. */
  1833. function hook_field_storage_delete($entity_type, $entity, $fields) {
  1834. list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
  1835. foreach (field_info_instances($entity_type, $bundle) as $instance) {
  1836. if (isset($fields[$instance['field_id']])) {
  1837. $field = field_info_field_by_id($instance['field_id']);
  1838. field_sql_storage_field_storage_purge($entity_type, $entity, $field, $instance);
  1839. }
  1840. }
  1841. }
  1842. /**
  1843. * Delete a single revision of field data for an entity.
  1844. *
  1845. * This hook is invoked from field_attach_delete_revision() to ask the field
  1846. * storage module to delete field revision data.
  1847. *
  1848. * Deleting the current (most recently written) revision is not
  1849. * allowed as has undefined results.
  1850. *
  1851. * @param $entity_type
  1852. * The entity type of entity, such as 'node' or 'user'.
  1853. * @param $entity
  1854. * The entity on which to operate. The revision to delete is
  1855. * indicated by the entity's revision ID property, as identified by
  1856. * hook_fieldable_info() for $entity_type.
  1857. * @param $fields
  1858. * An array listing the fields to delete. The keys and values of the
  1859. * array are field IDs.
  1860. */
  1861. function hook_field_storage_delete_revision($entity_type, $entity, $fields) {
  1862. list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
  1863. if (isset($vid)) {
  1864. foreach ($fields as $field_id) {
  1865. $field = field_info_field_by_id($field_id);
  1866. $revision_name = _field_sql_storage_revision_tablename($field);
  1867. db_delete($revision_name)
  1868. ->condition('entity_type', $entity_type)
  1869. ->condition('entity_id', $id)
  1870. ->condition('revision_id', $vid)
  1871. ->execute();
  1872. }
  1873. }
  1874. }
  1875. /**
  1876. * Execute an EntityFieldQuery.
  1877. *
  1878. * This hook is called to find the entities having certain entity and field
  1879. * conditions and sort them in the given field order. If the field storage
  1880. * engine also handles property sorts and orders, it should unset those
  1881. * properties in the called object to signal that those have been handled.
  1882. *
  1883. * @param EntityFieldQuery $query
  1884. * An EntityFieldQuery.
  1885. *
  1886. * @return
  1887. * See EntityFieldQuery::execute() for the return values.
  1888. */
  1889. function hook_field_storage_query($query) {
  1890. $groups = array();
  1891. if ($query->age == FIELD_LOAD_CURRENT) {
  1892. $tablename_function = '_field_sql_storage_tablename';
  1893. $id_key = 'entity_id';
  1894. }
  1895. else {
  1896. $tablename_function = '_field_sql_storage_revision_tablename';
  1897. $id_key = 'revision_id';
  1898. }
  1899. $table_aliases = array();
  1900. // Add tables for the fields used.
  1901. foreach ($query->fields as $key => $field) {
  1902. $tablename = $tablename_function($field);
  1903. // Every field needs a new table.
  1904. $table_alias = $tablename . $key;
  1905. $table_aliases[$key] = $table_alias;
  1906. if ($key) {
  1907. $select_query->join($tablename, $table_alias, "$table_alias.entity_type = $field_base_table.entity_type AND $table_alias.$id_key = $field_base_table.$id_key");
  1908. }
  1909. else {
  1910. $select_query = db_select($tablename, $table_alias);
  1911. $select_query->addTag('entity_field_access');
  1912. $select_query->addMetaData('base_table', $tablename);
  1913. $select_query->fields($table_alias, array('entity_type', 'entity_id', 'revision_id', 'bundle'));
  1914. $field_base_table = $table_alias;
  1915. }
  1916. if ($field['cardinality'] != 1) {
  1917. $select_query->distinct();
  1918. }
  1919. }
  1920. // Add field conditions.
  1921. foreach ($query->fieldConditions as $key => $condition) {
  1922. $table_alias = $table_aliases[$key];
  1923. $field = $condition['field'];
  1924. // Add the specified condition.
  1925. $sql_field = "$table_alias." . _field_sql_storage_columnname($field['field_name'], $condition['column']);
  1926. $query->addCondition($select_query, $sql_field, $condition);
  1927. // Add delta / language group conditions.
  1928. foreach (array('delta', 'language') as $column) {
  1929. if (isset($condition[$column . '_group'])) {
  1930. $group_name = $condition[$column . '_group'];
  1931. if (!isset($groups[$column][$group_name])) {
  1932. $groups[$column][$group_name] = $table_alias;
  1933. }
  1934. else {
  1935. $select_query->where("$table_alias.$column = " . $groups[$column][$group_name] . ".$column");
  1936. }
  1937. }
  1938. }
  1939. }
  1940. if (isset($query->deleted)) {
  1941. $select_query->condition("$field_base_table.deleted", (int) $query->deleted);
  1942. }
  1943. // Is there a need to sort the query by property?
  1944. $has_property_order = FALSE;
  1945. foreach ($query->order as $order) {
  1946. if ($order['type'] == 'property') {
  1947. $has_property_order = TRUE;
  1948. }
  1949. }
  1950. if ($query->propertyConditions || $has_property_order) {
  1951. if (empty($query->entityConditions['entity_type']['value'])) {
  1952. throw new EntityFieldQueryException('Property conditions and orders must have an entity type defined.');
  1953. }
  1954. $entity_type = $query->entityConditions['entity_type']['value'];
  1955. $entity_base_table = _field_sql_storage_query_join_entity($select_query, $entity_type, $field_base_table);
  1956. $query->entityConditions['entity_type']['operator'] = '=';
  1957. foreach ($query->propertyConditions as $property_condition) {
  1958. $query->addCondition($select_query, "$entity_base_table." . $property_condition['column'], $property_condition);
  1959. }
  1960. }
  1961. foreach ($query->entityConditions as $key => $condition) {
  1962. $query->addCondition($select_query, "$field_base_table.$key", $condition);
  1963. }
  1964. // Order the query.
  1965. foreach ($query->order as $order) {
  1966. if ($order['type'] == 'entity') {
  1967. $key = $order['specifier'];
  1968. $select_query->orderBy("$field_base_table.$key", $order['direction']);
  1969. }
  1970. elseif ($order['type'] == 'field') {
  1971. $specifier = $order['specifier'];
  1972. $field = $specifier['field'];
  1973. $table_alias = $table_aliases[$specifier['index']];
  1974. $sql_field = "$table_alias." . _field_sql_storage_columnname($field['field_name'], $specifier['column']);
  1975. $select_query->orderBy($sql_field, $order['direction']);
  1976. }
  1977. elseif ($order['type'] == 'property') {
  1978. $select_query->orderBy("$entity_base_table." . $order['specifier'], $order['direction']);
  1979. }
  1980. }
  1981. return $query->finishQuery($select_query, $id_key);
  1982. }
  1983. /**
  1984. * Act on creation of a new field.
  1985. *
  1986. * This hook is invoked from field_create_field() to ask the field storage
  1987. * module to save field information and prepare for storing field instances.
  1988. * If there is a problem, the field storage module should throw an exception.
  1989. *
  1990. * @param $field
  1991. * The field structure being created.
  1992. */
  1993. function hook_field_storage_create_field($field) {
  1994. $schema = _field_sql_storage_schema($field);
  1995. foreach ($schema as $name => $table) {
  1996. db_create_table($name, $table);
  1997. }
  1998. drupal_get_schema(NULL, TRUE);
  1999. }
  2000. /**
  2001. * Act on deletion of a field.
  2002. *
  2003. * This hook is invoked from field_delete_field() to ask the field storage
  2004. * module to mark all information stored in the field for deletion.
  2005. *
  2006. * @param $field
  2007. * The field being deleted.
  2008. */
  2009. function hook_field_storage_delete_field($field) {
  2010. // Mark all data associated with the field for deletion.
  2011. $field['deleted'] = 0;
  2012. $table = _field_sql_storage_tablename($field);
  2013. $revision_table = _field_sql_storage_revision_tablename($field);
  2014. db_update($table)
  2015. ->fields(array('deleted' => 1))
  2016. ->execute();
  2017. // Move the table to a unique name while the table contents are being deleted.
  2018. $field['deleted'] = 1;
  2019. $new_table = _field_sql_storage_tablename($field);
  2020. $revision_new_table = _field_sql_storage_revision_tablename($field);
  2021. db_rename_table($table, $new_table);
  2022. db_rename_table($revision_table, $revision_new_table);
  2023. drupal_get_schema(NULL, TRUE);
  2024. }
  2025. /**
  2026. * Act on deletion of a field instance.
  2027. *
  2028. * This hook is invoked from field_delete_instance() to ask the field storage
  2029. * module to mark all information stored for the field instance for deletion.
  2030. *
  2031. * @param $instance
  2032. * The instance being deleted.
  2033. */
  2034. function hook_field_storage_delete_instance($instance) {
  2035. $field = field_info_field($instance['field_name']);
  2036. $table_name = _field_sql_storage_tablename($field);
  2037. $revision_name = _field_sql_storage_revision_tablename($field);
  2038. db_update($table_name)
  2039. ->fields(array('deleted' => 1))
  2040. ->condition('entity_type', $instance['entity_type'])
  2041. ->condition('bundle', $instance['bundle'])
  2042. ->execute();
  2043. db_update($revision_name)
  2044. ->fields(array('deleted' => 1))
  2045. ->condition('entity_type', $instance['entity_type'])
  2046. ->condition('bundle', $instance['bundle'])
  2047. ->execute();
  2048. }
  2049. /**
  2050. * Act before the storage backends load field data.
  2051. *
  2052. * This hook allows modules to load data before the Field Storage API,
  2053. * optionally preventing the field storage module from doing so.
  2054. *
  2055. * This lets 3rd party modules override, mirror, shard, or otherwise store a
  2056. * subset of fields in a different way than the current storage engine.
  2057. * Possible use cases include per-bundle storage, per-combo-field storage, etc.
  2058. *
  2059. * Modules implementing this hook should load field values and add them to
  2060. * objects in $entities. Fields with no values should be added as empty
  2061. * arrays. In addition, fields loaded should be added as keys to $skip_fields.
  2062. *
  2063. * @param $entity_type
  2064. * The type of entity, such as 'node' or 'user'.
  2065. * @param $entities
  2066. * The array of entity objects to add fields to, keyed by entity ID.
  2067. * @param $age
  2068. * FIELD_LOAD_CURRENT to load the most recent revision for all fields, or
  2069. * FIELD_LOAD_REVISION to load the version indicated by each entity.
  2070. * @param $skip_fields
  2071. * An array keyed by field IDs whose data has already been loaded and
  2072. * therefore should not be loaded again. Add a key to this array to indicate
  2073. * that your module has already loaded a field.
  2074. * @param $options
  2075. * An associative array of additional options, with the following keys:
  2076. * - field_id: The field ID that should be loaded. If unset, all fields
  2077. * should be loaded.
  2078. * - deleted: If TRUE, deleted fields should be loaded as well as
  2079. * non-deleted fields. If unset or FALSE, only non-deleted fields should be
  2080. * loaded.
  2081. */
  2082. function hook_field_storage_pre_load($entity_type, $entities, $age, &$skip_fields, $options) {
  2083. // @todo Needs function body.
  2084. }
  2085. /**
  2086. * Act before the storage backends insert field data.
  2087. *
  2088. * This hook allows modules to store data before the Field Storage API,
  2089. * optionally preventing the field storage module from doing so.
  2090. *
  2091. * @param $entity_type
  2092. * The type of $entity; for example, 'node' or 'user'.
  2093. * @param $entity
  2094. * The entity with fields to save.
  2095. * @param $skip_fields
  2096. * An array keyed by field IDs whose data has already been written and
  2097. * therefore should not be written again. The values associated with these
  2098. * keys are not specified.
  2099. * @return
  2100. * Saved field IDs are set set as keys in $skip_fields.
  2101. */
  2102. function hook_field_storage_pre_insert($entity_type, $entity, &$skip_fields) {
  2103. if ($entity_type == 'node' && $entity->status && _forum_node_check_node_type($entity)) {
  2104. $query = db_insert('forum_index')->fields(array('nid', 'title', 'tid', 'sticky', 'created', 'comment_count', 'last_comment_timestamp'));
  2105. foreach ($entity->taxonomy_forums as $language) {
  2106. foreach ($language as $delta) {
  2107. $query->values(array(
  2108. 'nid' => $entity->nid,
  2109. 'title' => $entity->title,
  2110. 'tid' => $delta['value'],
  2111. 'sticky' => $entity->sticky,
  2112. 'created' => $entity->created,
  2113. 'comment_count' => 0,
  2114. 'last_comment_timestamp' => $entity->created,
  2115. ));
  2116. }
  2117. }
  2118. $query->execute();
  2119. }
  2120. }
  2121. /**
  2122. * Act before the storage backends update field data.
  2123. *
  2124. * This hook allows modules to store data before the Field Storage API,
  2125. * optionally preventing the field storage module from doing so.
  2126. *
  2127. * @param $entity_type
  2128. * The type of $entity; for example, 'node' or 'user'.
  2129. * @param $entity
  2130. * The entity with fields to save.
  2131. * @param $skip_fields
  2132. * An array keyed by field IDs whose data has already been written and
  2133. * therefore should not be written again. The values associated with these
  2134. * keys are not specified.
  2135. * @return
  2136. * Saved field IDs are set set as keys in $skip_fields.
  2137. */
  2138. function hook_field_storage_pre_update($entity_type, $entity, &$skip_fields) {
  2139. $first_call = &drupal_static(__FUNCTION__, array());
  2140. if ($entity_type == 'node' && $entity->status && _forum_node_check_node_type($entity)) {
  2141. // We don't maintain data for old revisions, so clear all previous values
  2142. // from the table. Since this hook runs once per field, per entity, make
  2143. // sure we only wipe values once.
  2144. if (!isset($first_call[$entity->nid])) {
  2145. $first_call[$entity->nid] = FALSE;
  2146. db_delete('forum_index')->condition('nid', $entity->nid)->execute();
  2147. }
  2148. // Only save data to the table if the node is published.
  2149. if ($entity->status) {
  2150. $query = db_insert('forum_index')->fields(array('nid', 'title', 'tid', 'sticky', 'created', 'comment_count', 'last_comment_timestamp'));
  2151. foreach ($entity->taxonomy_forums as $language) {
  2152. foreach ($language as $delta) {
  2153. $query->values(array(
  2154. 'nid' => $entity->nid,
  2155. 'title' => $entity->title,
  2156. 'tid' => $delta['value'],
  2157. 'sticky' => $entity->sticky,
  2158. 'created' => $entity->created,
  2159. 'comment_count' => 0,
  2160. 'last_comment_timestamp' => $entity->created,
  2161. ));
  2162. }
  2163. }
  2164. $query->execute();
  2165. // The logic for determining last_comment_count is fairly complex, so
  2166. // call _forum_update_forum_index() too.
  2167. _forum_update_forum_index($entity->nid);
  2168. }
  2169. }
  2170. }
  2171. /**
  2172. * @} End of "addtogroup field_storage
  2173. */
  2174. /**
  2175. * Returns the maximum weight for the entity components handled by the module.
  2176. *
  2177. * Field API takes care of fields and 'extra_fields'. This hook is intended for
  2178. * third-party modules adding other entity components (e.g. field_group).
  2179. *
  2180. * @param $entity_type
  2181. * The type of entity; e.g. 'node' or 'user'.
  2182. * @param $bundle
  2183. * The bundle name.
  2184. * @param $context
  2185. * The context for which the maximum weight is requested. Either 'form', or
  2186. * the name of a view mode.
  2187. *
  2188. * @return
  2189. * The maximum weight of the entity's components, or NULL if no components
  2190. * were found.
  2191. *
  2192. * @ingroup field_info
  2193. */
  2194. function hook_field_info_max_weight($entity_type, $bundle, $context) {
  2195. $weights = array();
  2196. foreach (my_module_entity_additions($entity_type, $bundle, $context) as $addition) {
  2197. $weights[] = $addition['weight'];
  2198. }
  2199. return $weights ? max($weights) : NULL;
  2200. }
  2201. /**
  2202. * @addtogroup field_types
  2203. * @{
  2204. */
  2205. /**
  2206. * Alters the display settings of a field before it gets displayed.
  2207. *
  2208. * Note that instead of hook_field_display_alter(), which is called for all
  2209. * fields on all entity types, hook_field_display_ENTITY_TYPE_alter() may be
  2210. * used to alter display settings for fields on a specific entity type only.
  2211. *
  2212. * This hook is called once per field per displayed entity. If the result of the
  2213. * hook involves reading from the database, it is highly recommended to
  2214. * statically cache the information.
  2215. *
  2216. * @param $display
  2217. * The display settings that will be used to display the field values, as
  2218. * found in the 'display' key of $instance definitions.
  2219. * @param $context
  2220. * An associative array containing:
  2221. * - entity_type: The entity type; e.g., 'node' or 'user'.
  2222. * - field: The field being rendered.
  2223. * - instance: The instance being rendered.
  2224. * - entity: The entity being rendered.
  2225. * - view_mode: The view mode, e.g. 'full', 'teaser'...
  2226. *
  2227. * @see hook_field_display_ENTITY_TYPE_alter()
  2228. */
  2229. function hook_field_display_alter(&$display, $context) {
  2230. // Leave field labels out of the search index.
  2231. // Note: The check against $context['entity_type'] == 'node' could be avoided
  2232. // by using hook_field_display_node_alter() instead of
  2233. // hook_field_display_alter(), resulting in less function calls when
  2234. // rendering non-node entities.
  2235. if ($context['entity_type'] == 'node' && $context['view_mode'] == 'search_index') {
  2236. $display['label'] = 'hidden';
  2237. }
  2238. }
  2239. /**
  2240. * Alters the display settings of a field on a given entity type before it gets displayed.
  2241. *
  2242. * Modules can implement hook_field_display_ENTITY_TYPE_alter() to alter display
  2243. * settings for fields on a specific entity type, rather than implementing
  2244. * hook_field_display_alter().
  2245. *
  2246. * This hook is called once per field per displayed entity. If the result of the
  2247. * hook involves reading from the database, it is highly recommended to
  2248. * statically cache the information.
  2249. *
  2250. * @param $display
  2251. * The display settings that will be used to display the field values, as
  2252. * found in the 'display' key of $instance definitions.
  2253. * @param $context
  2254. * An associative array containing:
  2255. * - entity_type: The entity type; e.g., 'node' or 'user'.
  2256. * - field: The field being rendered.
  2257. * - instance: The instance being rendered.
  2258. * - entity: The entity being rendered.
  2259. * - view_mode: The view mode, e.g. 'full', 'teaser'...
  2260. *
  2261. * @see hook_field_display_alter()
  2262. */
  2263. function hook_field_display_ENTITY_TYPE_alter(&$display, $context) {
  2264. // Leave field labels out of the search index.
  2265. if ($context['view_mode'] == 'search_index') {
  2266. $display['label'] = 'hidden';
  2267. }
  2268. }
  2269. /**
  2270. * @} End of "addtogroup field_types
  2271. */
  2272. /**
  2273. * Alters the display settings of pseudo-fields before an entity is displayed.
  2274. *
  2275. * This hook is called once per displayed entity. If the result of the hook
  2276. * involves reading from the database, it is highly recommended to statically
  2277. * cache the information.
  2278. *
  2279. * @param $displays
  2280. * An array of display settings for the pseudo-fields in the entity, keyed
  2281. * by pseudo-field names.
  2282. * @param $context
  2283. * An associative array containing:
  2284. * - entity_type: The entity type; e.g., 'node' or 'user'.
  2285. * - bundle: The bundle name.
  2286. * - view_mode: The view mode, e.g. 'full', 'teaser'...
  2287. *
  2288. * @ingroup field_types
  2289. */
  2290. function hook_field_extra_fields_display_alter(&$displays, $context) {
  2291. if ($context['entity_type'] == 'taxonomy_term' && $context['view_mode'] == 'full') {
  2292. $displays['description']['visible'] = FALSE;
  2293. }
  2294. }
  2295. /**
  2296. * Alters the widget properties of a field instance on a given entity type
  2297. * before it gets displayed.
  2298. *
  2299. * Modules can implement hook_field_widget_properties_ENTITY_TYPE_alter() to
  2300. * alter the widget properties for fields on a specific entity type, rather than
  2301. * implementing hook_field_widget_properties_alter().
  2302. *
  2303. * This hook is called once per field per displayed widget entity. If the result
  2304. * of the hook involves reading from the database, it is highly recommended to
  2305. * statically cache the information.
  2306. *
  2307. * @param $widget
  2308. * The instance's widget properties.
  2309. * @param $context
  2310. * An associative array containing:
  2311. * - entity_type: The entity type; e.g., 'node' or 'user'.
  2312. * - entity: The entity object.
  2313. * - field: The field that the widget belongs to.
  2314. * - instance: The instance of the field.
  2315. *
  2316. * @see hook_field_widget_properties_alter()
  2317. *
  2318. * @ingroup field_widget
  2319. */
  2320. function hook_field_widget_properties_ENTITY_TYPE_alter(&$widget, $context) {
  2321. // Change a widget's type according to the time of day.
  2322. $field = $context['field'];
  2323. if ($field['field_name'] == 'field_foo') {
  2324. $time = date('H');
  2325. $widget['type'] = $time < 12 ? 'widget_am' : 'widget_pm';
  2326. }
  2327. }
  2328. /**
  2329. * @addtogroup field_crud
  2330. * @{
  2331. */
  2332. /**
  2333. * Act on a field being created.
  2334. *
  2335. * This hook is invoked from field_create_field() after the field is created, to
  2336. * allow modules to act on field creation.
  2337. *
  2338. * @param $field
  2339. * The field just created.
  2340. */
  2341. function hook_field_create_field($field) {
  2342. // @todo Needs function body.
  2343. }
  2344. /**
  2345. * Act on a field instance being created.
  2346. *
  2347. * This hook is invoked from field_create_instance() after the instance record
  2348. * is saved, so it cannot be used to modify the instance itself.
  2349. *
  2350. * @param $instance
  2351. * The instance just created.
  2352. */
  2353. function hook_field_create_instance($instance) {
  2354. // @todo Needs function body.
  2355. }
  2356. /**
  2357. * Forbid a field update from occurring.
  2358. *
  2359. * Any module may forbid any update for any reason. For example, the
  2360. * field's storage module might forbid an update if it would change
  2361. * the storage schema while data for the field exists. A field type
  2362. * module might forbid an update if it would change existing data's
  2363. * semantics, or if there are external dependencies on field settings
  2364. * that cannot be updated.
  2365. *
  2366. * To forbid the update from occurring, throw a FieldUpdateForbiddenException.
  2367. *
  2368. * @param $field
  2369. * The field as it will be post-update.
  2370. * @param $prior_field
  2371. * The field as it is pre-update.
  2372. * @param $has_data
  2373. * Whether any data already exists for this field.
  2374. */
  2375. function hook_field_update_forbid($field, $prior_field, $has_data) {
  2376. // A 'list' field stores integer keys mapped to display values. If
  2377. // the new field will have fewer values, and any data exists for the
  2378. // abandoned keys, the field will have no way to display them. So,
  2379. // forbid such an update.
  2380. if ($has_data && count($field['settings']['allowed_values']) < count($prior_field['settings']['allowed_values'])) {
  2381. // Identify the keys that will be lost.
  2382. $lost_keys = array_diff(array_keys($field['settings']['allowed_values']), array_keys($prior_field['settings']['allowed_values']));
  2383. // If any data exist for those keys, forbid the update.
  2384. $query = new EntityFieldQuery();
  2385. $found = $query
  2386. ->fieldCondition($prior_field['field_name'], 'value', $lost_keys)
  2387. ->range(0, 1)
  2388. ->execute();
  2389. if ($found) {
  2390. throw new FieldUpdateForbiddenException("Cannot update a list field not to include keys with existing data");
  2391. }
  2392. }
  2393. }
  2394. /**
  2395. * Act on a field being updated.
  2396. *
  2397. * This hook is invoked just after field is updated in field_update_field().
  2398. *
  2399. * @param $field
  2400. * The field as it is post-update.
  2401. * @param $prior_field
  2402. * The field as it was pre-update.
  2403. * @param $has_data
  2404. * Whether any data already exists for this field.
  2405. */
  2406. function hook_field_update_field($field, $prior_field, $has_data) {
  2407. // Reset the static value that keeps track of allowed values for list fields.
  2408. drupal_static_reset('list_allowed_values');
  2409. }
  2410. /**
  2411. * Act on a field being deleted.
  2412. *
  2413. * This hook is invoked just after a field is deleted by field_delete_field().
  2414. *
  2415. * @param $field
  2416. * The field just deleted.
  2417. */
  2418. function hook_field_delete_field($field) {
  2419. // @todo Needs function body.
  2420. }
  2421. /**
  2422. * Act on a field instance being updated.
  2423. *
  2424. * This hook is invoked from field_update_instance() after the instance record
  2425. * is saved, so it cannot be used by a module to modify the instance itself.
  2426. *
  2427. * @param $instance
  2428. * The instance as it is post-update.
  2429. * @param $prior_instance
  2430. * The instance as it was pre-update.
  2431. */
  2432. function hook_field_update_instance($instance, $prior_instance) {
  2433. // @todo Needs function body.
  2434. }
  2435. /**
  2436. * Act on a field instance being deleted.
  2437. *
  2438. * This hook is invoked from field_delete_instance() after the instance is
  2439. * deleted.
  2440. *
  2441. * @param $instance
  2442. * The instance just deleted.
  2443. */
  2444. function hook_field_delete_instance($instance) {
  2445. // @todo Needs function body.
  2446. }
  2447. /**
  2448. * Act on field records being read from the database.
  2449. *
  2450. * This hook is invoked from field_read_fields() on each field being read.
  2451. *
  2452. * @param $field
  2453. * The field record just read from the database.
  2454. */
  2455. function hook_field_read_field($field) {
  2456. // @todo Needs function body.
  2457. }
  2458. /**
  2459. * Act on a field record being read from the database.
  2460. *
  2461. * This hook is invoked from field_read_instances() on each instance being read.
  2462. *
  2463. * @param $instance
  2464. * The instance record just read from the database.
  2465. */
  2466. function hook_field_read_instance($instance) {
  2467. // @todo Needs function body.
  2468. }
  2469. /**
  2470. * Acts when a field record is being purged.
  2471. *
  2472. * In field_purge_field(), after the field configuration has been
  2473. * removed from the database, the field storage module has had a chance to
  2474. * run its hook_field_storage_purge_field(), and the field info cache
  2475. * has been cleared, this hook is invoked on all modules to allow them to
  2476. * respond to the field being purged.
  2477. *
  2478. * @param $field
  2479. * The field being purged.
  2480. */
  2481. function hook_field_purge_field($field) {
  2482. db_delete('my_module_field_info')
  2483. ->condition('id', $field['id'])
  2484. ->execute();
  2485. }
  2486. /**
  2487. * Acts when a field instance is being purged.
  2488. *
  2489. * In field_purge_instance(), after the field instance has been
  2490. * removed from the database, the field storage module has had a chance to
  2491. * run its hook_field_storage_purge_instance(), and the field info cache
  2492. * has been cleared, this hook is invoked on all modules to allow them to
  2493. * respond to the field instance being purged.
  2494. *
  2495. * @param $instance
  2496. * The instance being purged.
  2497. */
  2498. function hook_field_purge_instance($instance) {
  2499. db_delete('my_module_field_instance_info')
  2500. ->condition('id', $instance['id'])
  2501. ->execute();
  2502. }
  2503. /**
  2504. * Remove field storage information when a field record is purged.
  2505. *
  2506. * Called from field_purge_field() to allow the field storage module
  2507. * to remove field information when a field is being purged.
  2508. *
  2509. * @param $field
  2510. * The field being purged.
  2511. *
  2512. * @ingroup field_storage
  2513. */
  2514. function hook_field_storage_purge_field($field) {
  2515. $table_name = _field_sql_storage_tablename($field);
  2516. $revision_name = _field_sql_storage_revision_tablename($field);
  2517. db_drop_table($table_name);
  2518. db_drop_table($revision_name);
  2519. }
  2520. /**
  2521. * Remove field storage information when a field instance is purged.
  2522. *
  2523. * Called from field_purge_instance() to allow the field storage module
  2524. * to remove field instance information when a field instance is being
  2525. * purged.
  2526. *
  2527. * @param $instance
  2528. * The instance being purged.
  2529. *
  2530. * @ingroup field_storage
  2531. */
  2532. function hook_field_storage_purge_field_instance($instance) {
  2533. db_delete('my_module_field_instance_info')
  2534. ->condition('id', $instance['id'])
  2535. ->execute();
  2536. }
  2537. /**
  2538. * Remove field storage information when field data is purged.
  2539. *
  2540. * Called from field_purge_data() to allow the field storage
  2541. * module to delete field data information.
  2542. *
  2543. * @param $entity_type
  2544. * The type of $entity; for example, 'node' or 'user'.
  2545. * @param $entity
  2546. * The pseudo-entity whose field data to delete.
  2547. * @param $field
  2548. * The (possibly deleted) field whose data is being purged.
  2549. * @param $instance
  2550. * The deleted field instance whose data is being purged.
  2551. *
  2552. * @ingroup field_storage
  2553. */
  2554. function hook_field_storage_purge($entity_type, $entity, $field, $instance) {
  2555. list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
  2556. $table_name = _field_sql_storage_tablename($field);
  2557. $revision_name = _field_sql_storage_revision_tablename($field);
  2558. db_delete($table_name)
  2559. ->condition('entity_type', $entity_type)
  2560. ->condition('entity_id', $id)
  2561. ->execute();
  2562. db_delete($revision_name)
  2563. ->condition('entity_type', $entity_type)
  2564. ->condition('entity_id', $id)
  2565. ->execute();
  2566. }
  2567. /**
  2568. * @} End of "addtogroup field_crud".
  2569. */
  2570. /**
  2571. * Determine whether the user has access to a given field.
  2572. *
  2573. * This hook is invoked from field_access() to let modules block access to
  2574. * operations on fields. If no module returns FALSE, the operation is allowed.
  2575. *
  2576. * @param $op
  2577. * The operation to be performed. Possible values: 'edit', 'view'.
  2578. * @param $field
  2579. * The field on which the operation is to be performed.
  2580. * @param $entity_type
  2581. * The type of $entity; for example, 'node' or 'user'.
  2582. * @param $entity
  2583. * (optional) The entity for the operation.
  2584. * @param $account
  2585. * (optional) The account to check; if not given use currently logged in user.
  2586. *
  2587. * @return
  2588. * TRUE if the operation is allowed, and FALSE if the operation is denied.
  2589. *
  2590. * @ingroup field_types
  2591. */
  2592. function hook_field_access($op, $field, $entity_type, $entity, $account) {
  2593. if ($field['field_name'] == 'field_of_interest' && $op == 'edit') {
  2594. return user_access('edit field of interest', $account);
  2595. }
  2596. return TRUE;
  2597. }
  2598. /**
  2599. * @} End of "addtogroup hooks".
  2600. */