field.info.inc 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  1. <?php
  2. /**
  3. * @file
  4. * Field Info API, providing information about available fields and field types.
  5. */
  6. /**
  7. * @defgroup field_info Field Info API
  8. * @{
  9. * Obtain information about Field API configuration.
  10. *
  11. * The Field Info API exposes information about field types, fields,
  12. * instances, bundles, widget types, display formatters, behaviors,
  13. * and settings defined by or with the Field API.
  14. *
  15. * See @link field Field API @endlink for information about the other parts of
  16. * the Field API.
  17. */
  18. /**
  19. * Clears the field info cache without clearing the field data cache.
  20. *
  21. * This is useful when deleted fields or instances are purged. We
  22. * need to remove the purged records, but no actual field data items
  23. * are affected.
  24. */
  25. function field_info_cache_clear() {
  26. drupal_static_reset('field_view_mode_settings');
  27. drupal_static_reset('field_available_languages');
  28. // @todo: Remove this when field_attach_*_bundle() bundle management
  29. // functions are moved to the entity API.
  30. entity_info_cache_clear();
  31. _field_info_collate_types(TRUE);
  32. _field_info_collate_fields(TRUE);
  33. }
  34. /**
  35. * Collates all information on field types, widget types and related structures.
  36. *
  37. * @param $reset
  38. * If TRUE, clear the cache. The information will be rebuilt from the database
  39. * next time it is needed. Defaults to FALSE.
  40. *
  41. * @return
  42. * If $reset is TRUE, nothing.
  43. * If $reset is FALSE, an array containing the following elements:
  44. * - 'field types': Array of hook_field_info() results, keyed by field_type.
  45. * Each element has the following components: label, description, settings,
  46. * instance_settings, default_widget, default_formatter, and behaviors
  47. * from hook_field_info(), as well as module, giving the module that exposes
  48. * the field type.
  49. * - 'widget types': Array of hook_field_widget_info() results, keyed by
  50. * widget_type. Each element has the following components: label, field
  51. * types, settings, and behaviors from hook_field_widget_info(), as well
  52. * as module, giving the module that exposes the widget type.
  53. * - 'formatter types': Array of hook_field_formatter_info() results, keyed by
  54. * formatter_type. Each element has the following components: label, field
  55. * types, and behaviors from hook_field_formatter_info(), as well as
  56. * module, giving the module that exposes the formatter type.
  57. * - 'storage types': Array of hook_field_storage_info() results, keyed by
  58. * storage type names. Each element has the following components: label,
  59. * description, and settings from hook_field_storage_info(), as well as
  60. * module, giving the module that exposes the storage type.
  61. * - 'fieldable types': Array of hook_entity_info() results, keyed by
  62. * entity_type. Each element has the following components: name, id key,
  63. * revision key, bundle key, cacheable, and bundles from hook_entity_info(),
  64. * as well as module, giving the module that exposes the entity type.
  65. */
  66. function _field_info_collate_types($reset = FALSE) {
  67. global $language;
  68. static $info;
  69. // The _info() hooks invoked below include translated strings, so each
  70. // language is cached separately.
  71. $langcode = $language->language;
  72. if ($reset) {
  73. $info = NULL;
  74. // Clear all languages.
  75. cache_clear_all('field_info_types:', 'cache_field', TRUE);
  76. return;
  77. }
  78. if (!isset($info)) {
  79. if ($cached = cache_get("field_info_types:$langcode", 'cache_field')) {
  80. $info = $cached->data;
  81. }
  82. else {
  83. $info = array(
  84. 'field types' => array(),
  85. 'widget types' => array(),
  86. 'formatter types' => array(),
  87. 'storage types' => array(),
  88. );
  89. // Populate field types.
  90. foreach (module_implements('field_info') as $module) {
  91. $field_types = (array) module_invoke($module, 'field_info');
  92. foreach ($field_types as $name => $field_info) {
  93. // Provide defaults.
  94. $field_info += array(
  95. 'settings' => array(),
  96. 'instance_settings' => array(),
  97. );
  98. $info['field types'][$name] = $field_info;
  99. $info['field types'][$name]['module'] = $module;
  100. }
  101. }
  102. drupal_alter('field_info', $info['field types']);
  103. // Populate widget types.
  104. foreach (module_implements('field_widget_info') as $module) {
  105. $widget_types = (array) module_invoke($module, 'field_widget_info');
  106. foreach ($widget_types as $name => $widget_info) {
  107. // Provide defaults.
  108. $widget_info += array(
  109. 'settings' => array(),
  110. );
  111. $info['widget types'][$name] = $widget_info;
  112. $info['widget types'][$name]['module'] = $module;
  113. }
  114. }
  115. drupal_alter('field_widget_info', $info['widget types']);
  116. // Populate formatter types.
  117. foreach (module_implements('field_formatter_info') as $module) {
  118. $formatter_types = (array) module_invoke($module, 'field_formatter_info');
  119. foreach ($formatter_types as $name => $formatter_info) {
  120. // Provide defaults.
  121. $formatter_info += array(
  122. 'settings' => array(),
  123. );
  124. $info['formatter types'][$name] = $formatter_info;
  125. $info['formatter types'][$name]['module'] = $module;
  126. }
  127. }
  128. drupal_alter('field_formatter_info', $info['formatter types']);
  129. // Populate storage types.
  130. foreach (module_implements('field_storage_info') as $module) {
  131. $storage_types = (array) module_invoke($module, 'field_storage_info');
  132. foreach ($storage_types as $name => $storage_info) {
  133. // Provide defaults.
  134. $storage_info += array(
  135. 'settings' => array(),
  136. );
  137. $info['storage types'][$name] = $storage_info;
  138. $info['storage types'][$name]['module'] = $module;
  139. }
  140. }
  141. drupal_alter('field_storage_info', $info['storage types']);
  142. cache_set("field_info_types:$langcode", $info, 'cache_field');
  143. }
  144. }
  145. return $info;
  146. }
  147. /**
  148. * Collates all information on existing fields and instances.
  149. *
  150. * @param $reset
  151. * If TRUE, clear the cache. The information will be rebuilt from the
  152. * database next time it is needed. Defaults to FALSE.
  153. *
  154. * @return
  155. * If $reset is TRUE, nothing.
  156. * If $reset is FALSE, an array containing the following elements:
  157. * - fields: Array of existing fields, keyed by field ID. This element
  158. * lists deleted and non-deleted fields, but not inactive ones.
  159. * Each field has an additional element, 'bundles', which is an array
  160. * of all non-deleted instances of that field.
  161. * - field_ids: Array of field IDs, keyed by field name. This element
  162. * only lists non-deleted, active fields.
  163. * - instances: Array of existing instances, keyed by entity type, bundle
  164. * name and field name. This element only lists non-deleted instances
  165. * whose field is active.
  166. */
  167. function _field_info_collate_fields($reset = FALSE) {
  168. static $info;
  169. if ($reset) {
  170. $info = NULL;
  171. cache_clear_all('field_info_fields', 'cache_field');
  172. return;
  173. }
  174. if (!isset($info)) {
  175. if ($cached = cache_get('field_info_fields', 'cache_field')) {
  176. $info = $cached->data;
  177. }
  178. else {
  179. $definitions = array(
  180. 'field_ids' => field_read_fields(array(), array('include_deleted' => 1)),
  181. 'instances' => field_read_instances(),
  182. );
  183. // Populate 'fields' with all fields, keyed by ID.
  184. $info['fields'] = array();
  185. foreach ($definitions['field_ids'] as $key => $field) {
  186. $info['fields'][$key] = $definitions['field_ids'][$key] = _field_info_prepare_field($field);
  187. }
  188. // Build an array of field IDs for non-deleted fields, keyed by name.
  189. $info['field_ids'] = array();
  190. foreach ($info['fields'] as $key => $field) {
  191. if (!$field['deleted']) {
  192. $info['field_ids'][$field['field_name']] = $key;
  193. }
  194. }
  195. // Populate 'instances'. Only non-deleted instances are considered.
  196. $info['instances'] = array();
  197. foreach (field_info_bundles() as $entity_type => $bundles) {
  198. foreach ($bundles as $bundle => $bundle_info) {
  199. $info['instances'][$entity_type][$bundle] = array();
  200. }
  201. }
  202. foreach ($definitions['instances'] as $instance) {
  203. $field = $info['fields'][$instance['field_id']];
  204. $instance = _field_info_prepare_instance($instance, $field);
  205. $info['instances'][$instance['entity_type']][$instance['bundle']][$instance['field_name']] = $instance;
  206. // Enrich field definitions with the list of bundles where they have
  207. // instances. NOTE: Deleted fields in $info['field_ids'] are not
  208. // enriched because all of their instances are deleted, too, and
  209. // are thus not in $definitions['instances'].
  210. $info['fields'][$instance['field_id']]['bundles'][$instance['entity_type']][] = $instance['bundle'];
  211. }
  212. // Populate 'extra_fields'.
  213. $extra = module_invoke_all('field_extra_fields');
  214. drupal_alter('field_extra_fields', $extra);
  215. // Merge in saved settings.
  216. foreach ($extra as $entity_type => $bundles) {
  217. foreach ($bundles as $bundle => $extra_fields) {
  218. $extra_fields = _field_info_prepare_extra_fields($extra_fields, $entity_type, $bundle);
  219. $info['extra_fields'][$entity_type][$bundle] = $extra_fields;
  220. }
  221. }
  222. cache_set('field_info_fields', $info, 'cache_field');
  223. }
  224. }
  225. return $info;
  226. }
  227. /**
  228. * Prepares a field definition for the current run-time context.
  229. *
  230. * Since the field was last saved or updated, new field settings can be
  231. * expected.
  232. *
  233. * @param $field
  234. * The raw field structure as read from the database.
  235. */
  236. function _field_info_prepare_field($field) {
  237. // Make sure all expected field settings are present.
  238. $field['settings'] += field_info_field_settings($field['type']);
  239. $field['storage']['settings'] += field_info_storage_settings($field['storage']['type']);
  240. // Add storage details.
  241. $details = (array) module_invoke($field['storage']['module'], 'field_storage_details', $field);
  242. drupal_alter('field_storage_details', $details, $field, $instance);
  243. $field['storage']['details'] = $details;
  244. // Initialize the 'bundles' list.
  245. $field['bundles'] = array();
  246. return $field;
  247. }
  248. /**
  249. * Prepares an instance definition for the current run-time context.
  250. *
  251. * Since the instance was last saved or updated, a number of things might have
  252. * changed: widgets or formatters disabled, new settings expected, new view
  253. * modes added...
  254. *
  255. * @param $instance
  256. * The raw instance structure as read from the database.
  257. * @param $field
  258. * The field structure for the instance.
  259. *
  260. * @return
  261. * Field instance array.
  262. */
  263. function _field_info_prepare_instance($instance, $field) {
  264. // Make sure all expected instance settings are present.
  265. $instance['settings'] += field_info_instance_settings($field['type']);
  266. // Set a default value for the instance.
  267. if (field_behaviors_widget('default value', $instance) == FIELD_BEHAVIOR_DEFAULT && !isset($instance['default_value'])) {
  268. $instance['default_value'] = NULL;
  269. }
  270. $instance['widget'] = _field_info_prepare_instance_widget($field, $instance['widget']);
  271. foreach ($instance['display'] as $view_mode => $display) {
  272. $instance['display'][$view_mode] = _field_info_prepare_instance_display($field, $display);
  273. }
  274. // Fallback to 'hidden' for view modes configured to use custom display
  275. // settings, and for which the instance has no explicit settings.
  276. $entity_info = entity_get_info($instance['entity_type']);
  277. $view_modes = array_merge(array('default'), array_keys($entity_info['view modes']));
  278. $view_mode_settings = field_view_mode_settings($instance['entity_type'], $instance['bundle']);
  279. foreach ($view_modes as $view_mode) {
  280. if ($view_mode == 'default' || !empty($view_mode_settings[$view_mode]['custom_settings'])) {
  281. if (!isset($instance['display'][$view_mode])) {
  282. $instance['display'][$view_mode] = array(
  283. 'type' => 'hidden',
  284. 'label' => 'above',
  285. 'settings' => array(),
  286. 'weight' => 0,
  287. );
  288. }
  289. }
  290. }
  291. return $instance;
  292. }
  293. /**
  294. * Adapts display specifications to the current run-time context.
  295. *
  296. * @param $field
  297. * The field structure for the instance.
  298. * @param $display
  299. * Display specifications as found in
  300. * $instance['display']['some_view_mode'].
  301. */
  302. function _field_info_prepare_instance_display($field, $display) {
  303. $field_type = field_info_field_types($field['type']);
  304. // Fill in default values.
  305. $display += array(
  306. 'label' => 'above',
  307. 'type' => $field_type['default_formatter'],
  308. 'settings' => array(),
  309. 'weight' => 0,
  310. );
  311. if ($display['type'] != 'hidden') {
  312. $formatter_type = field_info_formatter_types($display['type']);
  313. // Fallback to default formatter if formatter type is not available.
  314. if (!$formatter_type) {
  315. $display['type'] = $field_type['default_formatter'];
  316. $formatter_type = field_info_formatter_types($display['type']);
  317. }
  318. $display['module'] = $formatter_type['module'];
  319. // Fill in default settings for the formatter.
  320. $display['settings'] += field_info_formatter_settings($display['type']);
  321. }
  322. return $display;
  323. }
  324. /**
  325. * Prepares widget specifications for the current run-time context.
  326. *
  327. * @param $field
  328. * The field structure for the instance.
  329. * @param $widget
  330. * Widget specifications as found in $instance['widget'].
  331. */
  332. function _field_info_prepare_instance_widget($field, $widget) {
  333. $field_type = field_info_field_types($field['type']);
  334. // Fill in default values.
  335. $widget += array(
  336. 'type' => $field_type['default_widget'],
  337. 'settings' => array(),
  338. 'weight' => 0,
  339. );
  340. $widget_type = field_info_widget_types($widget['type']);
  341. // Fallback to default formatter if formatter type is not available.
  342. if (!$widget_type) {
  343. $widget['type'] = $field_type['default_widget'];
  344. $widget_type = field_info_widget_types($widget['type']);
  345. }
  346. $widget['module'] = $widget_type['module'];
  347. // Fill in default settings for the widget.
  348. $widget['settings'] += field_info_widget_settings($widget['type']);
  349. return $widget;
  350. }
  351. /**
  352. * Prepares 'extra fields' for the current run-time context.
  353. *
  354. * @param $extra_fields
  355. * The array of extra fields, as collected in hook_field_extra_fields().
  356. * @param $entity_type
  357. * The entity type.
  358. * @param $bundle
  359. * The bundle name.
  360. */
  361. function _field_info_prepare_extra_fields($extra_fields, $entity_type, $bundle) {
  362. $entity_type_info = entity_get_info($entity_type);
  363. $bundle_settings = field_bundle_settings($entity_type, $bundle);
  364. $extra_fields += array('form' => array(), 'display' => array());
  365. $result = array();
  366. // Extra fields in forms.
  367. foreach ($extra_fields['form'] as $name => $field_data) {
  368. $settings = isset($bundle_settings['extra_fields']['form'][$name]) ? $bundle_settings['extra_fields']['form'][$name] : array();
  369. if (isset($settings['weight'])) {
  370. $field_data['weight'] = $settings['weight'];
  371. }
  372. $result['form'][$name] = $field_data;
  373. }
  374. // Extra fields in displayed entities.
  375. $data = $extra_fields['display'];
  376. foreach ($extra_fields['display'] as $name => $field_data) {
  377. $settings = isset($bundle_settings['extra_fields']['display'][$name]) ? $bundle_settings['extra_fields']['display'][$name] : array();
  378. $view_modes = array_merge(array('default'), array_keys($entity_type_info['view modes']));
  379. foreach ($view_modes as $view_mode) {
  380. if (isset($settings[$view_mode])) {
  381. $field_data['display'][$view_mode] = $settings[$view_mode];
  382. }
  383. else {
  384. $field_data['display'][$view_mode] = array(
  385. 'weight' => $field_data['weight'],
  386. 'visible' => TRUE,
  387. );
  388. }
  389. }
  390. unset($field_data['weight']);
  391. $result['display'][$name] = $field_data;
  392. }
  393. return $result;
  394. }
  395. /**
  396. * Determines the behavior of a widget with respect to an operation.
  397. *
  398. * @param $op
  399. * The name of the operation. Currently supported: 'default value',
  400. * 'multiple values'.
  401. * @param $instance
  402. * The field instance array.
  403. *
  404. * @return
  405. * One of these values:
  406. * - FIELD_BEHAVIOR_NONE: Do nothing for this operation.
  407. * - FIELD_BEHAVIOR_CUSTOM: Use the widget's callback function.
  408. * - FIELD_BEHAVIOR_DEFAULT: Use field.module default behavior.
  409. */
  410. function field_behaviors_widget($op, $instance) {
  411. $info = field_info_widget_types($instance['widget']['type']);
  412. return isset($info['behaviors'][$op]) ? $info['behaviors'][$op] : FIELD_BEHAVIOR_DEFAULT;
  413. }
  414. /**
  415. * Returns information about field types from hook_field_info().
  416. *
  417. * @param $field_type
  418. * (optional) A field type name. If omitted, all field types will be
  419. * returned.
  420. *
  421. * @return
  422. * Either a field type description, as provided by hook_field_info(), or an
  423. * array of all existing field types, keyed by field type name.
  424. */
  425. function field_info_field_types($field_type = NULL) {
  426. $info = _field_info_collate_types();
  427. $field_types = $info['field types'];
  428. if ($field_type) {
  429. if (isset($field_types[$field_type])) {
  430. return $field_types[$field_type];
  431. }
  432. }
  433. else {
  434. return $field_types;
  435. }
  436. }
  437. /**
  438. * Returns information about field widgets from hook_field_widget_info().
  439. *
  440. * @param $widget_type
  441. * (optional) A widget type name. If omitted, all widget types will be
  442. * returned.
  443. *
  444. * @return
  445. * Either a single widget type description, as provided by
  446. * hook_field_widget_info(), or an array of all existing widget types, keyed
  447. * by widget type name.
  448. */
  449. function field_info_widget_types($widget_type = NULL) {
  450. $info = _field_info_collate_types();
  451. $widget_types = $info['widget types'];
  452. if ($widget_type) {
  453. if (isset($widget_types[$widget_type])) {
  454. return $widget_types[$widget_type];
  455. }
  456. }
  457. else {
  458. return $widget_types;
  459. }
  460. }
  461. /**
  462. * Returns information about field formatters from hook_field_formatter_info().
  463. *
  464. * @param $formatter_type
  465. * (optional) A formatter type name. If omitted, all formatter types will be
  466. * returned.
  467. *
  468. * @return
  469. * Either a single formatter type description, as provided by
  470. * hook_field_formatter_info(), or an array of all existing formatter types,
  471. * keyed by formatter type name.
  472. */
  473. function field_info_formatter_types($formatter_type = NULL) {
  474. $info = _field_info_collate_types();
  475. $formatter_types = $info['formatter types'];
  476. if ($formatter_type) {
  477. if (isset($formatter_types[$formatter_type])) {
  478. return $formatter_types[$formatter_type];
  479. }
  480. }
  481. else {
  482. return $formatter_types;
  483. }
  484. }
  485. /**
  486. * Returns information about field storage from hook_field_storage_info().
  487. *
  488. * @param $storage_type
  489. * (optional) A storage type name. If omitted, all storage types will be
  490. * returned.
  491. *
  492. * @return
  493. * Either a storage type description, as provided by
  494. * hook_field_storage_info(), or an array of all existing storage types,
  495. * keyed by storage type name.
  496. */
  497. function field_info_storage_types($storage_type = NULL) {
  498. $info = _field_info_collate_types();
  499. $storage_types = $info['storage types'];
  500. if ($storage_type) {
  501. if (isset($storage_types[$storage_type])) {
  502. return $storage_types[$storage_type];
  503. }
  504. }
  505. else {
  506. return $storage_types;
  507. }
  508. }
  509. /**
  510. * Returns information about existing bundles.
  511. *
  512. * @param $entity_type
  513. * The type of entity; e.g. 'node' or 'user'.
  514. *
  515. * @return
  516. * An array of bundles for the $entity_type keyed by bundle name,
  517. * or, if no $entity_type was provided, the array of all existing bundles,
  518. * keyed by entity type.
  519. */
  520. function field_info_bundles($entity_type = NULL) {
  521. $info = entity_get_info();
  522. if ($entity_type) {
  523. return isset($info[$entity_type]['bundles']) ? $info[$entity_type]['bundles'] : array();
  524. }
  525. $bundles = array();
  526. foreach ($info as $type => $entity_info) {
  527. $bundles[$type] = $entity_info['bundles'];
  528. }
  529. return $bundles;
  530. }
  531. /**
  532. * Returns all field definitions.
  533. *
  534. * @return
  535. * An array of field definitions, keyed by field name. Each field has an
  536. * additional property, 'bundles', which is an array of all the bundles to
  537. * which this field belongs keyed by entity type.
  538. */
  539. function field_info_fields() {
  540. $fields = array();
  541. $info = _field_info_collate_fields();
  542. foreach ($info['fields'] as $key => $field) {
  543. if (!$field['deleted']) {
  544. $fields[$field['field_name']] = $field;
  545. }
  546. }
  547. return $fields;
  548. }
  549. /**
  550. * Returns data about an individual field, given a field name.
  551. *
  552. * @param $field_name
  553. * The name of the field to retrieve. $field_name can only refer to a
  554. * non-deleted, active field. For deleted fields, use
  555. * field_info_field_by_id(). To retrieve information about inactive fields,
  556. * use field_read_fields().
  557. *
  558. * @return
  559. * The field array, as returned by field_read_fields(), with an
  560. * additional element 'bundles', whose value is an array of all the bundles
  561. * this field belongs to keyed by entity type. NULL if the field was not
  562. * found.
  563. *
  564. * @see field_info_field_by_id()
  565. */
  566. function field_info_field($field_name) {
  567. $info = _field_info_collate_fields();
  568. if (isset($info['field_ids'][$field_name])) {
  569. return $info['fields'][$info['field_ids'][$field_name]];
  570. }
  571. }
  572. /**
  573. * Returns data about an individual field, given a field ID.
  574. *
  575. * @param $field_id
  576. * The id of the field to retrieve. $field_id can refer to a
  577. * deleted field, but not an inactive one.
  578. *
  579. * @return
  580. * The field array, as returned by field_read_fields(), with an
  581. * additional element 'bundles', whose value is an array of all the bundles
  582. * this field belongs to.
  583. *
  584. * @see field_info_field()
  585. */
  586. function field_info_field_by_id($field_id) {
  587. $info = _field_info_collate_fields();
  588. if (isset($info['fields'][$field_id])) {
  589. return $info['fields'][$field_id];
  590. }
  591. }
  592. /**
  593. * Returns the same data as field_info_field_by_id() for every field.
  594. *
  595. * This function is typically used when handling all fields of some entities
  596. * to avoid thousands of calls to field_info_field_by_id().
  597. *
  598. * @return
  599. * An array, each key is a field ID and the values are field arrays as
  600. * returned by field_read_fields(), with an additional element 'bundles',
  601. * whose value is an array of all the bundle this field belongs to.
  602. *
  603. * @see field_info_field()
  604. * @see field_info_field_by_id()
  605. */
  606. function field_info_field_by_ids() {
  607. $info = _field_info_collate_fields();
  608. return $info['fields'];
  609. }
  610. /**
  611. * Retrieves information about field instances.
  612. *
  613. * @param $entity_type
  614. * The entity type for which to return instances.
  615. * @param $bundle_name
  616. * The bundle name for which to return instances.
  617. *
  618. * @return
  619. * If $entity_type is not set, return all instances keyed by entity type and
  620. * bundle name. If $entity_type is set, return all instances for that entity
  621. * type, keyed by bundle name. If $entity_type and $bundle_name are set, return
  622. * all instances for that bundle.
  623. */
  624. function field_info_instances($entity_type = NULL, $bundle_name = NULL) {
  625. $info = _field_info_collate_fields();
  626. if (isset($entity_type) && isset($bundle_name)) {
  627. return isset($info['instances'][$entity_type][$bundle_name]) ? $info['instances'][$entity_type][$bundle_name] : array();
  628. }
  629. elseif (isset($entity_type)) {
  630. return isset($info['instances'][$entity_type]) ? $info['instances'][$entity_type] : array();
  631. }
  632. else {
  633. return $info['instances'];
  634. }
  635. }
  636. /**
  637. * Returns an array of instance data for a specific field and bundle.
  638. *
  639. * @param $entity_type
  640. * The entity type for the instance.
  641. * @param $field_name
  642. * The field name for the instance.
  643. * @param $bundle_name
  644. * The bundle name for the instance.
  645. */
  646. function field_info_instance($entity_type, $field_name, $bundle_name) {
  647. $info = _field_info_collate_fields();
  648. if (isset($info['instances'][$entity_type][$bundle_name][$field_name])) {
  649. return $info['instances'][$entity_type][$bundle_name][$field_name];
  650. }
  651. }
  652. /**
  653. * Returns a list and settings of pseudo-field elements in a given bundle.
  654. *
  655. * If $context is 'form', an array with the following structure:
  656. * @code
  657. * array(
  658. * 'name_of_pseudo_field_component' => array(
  659. * 'label' => The human readable name of the component,
  660. * 'description' => A short description of the component content,
  661. * 'weight' => The weight of the component in edit forms,
  662. * ),
  663. * 'name_of_other_pseudo_field_component' => array(
  664. * // ...
  665. * ),
  666. * );
  667. * @endcode
  668. *
  669. * If $context is 'display', an array with the following structure:
  670. * @code
  671. * array(
  672. * 'name_of_pseudo_field_component' => array(
  673. * 'label' => The human readable name of the component,
  674. * 'description' => A short description of the component content,
  675. * // One entry per view mode, including the 'default' mode:
  676. * 'display' => array(
  677. * 'default' => array(
  678. * 'weight' => The weight of the component in displayed entities in
  679. * this view mode,
  680. * 'visible' => TRUE if the component is visible, FALSE if hidden, in
  681. * displayed entities in this view mode,
  682. * ),
  683. * 'teaser' => array(
  684. * // ...
  685. * ),
  686. * ),
  687. * ),
  688. * 'name_of_other_pseudo_field_component' => array(
  689. * // ...
  690. * ),
  691. * );
  692. * @endcode
  693. *
  694. * @param $entity_type
  695. * The type of entity; e.g. 'node' or 'user'.
  696. * @param $bundle
  697. * The bundle name.
  698. * @param $context
  699. * The context for which the list of pseudo-fields is requested. Either
  700. * 'form' or 'display'.
  701. *
  702. * @return
  703. * The array of pseudo-field elements in the bundle.
  704. */
  705. function field_info_extra_fields($entity_type, $bundle, $context) {
  706. $info = _field_info_collate_fields();
  707. if (isset($info['extra_fields'][$entity_type][$bundle][$context])) {
  708. return $info['extra_fields'][$entity_type][$bundle][$context];
  709. }
  710. return array();
  711. }
  712. /**
  713. * Returns the maximum weight of all the components in an entity.
  714. *
  715. * This includes fields, 'extra_fields', and other components added by
  716. * third-party modules (e.g. field_group).
  717. *
  718. * @param $entity_type
  719. * The type of entity; e.g. 'node' or 'user'.
  720. * @param $bundle
  721. * The bundle name.
  722. * @param $context
  723. * The context for which the maximum weight is requested. Either 'form', or
  724. * the name of a view mode.
  725. * @return
  726. * The maximum weight of the entity's components, or NULL if no components
  727. * were found.
  728. */
  729. function field_info_max_weight($entity_type, $bundle, $context) {
  730. $weights = array();
  731. // Collect weights for fields.
  732. foreach (field_info_instances($entity_type, $bundle) as $instance) {
  733. if ($context == 'form') {
  734. $weights[] = $instance['widget']['weight'];
  735. }
  736. elseif (isset($instance['display'][$context]['weight'])) {
  737. $weights[] = $instance['display'][$context]['weight'];
  738. }
  739. }
  740. // Collect weights for extra fields.
  741. foreach (field_info_extra_fields($entity_type, $bundle, $context) as $extra) {
  742. $weights[] = $extra['weight'];
  743. }
  744. // Let other modules feedback about their own additions.
  745. $weights = array_merge($weights, module_invoke_all('field_info_max_weight', $entity_type, $bundle, $context));
  746. $max_weight = $weights ? max($weights) : NULL;
  747. return $max_weight;
  748. }
  749. /**
  750. * Returns a field type's default settings.
  751. *
  752. * @param $type
  753. * A field type name.
  754. *
  755. * @return
  756. * The field type's default settings, as provided by hook_field_info(), or an
  757. * empty array if type or settings are not defined.
  758. */
  759. function field_info_field_settings($type) {
  760. $info = field_info_field_types($type);
  761. return isset($info['settings']) ? $info['settings'] : array();
  762. }
  763. /**
  764. * Returns a field type's default instance settings.
  765. *
  766. * @param $type
  767. * A field type name.
  768. *
  769. * @return
  770. * The field type's default instance settings, as provided by
  771. * hook_field_info(), or an empty array if type or settings are not defined.
  772. */
  773. function field_info_instance_settings($type) {
  774. $info = field_info_field_types($type);
  775. return isset($info['instance_settings']) ? $info['instance_settings'] : array();
  776. }
  777. /**
  778. * Returns a field widget's default settings.
  779. *
  780. * @param $type
  781. * A widget type name.
  782. *
  783. * @return
  784. * The widget type's default settings, as provided by
  785. * hook_field_widget_info(), or an empty array if type or settings are
  786. * undefined.
  787. */
  788. function field_info_widget_settings($type) {
  789. $info = field_info_widget_types($type);
  790. return isset($info['settings']) ? $info['settings'] : array();
  791. }
  792. /**
  793. * Returns a field formatter's default settings.
  794. *
  795. * @param $type
  796. * A field formatter type name.
  797. *
  798. * @return
  799. * The formatter type's default settings, as provided by
  800. * hook_field_formatter_info(), or an empty array if type or settings are
  801. * undefined.
  802. */
  803. function field_info_formatter_settings($type) {
  804. $info = field_info_formatter_types($type);
  805. return isset($info['settings']) ? $info['settings'] : array();
  806. }
  807. /**
  808. * Returns a field storage type's default settings.
  809. *
  810. * @param $type
  811. * A field storage type name.
  812. *
  813. * @return
  814. * The storage type's default settings, as provided by
  815. * hook_field_storage_info(), or an empty array if type or settings are
  816. * undefined.
  817. */
  818. function field_info_storage_settings($type) {
  819. $info = field_info_storage_types($type);
  820. return isset($info['settings']) ? $info['settings'] : array();
  821. }
  822. /**
  823. * @} End of "defgroup field_info".
  824. */