field.info.inc 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  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. cache_set("field_info_types:$langcode", $info, 'cache_field');
  204. }
  205. }
  206. return $info;
  207. }
  208. /**
  209. * Prepares a field definition for the current run-time context.
  210. *
  211. * The functionality has moved to the FieldInfo class. This function is kept as
  212. * a backwards-compatibility layer. See http://drupal.org/node/1915646.
  213. *
  214. * @see FieldInfo::prepareField()
  215. */
  216. function _field_info_prepare_field($field) {
  217. $cache = _field_info_field_cache();
  218. return $cache->prepareField($field);
  219. }
  220. /**
  221. * Prepares an instance definition for the current run-time context.
  222. *
  223. * The functionality has moved to the FieldInfo class. This function is kept as
  224. * a backwards-compatibility layer. See http://drupal.org/node/1915646.
  225. *
  226. * @see FieldInfo::prepareInstance()
  227. */
  228. function _field_info_prepare_instance($instance, $field) {
  229. $cache = _field_info_field_cache();
  230. return $cache->prepareInstance($instance, $field['type']);
  231. }
  232. /**
  233. * Adapts display specifications to the current run-time context.
  234. *
  235. * The functionality has moved to the FieldInfo class. This function is kept as
  236. * a backwards-compatibility layer. See http://drupal.org/node/1915646.
  237. *
  238. * @see FieldInfo::prepareInstanceDisplay()
  239. */
  240. function _field_info_prepare_instance_display($field, $display) {
  241. $cache = _field_info_field_cache();
  242. return $cache->prepareInstanceDisplay($display, $field['type']);
  243. }
  244. /**
  245. * Prepares widget specifications for the current run-time context.
  246. *
  247. * The functionality has moved to the FieldInfo class. This function is kept as
  248. * a backwards-compatibility layer. See http://drupal.org/node/1915646.
  249. *
  250. * @see FieldInfo::prepareInstanceWidget()
  251. */
  252. function _field_info_prepare_instance_widget($field, $widget) {
  253. $cache = _field_info_field_cache();
  254. return $cache->prepareInstanceWidget($widget, $field['type']);
  255. }
  256. /**
  257. * Prepares 'extra fields' for the current run-time context.
  258. *
  259. * The functionality has moved to the FieldInfo class. This function is kept as
  260. * a backwards-compatibility layer. See http://drupal.org/node/1915646.
  261. *
  262. * @see FieldInfo::prepareExtraFields()
  263. */
  264. function _field_info_prepare_extra_fields($extra_fields, $entity_type, $bundle) {
  265. $cache = _field_info_field_cache();
  266. return $cache->prepareExtraFields($extra_fields, $entity_type, $bundle);
  267. }
  268. /**
  269. * Determines the behavior of a widget with respect to an operation.
  270. *
  271. * @param $op
  272. * The name of the operation. Currently supported: 'default value',
  273. * 'multiple values'.
  274. * @param $instance
  275. * The field instance array.
  276. *
  277. * @return
  278. * One of these values:
  279. * - FIELD_BEHAVIOR_NONE: Do nothing for this operation.
  280. * - FIELD_BEHAVIOR_CUSTOM: Use the widget's callback function.
  281. * - FIELD_BEHAVIOR_DEFAULT: Use field.module default behavior.
  282. */
  283. function field_behaviors_widget($op, $instance) {
  284. $info = field_info_widget_types($instance['widget']['type']);
  285. return isset($info['behaviors'][$op]) ? $info['behaviors'][$op] : FIELD_BEHAVIOR_DEFAULT;
  286. }
  287. /**
  288. * Returns information about field types from hook_field_info().
  289. *
  290. * @param $field_type
  291. * (optional) A field type name. If omitted, all field types will be
  292. * returned.
  293. *
  294. * @return
  295. * Either a field type description, as provided by hook_field_info(), or an
  296. * array of all existing field types, keyed by field type name.
  297. */
  298. function field_info_field_types($field_type = NULL) {
  299. $info = _field_info_collate_types();
  300. $field_types = $info['field types'];
  301. if ($field_type) {
  302. if (isset($field_types[$field_type])) {
  303. return $field_types[$field_type];
  304. }
  305. }
  306. else {
  307. return $field_types;
  308. }
  309. }
  310. /**
  311. * Returns information about field widgets from hook_field_widget_info().
  312. *
  313. * @param $widget_type
  314. * (optional) A widget type name. If omitted, all widget types will be
  315. * returned.
  316. *
  317. * @return
  318. * Either a single widget type description, as provided by
  319. * hook_field_widget_info(), or an array of all existing widget types, keyed
  320. * by widget type name.
  321. */
  322. function field_info_widget_types($widget_type = NULL) {
  323. $info = _field_info_collate_types();
  324. $widget_types = $info['widget types'];
  325. if ($widget_type) {
  326. if (isset($widget_types[$widget_type])) {
  327. return $widget_types[$widget_type];
  328. }
  329. }
  330. else {
  331. return $widget_types;
  332. }
  333. }
  334. /**
  335. * Returns information about field formatters from hook_field_formatter_info().
  336. *
  337. * @param $formatter_type
  338. * (optional) A formatter type name. If omitted, all formatter types will be
  339. * returned.
  340. *
  341. * @return
  342. * Either a single formatter type description, as provided by
  343. * hook_field_formatter_info(), or an array of all existing formatter types,
  344. * keyed by formatter type name.
  345. */
  346. function field_info_formatter_types($formatter_type = NULL) {
  347. $info = _field_info_collate_types();
  348. $formatter_types = $info['formatter types'];
  349. if ($formatter_type) {
  350. if (isset($formatter_types[$formatter_type])) {
  351. return $formatter_types[$formatter_type];
  352. }
  353. }
  354. else {
  355. return $formatter_types;
  356. }
  357. }
  358. /**
  359. * Returns information about field storage from hook_field_storage_info().
  360. *
  361. * @param $storage_type
  362. * (optional) A storage type name. If omitted, all storage types will be
  363. * returned.
  364. *
  365. * @return
  366. * Either a storage type description, as provided by
  367. * hook_field_storage_info(), or an array of all existing storage types,
  368. * keyed by storage type name.
  369. */
  370. function field_info_storage_types($storage_type = NULL) {
  371. $info = _field_info_collate_types();
  372. $storage_types = $info['storage types'];
  373. if ($storage_type) {
  374. if (isset($storage_types[$storage_type])) {
  375. return $storage_types[$storage_type];
  376. }
  377. }
  378. else {
  379. return $storage_types;
  380. }
  381. }
  382. /**
  383. * Returns information about existing bundles.
  384. *
  385. * @param $entity_type
  386. * The type of entity; e.g. 'node' or 'user'.
  387. *
  388. * @return
  389. * An array of bundles for the $entity_type keyed by bundle name,
  390. * or, if no $entity_type was provided, the array of all existing bundles,
  391. * keyed by entity type.
  392. */
  393. function field_info_bundles($entity_type = NULL) {
  394. $info = entity_get_info();
  395. if ($entity_type) {
  396. return isset($info[$entity_type]['bundles']) ? $info[$entity_type]['bundles'] : array();
  397. }
  398. $bundles = array();
  399. foreach ($info as $type => $entity_info) {
  400. $bundles[$type] = $entity_info['bundles'];
  401. }
  402. return $bundles;
  403. }
  404. /**
  405. * Returns a lightweight map of fields across bundles.
  406. *
  407. * The function only returns active, non deleted fields.
  408. *
  409. * @return
  410. * An array keyed by field name. Each value is an array with two entries:
  411. * - type: The field type.
  412. * - bundles: The bundles in which the field appears, as an array with entity
  413. * types as keys and the array of bundle names as values.
  414. */
  415. function field_info_field_map() {
  416. $cache = _field_info_field_cache();
  417. return $cache->getFieldMap();
  418. }
  419. /**
  420. * Returns all field definitions.
  421. *
  422. * Use of this function should be avoided when possible, since it loads and
  423. * statically caches a potentially large array of information. Use
  424. * field_info_field_map() instead.
  425. *
  426. * When iterating over the fields present in a given bundle after a call to
  427. * field_info_instances($entity_type, $bundle), it is recommended to use
  428. * field_info_field() on each individual field instead.
  429. *
  430. * @return
  431. * An array of field definitions, keyed by field name. Each field has an
  432. * additional property, 'bundles', which is an array of all the bundles to
  433. * which this field belongs keyed by entity type.
  434. *
  435. * @see field_info_field_map()
  436. */
  437. function field_info_fields() {
  438. $cache = _field_info_field_cache();
  439. $info = $cache->getFields();
  440. $fields = array();
  441. foreach ($info as $key => $field) {
  442. if (!$field['deleted']) {
  443. $fields[$field['field_name']] = $field;
  444. }
  445. }
  446. return $fields;
  447. }
  448. /**
  449. * Returns data about an individual field, given a field name.
  450. *
  451. * @param $field_name
  452. * The name of the field to retrieve. $field_name can only refer to a
  453. * non-deleted, active field. For deleted fields, use
  454. * field_info_field_by_id(). To retrieve information about inactive fields,
  455. * use field_read_fields().
  456. *
  457. * @return
  458. * The field array, as returned by field_read_fields(), with an
  459. * additional element 'bundles', whose value is an array of all the bundles
  460. * this field belongs to keyed by entity type. NULL if the field was not
  461. * found.
  462. *
  463. * @see field_info_field_by_id()
  464. */
  465. function field_info_field($field_name) {
  466. $cache = _field_info_field_cache();
  467. return $cache->getField($field_name);
  468. }
  469. /**
  470. * Returns data about an individual field, given a field ID.
  471. *
  472. * @param $field_id
  473. * The id of the field to retrieve. $field_id can refer to a
  474. * deleted field, but not an inactive one.
  475. *
  476. * @return
  477. * The field array, as returned by field_read_fields(), with an
  478. * additional element 'bundles', whose value is an array of all the bundles
  479. * this field belongs to.
  480. *
  481. * @see field_info_field()
  482. */
  483. function field_info_field_by_id($field_id) {
  484. $cache = _field_info_field_cache();
  485. return $cache->getFieldById($field_id);
  486. }
  487. /**
  488. * Returns the same data as field_info_field_by_id() for every field.
  489. *
  490. * Use of this function should be avoided when possible, since it loads and
  491. * statically caches a potentially large array of information.
  492. *
  493. * When iterating over the fields present in a given bundle after a call to
  494. * field_info_instances($entity_type, $bundle), it is recommended to use
  495. * field_info_field() on each individual field instead.
  496. *
  497. * @return
  498. * An array, each key is a field ID and the values are field arrays as
  499. * returned by field_read_fields(), with an additional element 'bundles',
  500. * whose value is an array of all the bundle this field belongs to.
  501. *
  502. * @see field_info_field()
  503. * @see field_info_field_by_id()
  504. */
  505. function field_info_field_by_ids() {
  506. $cache = _field_info_field_cache();
  507. return $cache->getFields();
  508. }
  509. /**
  510. * Retrieves information about field instances.
  511. *
  512. * Use of this function to retrieve instances across separate bundles (i.e.
  513. * when the $bundle parameter is NULL) should be avoided when possible, since
  514. * it loads and statically caches a potentially large array of information. Use
  515. * field_info_field_map() instead.
  516. *
  517. * When retrieving the instances of a specific bundle (i.e. when both
  518. * $entity_type and $bundle_name are provided), the function also populates a
  519. * static cache with the corresponding field definitions, allowing fast
  520. * retrieval of field_info_field() later in the request.
  521. *
  522. * @param $entity_type
  523. * (optional) The entity type for which to return instances.
  524. * @param $bundle_name
  525. * (optional) The bundle name for which to return instances. If $entity_type
  526. * is NULL, the $bundle_name parameter is ignored.
  527. *
  528. * @return
  529. * If $entity_type is not set, return all instances keyed by entity type and
  530. * bundle name. If $entity_type is set, return all instances for that entity
  531. * type, keyed by bundle name. If $entity_type and $bundle_name are set, return
  532. * all instances for that bundle.
  533. *
  534. * @see field_info_field_map()
  535. */
  536. function field_info_instances($entity_type = NULL, $bundle_name = NULL) {
  537. $cache = _field_info_field_cache();
  538. if (!isset($entity_type)) {
  539. return $cache->getInstances();
  540. }
  541. if (!isset($bundle_name)) {
  542. return $cache->getInstances($entity_type);
  543. }
  544. return $cache->getBundleInstances($entity_type, $bundle_name);
  545. }
  546. /**
  547. * Returns an array of instance data for a specific field and bundle.
  548. *
  549. * The function populates a static cache with all fields and instances used in
  550. * the bundle, allowing fast retrieval of field_info_field() or
  551. * field_info_instance() later in the request.
  552. *
  553. * @param $entity_type
  554. * The entity type for the instance.
  555. * @param $field_name
  556. * The field name for the instance.
  557. * @param $bundle_name
  558. * The bundle name for the instance.
  559. *
  560. * @return
  561. * An associative array of instance data for the specific field and bundle;
  562. * NULL if the instance does not exist.
  563. */
  564. function field_info_instance($entity_type, $field_name, $bundle_name) {
  565. $cache = _field_info_field_cache();
  566. $info = $cache->getBundleInstances($entity_type, $bundle_name);
  567. if (isset($info[$field_name])) {
  568. return $info[$field_name];
  569. }
  570. }
  571. /**
  572. * Returns a list and settings of pseudo-field elements in a given bundle.
  573. *
  574. * If $context is 'form', an array with the following structure:
  575. * @code
  576. * array(
  577. * 'name_of_pseudo_field_component' => array(
  578. * 'label' => The human readable name of the component,
  579. * 'description' => A short description of the component content,
  580. * 'weight' => The weight of the component in edit forms,
  581. * ),
  582. * 'name_of_other_pseudo_field_component' => array(
  583. * // ...
  584. * ),
  585. * );
  586. * @endcode
  587. *
  588. * If $context is 'display', an array with the following structure:
  589. * @code
  590. * array(
  591. * 'name_of_pseudo_field_component' => array(
  592. * 'label' => The human readable name of the component,
  593. * 'description' => A short description of the component content,
  594. * // One entry per view mode, including the 'default' mode:
  595. * 'display' => array(
  596. * 'default' => array(
  597. * 'weight' => The weight of the component in displayed entities in
  598. * this view mode,
  599. * 'visible' => TRUE if the component is visible, FALSE if hidden, in
  600. * displayed entities in this view mode,
  601. * ),
  602. * 'teaser' => array(
  603. * // ...
  604. * ),
  605. * ),
  606. * ),
  607. * 'name_of_other_pseudo_field_component' => array(
  608. * // ...
  609. * ),
  610. * );
  611. * @endcode
  612. *
  613. * @param $entity_type
  614. * The type of entity; e.g. 'node' or 'user'.
  615. * @param $bundle
  616. * The bundle name.
  617. * @param $context
  618. * The context for which the list of pseudo-fields is requested. Either
  619. * 'form' or 'display'.
  620. *
  621. * @return
  622. * The array of pseudo-field elements in the bundle.
  623. */
  624. function field_info_extra_fields($entity_type, $bundle, $context) {
  625. $cache = _field_info_field_cache();
  626. $info = $cache->getBundleExtraFields($entity_type, $bundle);
  627. return isset($info[$context]) ? $info[$context] : array();
  628. }
  629. /**
  630. * Returns the maximum weight of all the components in an entity.
  631. *
  632. * This includes fields, 'extra_fields', and other components added by
  633. * third-party modules (e.g. field_group).
  634. *
  635. * @param $entity_type
  636. * The type of entity; e.g. 'node' or 'user'.
  637. * @param $bundle
  638. * The bundle name.
  639. * @param $context
  640. * The context for which the maximum weight is requested. Either 'form', or
  641. * the name of a view mode.
  642. * @return
  643. * The maximum weight of the entity's components, or NULL if no components
  644. * were found.
  645. */
  646. function field_info_max_weight($entity_type, $bundle, $context) {
  647. $weights = array();
  648. // Collect weights for fields.
  649. foreach (field_info_instances($entity_type, $bundle) as $instance) {
  650. if ($context == 'form') {
  651. $weights[] = $instance['widget']['weight'];
  652. }
  653. elseif (isset($instance['display'][$context]['weight'])) {
  654. $weights[] = $instance['display'][$context]['weight'];
  655. }
  656. }
  657. // Collect weights for extra fields.
  658. foreach (field_info_extra_fields($entity_type, $bundle, $context) as $extra) {
  659. $weights[] = $extra['weight'];
  660. }
  661. // Let other modules feedback about their own additions.
  662. $weights = array_merge($weights, module_invoke_all('field_info_max_weight', $entity_type, $bundle, $context));
  663. $max_weight = $weights ? max($weights) : NULL;
  664. return $max_weight;
  665. }
  666. /**
  667. * Returns a field type's default settings.
  668. *
  669. * @param $type
  670. * A field type name.
  671. *
  672. * @return
  673. * The field type's default settings, as provided by hook_field_info(), or an
  674. * empty array if type or settings are not defined.
  675. */
  676. function field_info_field_settings($type) {
  677. $info = field_info_field_types($type);
  678. return isset($info['settings']) ? $info['settings'] : array();
  679. }
  680. /**
  681. * Returns a field type's default instance settings.
  682. *
  683. * @param $type
  684. * A field type name.
  685. *
  686. * @return
  687. * The field type's default instance settings, as provided by
  688. * hook_field_info(), or an empty array if type or settings are not defined.
  689. */
  690. function field_info_instance_settings($type) {
  691. $info = field_info_field_types($type);
  692. return isset($info['instance_settings']) ? $info['instance_settings'] : array();
  693. }
  694. /**
  695. * Returns a field widget's default settings.
  696. *
  697. * @param $type
  698. * A widget type name.
  699. *
  700. * @return
  701. * The widget type's default settings, as provided by
  702. * hook_field_widget_info(), or an empty array if type or settings are
  703. * undefined.
  704. */
  705. function field_info_widget_settings($type) {
  706. $info = field_info_widget_types($type);
  707. return isset($info['settings']) ? $info['settings'] : array();
  708. }
  709. /**
  710. * Returns a field formatter's default settings.
  711. *
  712. * @param $type
  713. * A field formatter type name.
  714. *
  715. * @return
  716. * The formatter type's default settings, as provided by
  717. * hook_field_formatter_info(), or an empty array if type or settings are
  718. * undefined.
  719. */
  720. function field_info_formatter_settings($type) {
  721. $info = field_info_formatter_types($type);
  722. return isset($info['settings']) ? $info['settings'] : array();
  723. }
  724. /**
  725. * Returns a field storage type's default settings.
  726. *
  727. * @param $type
  728. * A field storage type name.
  729. *
  730. * @return
  731. * The storage type's default settings, as provided by
  732. * hook_field_storage_info(), or an empty array if type or settings are
  733. * undefined.
  734. */
  735. function field_info_storage_settings($type) {
  736. $info = field_info_storage_types($type);
  737. return isset($info['settings']) ? $info['settings'] : array();
  738. }
  739. /**
  740. * @} End of "defgroup field_info".
  741. */