field.info.class.inc 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. <?php
  2. /*
  3. * @file
  4. * Definition of the FieldInfo class.
  5. */
  6. /**
  7. * Provides field and instance definitions for the current runtime environment.
  8. *
  9. * A FieldInfo object is created and statically persisted through the request
  10. * by the _field_info_field_cache() function. The object properties act as a
  11. * "static cache" of fields and instances definitions.
  12. *
  13. * The preferred way to access definitions is through the getBundleInstances()
  14. * method, which keeps cache entries per bundle, storing both fields and
  15. * instances for a given bundle. Fields used in multiple bundles are duplicated
  16. * in several cache entries, and are merged into a single list in the memory
  17. * cache. Cache entries are loaded for bundles as a whole, optimizing memory
  18. * and CPU usage for the most common pattern of iterating over all instances of
  19. * a bundle rather than accessing a single instance.
  20. *
  21. * The getFields() and getInstances() methods, which return all existing field
  22. * and instance definitions, are kept mainly for backwards compatibility, and
  23. * should be avoided when possible, since they load and persist in memory a
  24. * potentially large array of information. In many cases, the lightweight
  25. * getFieldMap() method should be preferred.
  26. */
  27. class FieldInfo {
  28. /**
  29. * Lightweight map of fields across entity types and bundles.
  30. *
  31. * @var array
  32. */
  33. protected $fieldMap;
  34. /**
  35. * List of $field structures keyed by ID. Includes deleted fields.
  36. *
  37. * @var array
  38. */
  39. protected $fieldsById = array();
  40. /**
  41. * Mapping of field names to the ID of the corresponding non-deleted field.
  42. *
  43. * @var array
  44. */
  45. protected $fieldIdsByName = array();
  46. /**
  47. * Whether $fieldsById contains all field definitions or a subset.
  48. *
  49. * @var bool
  50. */
  51. protected $loadedAllFields = FALSE;
  52. /**
  53. * Separately tracks requested field names or IDs that do not exist.
  54. *
  55. * @var array
  56. */
  57. protected $unknownFields = array();
  58. /**
  59. * Instance definitions by bundle.
  60. *
  61. * @var array
  62. */
  63. protected $bundleInstances = array();
  64. /**
  65. * Whether $bundleInstances contains all instances definitions or a subset.
  66. *
  67. * @var bool
  68. */
  69. protected $loadedAllInstances = FALSE;
  70. /**
  71. * Separately tracks requested bundles that are empty (or do not exist).
  72. *
  73. * @var array
  74. */
  75. protected $emptyBundles = array();
  76. /**
  77. * Extra fields by bundle.
  78. *
  79. * @var array
  80. */
  81. protected $bundleExtraFields = array();
  82. /**
  83. * Clears the "static" and persistent caches.
  84. */
  85. public function flush() {
  86. $this->fieldMap = NULL;
  87. $this->fieldsById = array();
  88. $this->fieldIdsByName = array();
  89. $this->loadedAllFields = FALSE;
  90. $this->unknownFields = array();
  91. $this->bundleInstances = array();
  92. $this->loadedAllInstances = FALSE;
  93. $this->emptyBundles = array();
  94. $this->bundleExtraFields = array();
  95. cache_clear_all('field_info:', 'cache_field', TRUE);
  96. }
  97. /**
  98. * Collects a lightweight map of fields across bundles.
  99. *
  100. * @return
  101. * An array keyed by field name. Each value is an array with two entries:
  102. * - type: The field type.
  103. * - bundles: The bundles in which the field appears, as an array with
  104. * entity types as keys and the array of bundle names as values.
  105. */
  106. public function getFieldMap() {
  107. // Read from the "static" cache.
  108. if ($this->fieldMap !== NULL) {
  109. return $this->fieldMap;
  110. }
  111. // Read from persistent cache.
  112. if ($cached = cache_get('field_info:field_map', 'cache_field')) {
  113. $map = $cached->data;
  114. // Save in "static" cache.
  115. $this->fieldMap = $map;
  116. return $map;
  117. }
  118. $map = array();
  119. $query = db_query('SELECT fc.type, fci.field_name, fci.entity_type, fci.bundle FROM {field_config_instance} fci INNER JOIN {field_config} fc ON fc.id = fci.field_id WHERE fc.active = 1 AND fc.storage_active = 1 AND fc.deleted = 0 AND fci.deleted = 0');
  120. foreach ($query as $row) {
  121. $map[$row->field_name]['bundles'][$row->entity_type][] = $row->bundle;
  122. $map[$row->field_name]['type'] = $row->type;
  123. }
  124. // Save in "static" and persistent caches.
  125. $this->fieldMap = $map;
  126. cache_set('field_info:field_map', $map, 'cache_field');
  127. return $map;
  128. }
  129. /**
  130. * Returns all active fields, including deleted ones.
  131. *
  132. * @return
  133. * An array of field definitions, keyed by field ID.
  134. */
  135. public function getFields() {
  136. // Read from the "static" cache.
  137. if ($this->loadedAllFields) {
  138. return $this->fieldsById;
  139. }
  140. // Read from persistent cache.
  141. if ($cached = cache_get('field_info:fields', 'cache_field')) {
  142. $this->fieldsById = $cached->data;
  143. }
  144. else {
  145. // Collect and prepare fields.
  146. foreach (field_read_fields(array(), array('include_deleted' => TRUE)) as $field) {
  147. $this->fieldsById[$field['id']] = $this->prepareField($field);
  148. }
  149. // Store in persistent cache.
  150. cache_set('field_info:fields', $this->fieldsById, 'cache_field');
  151. }
  152. // Fill the name/ID map.
  153. foreach ($this->fieldsById as $field) {
  154. if (!$field['deleted']) {
  155. $this->fieldIdsByName[$field['field_name']] = $field['id'];
  156. }
  157. }
  158. $this->loadedAllFields = TRUE;
  159. return $this->fieldsById;
  160. }
  161. /**
  162. * Retrieves all active, non-deleted instances definitions.
  163. *
  164. * @param $entity_type
  165. * (optional) The entity type.
  166. *
  167. * @return
  168. * If $entity_type is not set, all instances keyed by entity type and bundle
  169. * name. If $entity_type is set, all instances for that entity type, keyed
  170. * by bundle name.
  171. */
  172. public function getInstances($entity_type = NULL) {
  173. // If the full list is not present in "static" cache yet.
  174. if (!$this->loadedAllInstances) {
  175. // Read from persistent cache.
  176. if ($cached = cache_get('field_info:instances', 'cache_field')) {
  177. $this->bundleInstances = $cached->data;
  178. }
  179. else {
  180. // Collect and prepare instances.
  181. // We also need to populate the static field cache, since it will not
  182. // be set by subsequent getBundleInstances() calls.
  183. $this->getFields();
  184. // Initialize empty arrays for all existing entity types and bundles.
  185. // This is not strictly needed, but is done to preserve the behavior of
  186. // field_info_instances() before http://drupal.org/node/1915646.
  187. foreach (field_info_bundles() as $existing_entity_type => $bundles) {
  188. foreach ($bundles as $bundle => $bundle_info) {
  189. $this->bundleInstances[$existing_entity_type][$bundle] = array();
  190. }
  191. }
  192. foreach (field_read_instances() as $instance) {
  193. $field = $this->getField($instance['field_name']);
  194. $instance = $this->prepareInstance($instance, $field['type']);
  195. $this->bundleInstances[$instance['entity_type']][$instance['bundle']][$instance['field_name']] = $instance;
  196. }
  197. // Store in persistent cache.
  198. cache_set('field_info:instances', $this->bundleInstances, 'cache_field');
  199. }
  200. $this->loadedAllInstances = TRUE;
  201. }
  202. if (isset($entity_type)) {
  203. return isset($this->bundleInstances[$entity_type]) ? $this->bundleInstances[$entity_type] : array();
  204. }
  205. else {
  206. return $this->bundleInstances;
  207. }
  208. }
  209. /**
  210. * Returns a field definition from a field name.
  211. *
  212. * This method only retrieves active, non-deleted fields.
  213. *
  214. * @param $field_name
  215. * The field name.
  216. *
  217. * @return
  218. * The field definition, or NULL if no field was found.
  219. */
  220. public function getField($field_name) {
  221. // Read from the "static" cache.
  222. if (isset($this->fieldIdsByName[$field_name])) {
  223. $field_id = $this->fieldIdsByName[$field_name];
  224. return $this->fieldsById[$field_id];
  225. }
  226. if (isset($this->unknownFields[$field_name])) {
  227. return;
  228. }
  229. // Do not check the (large) persistent cache, but read the definition.
  230. // Cache miss: read from definition.
  231. if ($field = field_read_field($field_name)) {
  232. $field = $this->prepareField($field);
  233. // Save in the "static" cache.
  234. $this->fieldsById[$field['id']] = $field;
  235. $this->fieldIdsByName[$field['field_name']] = $field['id'];
  236. return $field;
  237. }
  238. else {
  239. $this->unknownFields[$field_name] = TRUE;
  240. }
  241. }
  242. /**
  243. * Returns a field definition from a field ID.
  244. *
  245. * This method only retrieves active fields, deleted or not.
  246. *
  247. * @param $field_id
  248. * The field ID.
  249. *
  250. * @return
  251. * The field definition, or NULL if no field was found.
  252. */
  253. public function getFieldById($field_id) {
  254. // Read from the "static" cache.
  255. if (isset($this->fieldsById[$field_id])) {
  256. return $this->fieldsById[$field_id];
  257. }
  258. if (isset($this->unknownFields[$field_id])) {
  259. return;
  260. }
  261. // No persistent cache, fields are only persistently cached as part of a
  262. // bundle.
  263. // Cache miss: read from definition.
  264. if ($fields = field_read_fields(array('id' => $field_id), array('include_deleted' => TRUE))) {
  265. $field = current($fields);
  266. $field = $this->prepareField($field);
  267. // Store in the static cache.
  268. $this->fieldsById[$field['id']] = $field;
  269. if (!$field['deleted']) {
  270. $this->fieldIdsByName[$field['field_name']] = $field['id'];
  271. }
  272. return $field;
  273. }
  274. else {
  275. $this->unknownFields[$field_id] = TRUE;
  276. }
  277. }
  278. /**
  279. * Retrieves the instances for a bundle.
  280. *
  281. * The function also populates the corresponding field definitions in the
  282. * "static" cache.
  283. *
  284. * @param $entity_type
  285. * The entity type.
  286. * @param $bundle
  287. * The bundle name.
  288. *
  289. * @return
  290. * The array of instance definitions, keyed by field name.
  291. */
  292. public function getBundleInstances($entity_type, $bundle) {
  293. // Read from the "static" cache.
  294. if (isset($this->bundleInstances[$entity_type][$bundle])) {
  295. return $this->bundleInstances[$entity_type][$bundle];
  296. }
  297. if (isset($this->emptyBundles[$entity_type][$bundle])) {
  298. return array();
  299. }
  300. // Read from the persistent cache.
  301. if ($cached = cache_get("field_info:bundle:$entity_type:$bundle", 'cache_field')) {
  302. $info = $cached->data;
  303. // Extract the field definitions and save them in the "static" cache.
  304. foreach ($info['fields'] as $field) {
  305. if (!isset($this->fieldsById[$field['id']])) {
  306. $this->fieldsById[$field['id']] = $field;
  307. if (!$field['deleted']) {
  308. $this->fieldIdsByName[$field['field_name']] = $field['id'];
  309. }
  310. }
  311. }
  312. unset($info['fields']);
  313. // Store the instance definitions in the "static" cache'. Empty (or
  314. // non-existent) bundles are stored separately, so that they do not
  315. // pollute the global list returned by getInstances().
  316. if ($info['instances']) {
  317. $this->bundleInstances[$entity_type][$bundle] = $info['instances'];
  318. }
  319. else {
  320. $this->emptyBundles[$entity_type][$bundle] = TRUE;
  321. }
  322. return $info['instances'];
  323. }
  324. // Cache miss: collect from the definitions.
  325. $instances = array();
  326. // Collect the fields in the bundle.
  327. $params = array('entity_type' => $entity_type, 'bundle' => $bundle);
  328. $fields = field_read_fields($params);
  329. // This iterates on non-deleted instances, so deleted fields are kept out of
  330. // the persistent caches.
  331. foreach (field_read_instances($params) as $instance) {
  332. $field = $fields[$instance['field_name']];
  333. $instance = $this->prepareInstance($instance, $field['type']);
  334. $instances[$field['field_name']] = $instance;
  335. // If the field is not in our global "static" list yet, add it.
  336. if (!isset($this->fieldsById[$field['id']])) {
  337. $field = $this->prepareField($field);
  338. $this->fieldsById[$field['id']] = $field;
  339. $this->fieldIdsByName[$field['field_name']] = $field['id'];
  340. }
  341. }
  342. // Store in the 'static' cache'. Empty (or non-existent) bundles are stored
  343. // separately, so that they do not pollute the global list returned by
  344. // getInstances().
  345. if ($instances) {
  346. $this->bundleInstances[$entity_type][$bundle] = $instances;
  347. }
  348. else {
  349. $this->emptyBundles[$entity_type][$bundle] = TRUE;
  350. }
  351. // The persistent cache additionally contains the definitions of the fields
  352. // involved in the bundle.
  353. $cache = array(
  354. 'instances' => $instances,
  355. 'fields' => array()
  356. );
  357. foreach ($instances as $instance) {
  358. $cache['fields'][] = $this->fieldsById[$instance['field_id']];
  359. }
  360. cache_set("field_info:bundle:$entity_type:$bundle", $cache, 'cache_field');
  361. return $instances;
  362. }
  363. /**
  364. * Retrieves the "extra fields" for a bundle.
  365. *
  366. * @param $entity_type
  367. * The entity type.
  368. * @param $bundle
  369. * The bundle name.
  370. *
  371. * @return
  372. * The array of extra fields.
  373. */
  374. public function getBundleExtraFields($entity_type, $bundle) {
  375. // Read from the "static" cache.
  376. if (isset($this->bundleExtraFields[$entity_type][$bundle])) {
  377. return $this->bundleExtraFields[$entity_type][$bundle];
  378. }
  379. // Read from the persistent cache.
  380. if ($cached = cache_get("field_info:bundle_extra:$entity_type:$bundle", 'cache_field')) {
  381. $this->bundleExtraFields[$entity_type][$bundle] = $cached->data;
  382. return $this->bundleExtraFields[$entity_type][$bundle];
  383. }
  384. // Cache miss: read from hook_field_extra_fields(). Note: given the current
  385. // shape of the hook, we have no other way than collecting extra fields on
  386. // all bundles.
  387. $info = array();
  388. $extra = module_invoke_all('field_extra_fields');
  389. drupal_alter('field_extra_fields', $extra);
  390. // Merge in saved settings.
  391. if (isset($extra[$entity_type][$bundle])) {
  392. $info = $this->prepareExtraFields($extra[$entity_type][$bundle], $entity_type, $bundle);
  393. }
  394. // Store in the 'static' and persistent caches.
  395. $this->bundleExtraFields[$entity_type][$bundle] = $info;
  396. cache_set("field_info:bundle_extra:$entity_type:$bundle", $info, 'cache_field');
  397. return $this->bundleExtraFields[$entity_type][$bundle];
  398. }
  399. /**
  400. * Prepares a field definition for the current run-time context.
  401. *
  402. * @param $field
  403. * The raw field structure as read from the database.
  404. *
  405. * @return
  406. * The field definition completed for the current runtime context.
  407. */
  408. public function prepareField($field) {
  409. // Make sure all expected field settings are present.
  410. $field['settings'] += field_info_field_settings($field['type']);
  411. $field['storage']['settings'] += field_info_storage_settings($field['storage']['type']);
  412. // Add storage details.
  413. $details = (array) module_invoke($field['storage']['module'], 'field_storage_details', $field);
  414. drupal_alter('field_storage_details', $details, $field);
  415. $field['storage']['details'] = $details;
  416. // Populate the list of bundles using the field.
  417. $field['bundles'] = array();
  418. if (!$field['deleted']) {
  419. $map = $this->getFieldMap();
  420. if (isset($map[$field['field_name']])) {
  421. $field['bundles'] = $map[$field['field_name']]['bundles'];
  422. }
  423. }
  424. return $field;
  425. }
  426. /**
  427. * Prepares an instance definition for the current run-time context.
  428. *
  429. * @param $instance
  430. * The raw instance structure as read from the database.
  431. * @param $field_type
  432. * The field type.
  433. *
  434. * @return
  435. * The field instance array completed for the current runtime context.
  436. */
  437. public function prepareInstance($instance, $field_type) {
  438. // Make sure all expected instance settings are present.
  439. $instance['settings'] += field_info_instance_settings($field_type);
  440. // Set a default value for the instance.
  441. if (field_behaviors_widget('default value', $instance) == FIELD_BEHAVIOR_DEFAULT && !isset($instance['default_value'])) {
  442. $instance['default_value'] = NULL;
  443. }
  444. // Prepare widget settings.
  445. $instance['widget'] = $this->prepareInstanceWidget($instance['widget'], $field_type);
  446. // Prepare display settings.
  447. foreach ($instance['display'] as $view_mode => $display) {
  448. $instance['display'][$view_mode] = $this->prepareInstanceDisplay($display, $field_type);
  449. }
  450. // Fall back to 'hidden' for view modes configured to use custom display
  451. // settings, and for which the instance has no explicit settings.
  452. $entity_info = entity_get_info($instance['entity_type']);
  453. $view_modes = array_merge(array('default'), array_keys($entity_info['view modes']));
  454. $view_mode_settings = field_view_mode_settings($instance['entity_type'], $instance['bundle']);
  455. foreach ($view_modes as $view_mode) {
  456. if ($view_mode == 'default' || !empty($view_mode_settings[$view_mode]['custom_settings'])) {
  457. if (!isset($instance['display'][$view_mode])) {
  458. $instance['display'][$view_mode] = array(
  459. 'type' => 'hidden',
  460. 'label' => 'above',
  461. 'settings' => array(),
  462. 'weight' => 0,
  463. );
  464. }
  465. }
  466. }
  467. return $instance;
  468. }
  469. /**
  470. * Prepares widget properties for the current run-time context.
  471. *
  472. * @param $widget
  473. * Widget specifications as found in $instance['widget'].
  474. * @param $field_type
  475. * The field type.
  476. *
  477. * @return
  478. * The widget properties completed for the current runtime context.
  479. */
  480. public function prepareInstanceWidget($widget, $field_type) {
  481. $field_type_info = field_info_field_types($field_type);
  482. // Fill in default values.
  483. $widget += array(
  484. 'type' => $field_type_info['default_widget'],
  485. 'settings' => array(),
  486. 'weight' => 0,
  487. );
  488. $widget_type_info = field_info_widget_types($widget['type']);
  489. // Fall back to default formatter if formatter type is not available.
  490. if (!$widget_type_info) {
  491. $widget['type'] = $field_type_info['default_widget'];
  492. $widget_type_info = field_info_widget_types($widget['type']);
  493. }
  494. $widget['module'] = $widget_type_info['module'];
  495. // Fill in default settings for the widget.
  496. $widget['settings'] += field_info_widget_settings($widget['type']);
  497. return $widget;
  498. }
  499. /**
  500. * Adapts display specifications to the current run-time context.
  501. *
  502. * @param $display
  503. * Display specifications as found in $instance['display']['a_view_mode'].
  504. * @param $field_type
  505. * The field type.
  506. *
  507. * @return
  508. * The display properties completed for the current runtime context.
  509. */
  510. public function prepareInstanceDisplay($display, $field_type) {
  511. $field_type_info = field_info_field_types($field_type);
  512. // Fill in default values.
  513. $display += array(
  514. 'label' => 'above',
  515. 'type' => $field_type_info['default_formatter'],
  516. 'settings' => array(),
  517. 'weight' => 0,
  518. );
  519. if ($display['type'] != 'hidden') {
  520. $formatter_type_info = field_info_formatter_types($display['type']);
  521. // Fall back to default formatter if formatter type is not available.
  522. if (!$formatter_type_info) {
  523. $display['type'] = $field_type_info['default_formatter'];
  524. $formatter_type_info = field_info_formatter_types($display['type']);
  525. }
  526. $display['module'] = $formatter_type_info['module'];
  527. // Fill in default settings for the formatter.
  528. $display['settings'] += field_info_formatter_settings($display['type']);
  529. }
  530. return $display;
  531. }
  532. /**
  533. * Prepares 'extra fields' for the current run-time context.
  534. *
  535. * @param $extra_fields
  536. * The array of extra fields, as collected in hook_field_extra_fields().
  537. * @param $entity_type
  538. * The entity type.
  539. * @param $bundle
  540. * The bundle name.
  541. *
  542. * @return
  543. * The list of extra fields completed for the current runtime context.
  544. */
  545. public function prepareExtraFields($extra_fields, $entity_type, $bundle) {
  546. $entity_type_info = entity_get_info($entity_type);
  547. $bundle_settings = field_bundle_settings($entity_type, $bundle);
  548. $extra_fields += array('form' => array(), 'display' => array());
  549. $result = array();
  550. // Extra fields in forms.
  551. foreach ($extra_fields['form'] as $name => $field_data) {
  552. $settings = isset($bundle_settings['extra_fields']['form'][$name]) ? $bundle_settings['extra_fields']['form'][$name] : array();
  553. if (isset($settings['weight'])) {
  554. $field_data['weight'] = $settings['weight'];
  555. }
  556. $result['form'][$name] = $field_data;
  557. }
  558. // Extra fields in displayed entities.
  559. $data = $extra_fields['display'];
  560. foreach ($extra_fields['display'] as $name => $field_data) {
  561. $settings = isset($bundle_settings['extra_fields']['display'][$name]) ? $bundle_settings['extra_fields']['display'][$name] : array();
  562. $view_modes = array_merge(array('default'), array_keys($entity_type_info['view modes']));
  563. foreach ($view_modes as $view_mode) {
  564. if (isset($settings[$view_mode])) {
  565. $field_data['display'][$view_mode] = $settings[$view_mode];
  566. }
  567. else {
  568. $field_data['display'][$view_mode] = array(
  569. 'weight' => $field_data['weight'],
  570. 'visible' => TRUE,
  571. );
  572. }
  573. }
  574. unset($field_data['weight']);
  575. $result['display'][$name] = $field_data;
  576. }
  577. return $result;
  578. }
  579. }