field.api.php 95 KB

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