diff --git a/modules/field/field.info.inc b/modules/field/field.info.inc index cc8dac5..9e9b959 100644 --- a/modules/field/field.info.inc +++ b/modules/field/field.info.inc @@ -29,20 +29,14 @@ function field_info_cache_clear() { // functions are moved to the entity API. entity_info_cache_clear(); - _field_info_collate_types(TRUE); - _field_info_collate_fields(TRUE); + _field_info_collate_types_reset(); + _field_info_collate_fields_reset(); } /** * Collates all information on field types, widget types and related structures. * - * @param $reset - * If TRUE, clear the cache. The information will be rebuilt from the database - * next time it is needed. Defaults to FALSE. - * * @return - * If $reset is TRUE, nothing. - * If $reset is FALSE, an array containing the following elements: * - 'field types': Array of hook_field_info() results, keyed by field_type. * Each element has the following components: label, description, settings, * instance_settings, default_widget, default_formatter, and behaviors @@ -65,21 +59,20 @@ function field_info_cache_clear() { * revision key, bundle key, cacheable, and bundles from hook_entity_info(), * as well as module, giving the module that exposes the entity type. */ -function _field_info_collate_types($reset = FALSE) { +function _field_info_collate_types() { global $language; - static $info; + // Use the advanced drupal_static() pattern, since this is called very often. + static $drupal_static_fast; + + if (!isset($drupal_static_fast)) { + $drupal_static_fast['field_info_collate_types'] = &drupal_static(__FUNCTION__); + } + $info = &$drupal_static_fast['field_info_collate_types']; // The _info() hooks invoked below include translated strings, so each // language is cached separately. $langcode = $language->language; - if ($reset) { - $info = NULL; - // Clear all languages. - cache('field')->deletePrefix('field_info_types:'); - return; - } - if (!isset($info)) { if ($cached = cache('field')->get("field_info_types:$langcode")) { $info = $cached->data; @@ -157,15 +150,18 @@ function _field_info_collate_types($reset = FALSE) { } /** + * Clear collated information on field and widget types and related structures. + */ +function _field_info_collate_types_reset() { + drupal_static_reset('_field_info_collate_types'); + // Clear all languages. + cache('field')->deletePrefix('field_info_types:'); +} + +/** * Collates all information on existing fields and instances. * - * @param $reset - * If TRUE, clear the cache. The information will be rebuilt from the - * database next time it is needed. Defaults to FALSE. - * * @return - * If $reset is TRUE, nothing. - * If $reset is FALSE, an array containing the following elements: * - fields: Array of existing fields, keyed by field ID. This element * lists deleted and non-deleted fields, but not inactive ones. * Each field has an additional element, 'bundles', which is an array @@ -176,14 +172,14 @@ function _field_info_collate_types($reset = FALSE) { * name and field name. This element only lists non-deleted instances * whose field is active. */ -function _field_info_collate_fields($reset = FALSE) { - static $info; +function _field_info_collate_fields() { + // Use the advanced drupal_static() pattern, since this is called very often. + static $drupal_static_fast; - if ($reset) { - $info = NULL; - cache('field')->delete('field_info_fields'); - return; + if (!isset($drupal_static_fast)) { + $drupal_static_fast['field_info_collate_fields'] = &drupal_static(__FUNCTION__); } + $info = &$drupal_static_fast['field_info_collate_fields']; if (!isset($info)) { if ($cached = cache('field')->get('field_info_fields')) { @@ -246,6 +242,14 @@ function _field_info_collate_fields($reset = FALSE) { } /** + * Clear collated information on existing fields and instances. + */ +function _field_info_collate_fields_reset() { + drupal_static_reset('_field_info_collate_fields'); + cache('field')->delete('field_info_fields'); +} + +/** * Prepares a field definition for the current run-time context. * * Since the field was last saved or updated, new field settings can be diff --git a/modules/field_ui/field_ui.test b/modules/field_ui/field_ui.test index 9ff6c17..b898695 100644 --- a/modules/field_ui/field_ui.test +++ b/modules/field_ui/field_ui.test @@ -269,7 +269,7 @@ class FieldUIManageFieldsTestCase extends FieldUITestCase { */ function assertFieldSettings($bundle, $field_name, $string = 'dummy test string', $entity_type = 'node') { // Reset the fields info. - _field_info_collate_fields(TRUE); + _field_info_collate_fields_reset(); // Assert field settings. $field = field_info_field($field_name); $this->assertTrue($field['settings']['test_field_setting'] == $string, t('Field settings were found.')); @@ -360,7 +360,7 @@ class FieldUIManageFieldsTestCase extends FieldUITestCase { $this->fieldUIDeleteField($bundle_path1, $this->field_name, $this->field_label, $this->type); // Reset the fields info. - _field_info_collate_fields(TRUE); + _field_info_collate_fields_reset(); // Check that the field instance was deleted. $this->assertNull(field_info_instance('node', $this->field_name, $this->type), t('Field instance was deleted.')); // Check that the field was not deleted @@ -370,7 +370,7 @@ class FieldUIManageFieldsTestCase extends FieldUITestCase { $this->fieldUIDeleteField($bundle_path2, $this->field_name, $this->field_label, $type_name2); // Reset the fields info. - _field_info_collate_fields(TRUE); + _field_info_collate_fields_reset(); // Check that the field instance was deleted. $this->assertNull(field_info_instance('node', $this->field_name, $type_name2), t('Field instance was deleted.')); // Check that the field was deleted too.