field.info.inc

  1. drupal
    1. 7 modules/field/field.info.inc
    2. 8 core/modules/field/field.info.inc

Field Info API, providing information about available fields and field types.

Functions & methods

NameDescription
field_behaviors_widgetDetermines the behavior of a widget with respect to an operation.
field_info_bundlesReturns information about existing bundles.
field_info_cache_clearClears the field info cache without clearing the field data cache.
field_info_extra_fieldsReturns a list and settings of pseudo-field elements in a given bundle.
field_info_fieldReturns data about an individual field, given a field name.
field_info_fieldsReturns all field definitions.
field_info_field_by_idReturns data about an individual field, given a field ID.
field_info_field_by_idsReturns the same data as field_info_field_by_id() for every field.
field_info_field_settingsReturns a field type's default settings.
field_info_field_typesReturns information about field types from hook_field_info().
field_info_formatter_settingsReturns a field formatter's default settings.
field_info_formatter_typesReturns information about field formatters from hook_field_formatter_info().
field_info_instanceReturns an array of instance data for a specific field and bundle.
field_info_instancesRetrieves information about field instances.
field_info_instance_settingsReturns a field type's default instance settings.
field_info_max_weightReturns the maximum weight of all the components in an entity.
field_info_storage_settingsReturns a field storage type's default settings.
field_info_storage_typesReturns information about field storage from hook_field_storage_info().
field_info_widget_settingsReturns a field widget's default settings.
field_info_widget_typesReturns information about field widgets from hook_field_widget_info().
_field_info_collate_fieldsCollates all information on existing fields and instances.
_field_info_collate_typesCollates all information on field types, widget types and related structures.
_field_info_prepare_extra_fieldsPrepares 'extra fields' for the current run-time context.
_field_info_prepare_fieldPrepares a field definition for the current run-time context.
_field_info_prepare_instancePrepares an instance definition for the current run-time context.
_field_info_prepare_instance_displayAdapts display specifications to the current run-time context.
_field_info_prepare_instance_widgetPrepares widget specifications for the current run-time context.

File

