field.info.inc 26 KB

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