field.info.inc 29 KB

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