modules/field/field.info.inc
View source
  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.
  562. *
  563. * @see field_info_field_by_id()
  564. */
  565. function field_info_field($field_name) {
  566. $info = _field_info_collate_fields();
  567. if (isset($info['field_ids'][$field_name])) {
  568. return $info['fields'][$info['field_ids'][$field_name]];
  569. }
  570. }
  571. /**
  572. * Returns data about an individual field, given a field ID.
  573. *
  574. * @param $field_id
  575. * The id of the field to retrieve. $field_id can refer to a
  576. * deleted field, but not an inactive one.
  577. *
  578. * @return
  579. * The field array, as returned by field_read_fields(), with an
  580. * additional element 'bundles', whose value is an array of all the bundles
  581. * this field belongs to.
  582. *
  583. * @see field_info_field()
  584. */
  585. function field_info_field_by_id($field_id) {
  586. $info = _field_info_collate_fields();
  587. if (isset($info['fields'][$field_id])) {
  588. return $info['fields'][$field_id];
  589. }
  590. }
  591. /**
  592. * Returns the same data as field_info_field_by_id() for every field.
  593. *
  594. * This function is typically used when handling all fields of some entities
  595. * to avoid thousands of calls to field_info_field_by_id().
  596. *
  597. * @return
  598. * An array, each key is a field ID and the values are field arrays as
  599. * returned by field_read_fields(), with an additional element 'bundles',
  600. * whose value is an array of all the bundle this field belongs to.
  601. *
  602. * @see field_info_field()
  603. * @see field_info_field_by_id()
  604. */
  605. function field_info_field_by_ids() {
  606. $info = _field_info_collate_fields();
  607. return $info['fields'];
  608. }
  609. /**
  610. * Retrieves information about field instances.
  611. *
  612. * @param $entity_type
  613. * The entity type for which to return instances.
  614. * @param $bundle_name
  615. * The bundle name for which to return instances.
  616. *
  617. * @return
  618. * If $entity_type is not set, return all instances keyed by entity type and
  619. * bundle name. If $entity_type is set, return all instances for that entity
  620. * type, keyed by bundle name. If $entity_type and $bundle_name are set, return
  621. * all instances for that bundle.
  622. */
  623. function field_info_instances($entity_type = NULL, $bundle_name = NULL) {
  624. $info = _field_info_collate_fields();
  625. if (isset($entity_type) && isset($bundle_name)) {
  626. return isset($info['instances'][$entity_type][$bundle_name]) ? $info['instances'][$entity_type][$bundle_name] : array();
  627. }
  628. elseif (isset($entity_type)) {
  629. return isset($info['instances'][$entity_type]) ? $info['instances'][$entity_type] : array();
  630. }
  631. else {
  632. return $info['instances'];
  633. }
  634. }
  635. /**
  636. * Returns an array of instance data for a specific field and bundle.
  637. *
  638. * @param $entity_type
  639. * The entity type for the instance.
  640. * @param $field_name
  641. * The field name for the instance.
  642. * @param $bundle_name
  643. * The bundle name for the instance.
  644. */
  645. function field_info_instance($entity_type, $field_name, $bundle_name) {
  646. $info = _field_info_collate_fields();
  647. if (isset($info['instances'][$entity_type][$bundle_name][$field_name])) {
  648. return $info['instances'][$entity_type][$bundle_name][$field_name];
  649. }
  650. }
  651. /**
  652. * Returns a list and settings of pseudo-field elements in a given bundle.
  653. *
  654. * If $context is 'form', an array with the following structure:
  655. * @code
  656. * array(
  657. * 'name_of_pseudo_field_component' => array(
  658. * 'label' => The human readable name of the component,
  659. * 'description' => A short description of the component content,
  660. * 'weight' => The weight of the component in edit forms,
  661. * ),
  662. * 'name_of_other_pseudo_field_component' => array(
  663. * // ...
  664. * ),
  665. * );
  666. * @endcode
  667. *
  668. * If $context is 'display', an array with the following structure:
  669. * @code
  670. * array(
  671. * 'name_of_pseudo_field_component' => array(
  672. * 'label' => The human readable name of the component,
  673. * 'description' => A short description of the component content,
  674. * // One entry per view mode, including the 'default' mode:
  675. * 'display' => array(
  676. * 'default' => array(
  677. * 'weight' => The weight of the component in displayed entities in
  678. * this view mode,
  679. * 'visible' => TRUE if the component is visible, FALSE if hidden, in
  680. * displayed entities in this view mode,
  681. * ),
  682. * 'teaser' => array(
  683. * // ...
  684. * ),
  685. * ),
  686. * ),
  687. * 'name_of_other_pseudo_field_component' => array(
  688. * // ...
  689. * ),
  690. * );
  691. * @endcode
  692. *
  693. * @param $entity_type
  694. * The type of entity; e.g. 'node' or 'user'.
  695. * @param $bundle
  696. * The bundle name.
  697. * @param $context
  698. * The context for which the list of pseudo-fields is requested. Either
  699. * 'form' or 'display'.
  700. *
  701. * @return
  702. * The array of pseudo-field elements in the bundle.
  703. */
  704. function field_info_extra_fields($entity_type, $bundle, $context) {
  705. $info = _field_info_collate_fields();
  706. if (isset($info['extra_fields'][$entity_type][$bundle][$context])) {
  707. return $info['extra_fields'][$entity_type][$bundle][$context];
  708. }
  709. return array();
  710. }
  711. /**
  712. * Returns the maximum weight of all the components in an entity.
  713. *
  714. * This includes fields, 'extra_fields', and other components added by
  715. * third-party modules (e.g. field_group).
  716. *
  717. * @param $entity_type
  718. * The type of entity; e.g. 'node' or 'user'.
  719. * @param $bundle
  720. * The bundle name.
  721. * @param $context
  722. * The context for which the maximum weight is requested. Either 'form', or
  723. * the name of a view mode.
  724. * @return
  725. * The maximum weight of the entity's components, or NULL if no components
  726. * were found.
  727. */
  728. function field_info_max_weight($entity_type, $bundle, $context) {
  729. $weights = array();
  730. // Collect weights for fields.
  731. foreach (field_info_instances($entity_type, $bundle) as $instance) {
  732. if ($context == 'form') {
  733. $weights[] = $instance['widget']['weight'];
  734. }
  735. elseif (isset($instance['display'][$context]['weight'])) {
  736. $weights[] = $instance['display'][$context]['weight'];
  737. }
  738. }
  739. // Collect weights for extra fields.
  740. foreach (field_info_extra_fields($entity_type, $bundle, $context) as $extra) {
  741. $weights[] = $extra['weight'];
  742. }
  743. // Let other modules feedback about their own additions.
  744. $weights = array_merge($weights, module_invoke_all('field_info_max_weight', $entity_type, $bundle, $context));
  745. $max_weight = $weights ? max($weights) : NULL;
  746. return $max_weight;
  747. }
  748. /**
  749. * Returns a field type's default settings.
  750. *
  751. * @param $type
  752. * A field type name.
  753. *
  754. * @return
  755. * The field type's default settings, as provided by hook_field_info(), or an
  756. * empty array if type or settings are not defined.
  757. */
  758. function field_info_field_settings($type) {
  759. $info = field_info_field_types($type);
  760. return isset($info['settings']) ? $info['settings'] : array();
  761. }
  762. /**
  763. * Returns a field type's default instance settings.
  764. *
  765. * @param $type
  766. * A field type name.
  767. *
  768. * @return
  769. * The field type's default instance settings, as provided by
  770. * hook_field_info(), or an empty array if type or settings are not defined.
  771. */
  772. function field_info_instance_settings($type) {
  773. $info = field_info_field_types($type);
  774. return isset($info['instance_settings']) ? $info['instance_settings'] : array();
  775. }
  776. /**
  777. * Returns a field widget's default settings.
  778. *
  779. * @param $type
  780. * A widget type name.
  781. *
  782. * @return
  783. * The widget type's default settings, as provided by
  784. * hook_field_widget_info(), or an empty array if type or settings are
  785. * undefined.
  786. */
  787. function field_info_widget_settings($type) {
  788. $info = field_info_widget_types($type);
  789. return isset($info['settings']) ? $info['settings'] : array();
  790. }
  791. /**
  792. * Returns a field formatter's default settings.
  793. *
  794. * @param $type
  795. * A field formatter type name.
  796. *
  797. * @return
  798. * The formatter type's default settings, as provided by
  799. * hook_field_formatter_info(), or an empty array if type or settings are
  800. * undefined.
  801. */
  802. function field_info_formatter_settings($type) {
  803. $info = field_info_formatter_types($type);
  804. return isset($info['settings']) ? $info['settings'] : array();
  805. }
  806. /**
  807. * Returns a field storage type's default settings.
  808. *
  809. * @param $type
  810. * A field storage type name.
  811. *
  812. * @return
  813. * The storage type's default settings, as provided by
  814. * hook_field_storage_info(), or an empty array if type or settings are
  815. * undefined.
  816. */
  817. function field_info_storage_settings($type) {
  818. $info = field_info_storage_types($type);
  819. return isset($info['settings']) ? $info['settings'] : array();
  820. }
  821. /**
  822. * @} End of "defgroup field_info"
  823. */
Login or register to post comments