field.api.php 98 KB

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