diff --git a/core/modules/field/field.info.inc b/core/modules/field/field.info.inc index 4ec8bfe..d364a70 100644 --- a/core/modules/field/field.info.inc +++ b/core/modules/field/field.info.inc @@ -5,32 +5,9 @@ * Field Info API, providing information about available fields and field types. */ -use Drupal\field\Plugin\Core\Entity\FieldInstance; -use Drupal\field\FieldInfo; - -/** - * Retrieves the Drupal\field\FieldInfo object for the current request. - * - * @return Drupal\field\FieldInfo - * An instance of the Drupal\field\FieldInfo class. - */ -function _field_info_field_cache() { - // 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_field_cache'] = &drupal_static(__FUNCTION__); - } - $info = &$drupal_static_fast['field_info_field_cache']; - - if (!isset($info)) { - $info = new FieldInfo(); - } - - return $info; -} - use Drupal\Core\Cache\CacheBackendInterface; +use Drupal\field\Plugin\Core\Entity\FieldInstance; +use Drupal\field\Field; /** * @defgroup field_info Field Info API @@ -60,7 +37,7 @@ function field_info_cache_clear() { entity_info_cache_clear(); _field_info_collate_types_reset(); - _field_info_field_cache()->flush(); + Field::fieldInfo()->flush(); } /** @@ -182,8 +159,7 @@ function field_behaviors_widget($op, $instance) { * types as keys and the array of bundle names as values. */ function field_info_field_map() { - $cache = _field_info_field_cache(); - return $cache->getFieldMap(); + return Field::fieldInfo()->getFieldMap(); } /** @@ -294,8 +270,7 @@ function field_info_storage_types($storage_type = NULL) { * @see field_info_field_map() */ function field_info_fields() { - $cache = _field_info_field_cache(); - $info = $cache->getFields(); + $info = Field::fieldInfo()->getFields(); $fields = array(); foreach ($info as $key => $field) { @@ -325,8 +300,7 @@ function field_info_fields() { * @see field_info_field_by_id() */ function field_info_field($field_name) { - $cache = _field_info_field_cache(); - return $cache->getField($field_name); + return Field::fieldInfo()->getField($field_name); } /** @@ -344,8 +318,7 @@ function field_info_field($field_name) { * @see field_info_field() */ function field_info_field_by_id($field_id) { - $cache = _field_info_field_cache(); - return $cache->getFieldById($field_id); + return Field::fieldInfo()->getFieldById($field_id); } /** @@ -367,8 +340,7 @@ function field_info_field_by_id($field_id) { * @see field_info_field_by_id() */ function field_info_field_by_ids() { - $cache = _field_info_field_cache(); - return $cache->getFields(); + return Field::fieldInfo()->getFields(); } /** @@ -397,7 +369,7 @@ function field_info_field_by_ids() { * return all instances for that bundle. */ function field_info_instances($entity_type = NULL, $bundle_name = NULL) { - $cache = _field_info_field_cache(); + $cache = Field::fieldInfo(); if (!isset($entity_type)) { return $cache->getInstances(); @@ -429,8 +401,7 @@ function field_info_instances($entity_type = NULL, $bundle_name = NULL) { * NULL if the instance does not exist. */ function field_info_instance($entity_type, $field_name, $bundle_name) { - $cache = _field_info_field_cache(); - $info = $cache->getBundleInstances($entity_type, $bundle_name); + $info = Field::fieldInfo()->getBundleInstances($entity_type, $bundle_name); if (isset($info[$field_name])) { return $info[$field_name]; } @@ -490,8 +461,7 @@ function field_info_instance($entity_type, $field_name, $bundle_name) { * The array of pseudo-field elements in the bundle. */ function field_info_extra_fields($entity_type, $bundle, $context) { - $cache = _field_info_field_cache(); - $info = $cache->getBundleExtraFields($entity_type, $bundle); + $info = Field::fieldInfo()->getBundleExtraFields($entity_type, $bundle); return isset($info[$context]) ? $info[$context] : array(); } diff --git a/core/modules/field/field.services.yml b/core/modules/field/field.services.yml index 8e79c7f..2313254 100644 --- a/core/modules/field/field.services.yml +++ b/core/modules/field/field.services.yml @@ -5,6 +5,9 @@ services: plugin.manager.field.formatter: class: Drupal\field\Plugin\Type\Formatter\FormatterPluginManager arguments: ['%container.namespaces%'] + field.info: + class: Drupal\field\FieldInfo + arguments: ['@cache.field', '@config.factory', '@module_handler'] cache.field: class: Drupal\Core\Cache\CacheBackendInterface tags: diff --git a/core/modules/field/lib/Drupal/field/Field.php b/core/modules/field/lib/Drupal/field/Field.php new file mode 100644 index 0000000..80d356f --- /dev/null +++ b/core/modules/field/lib/Drupal/field/Field.php @@ -0,0 +1,27 @@ +cacheBackend = $cache_backend; + $this->moduleHandler = $module_handler; + $this->config = $config; + } + + /** * Clears the "static" and persistent caches. */ public function flush() { @@ -112,7 +151,7 @@ public function flush() { $this->bundleExtraFields = array(); - cache('field')->deleteTags(array('field_info' => TRUE)); + $this->cacheBackend->deleteTags(array('field_info' => TRUE)); } /** @@ -131,7 +170,7 @@ public function getFieldMap() { } // Read from persistent cache. - if ($cached = cache('field')->get('field_info:field_map')) { + if ($cached = $this->cacheBackend->get('field_info:field_map')) { $map = $cached->data; // Save in "static" cache. @@ -144,14 +183,14 @@ public function getFieldMap() { // Get active fields. foreach (config_get_storage_names_with_prefix('field.field') as $config_id) { - $field_config = \Drupal::config($config_id)->get(); + $field_config = $this->config->get($config_id)->get(); if ($field_config['active'] && $field_config['storage']['active']) { $fields[$field_config['uuid']] = $field_config; } } // Get field instances. foreach (config_get_storage_names_with_prefix('field.instance') as $config_id) { - $instance_config = \Drupal::config($config_id)->get(); + $instance_config = $this->config->get($config_id)->get(); $field_uuid = $instance_config['field_uuid']; // Filter out instances of inactive fields, and instances on unknown // entity types. @@ -164,7 +203,7 @@ public function getFieldMap() { // Save in "static" and persistent caches. $this->fieldMap = $map; - cache('field')->set('field_info:field_map', $map, CacheBackendInterface::CACHE_PERMANENT, array('field_info' => TRUE)); + $this->cacheBackend->set('field_info:field_map', $map, CacheBackendInterface::CACHE_PERMANENT, array('field_info' => TRUE)); return $map; } @@ -182,7 +221,7 @@ public function getFields() { } // Read from persistent cache. - if ($cached = cache('field')->get('field_info:fields')) { + if ($cached = $this->cacheBackend->get('field_info:fields')) { $this->fieldsById = $cached->data; } else { @@ -192,7 +231,7 @@ public function getFields() { } // Store in persistent cache. - cache('field')->set('field_info:fields', $this->fieldsById, CacheBackendInterface::CACHE_PERMANENT, array('field_info' => TRUE)); + $this->cacheBackend->set('field_info:fields', $this->fieldsById, CacheBackendInterface::CACHE_PERMANENT, array('field_info' => TRUE)); } // Fill the name/ID map. @@ -223,7 +262,7 @@ public function getInstances($entity_type = NULL) { if (!$this->loadedAllInstances) { // Read from persistent cache. - if ($cached = cache('field')->get('field_info:instances')) { + if ($cached = $this->cacheBackend->get('field_info:instances')) { $this->bundleInstances = $cached->data; } else { @@ -240,7 +279,7 @@ public function getInstances($entity_type = NULL) { } // Store in persistent cache. - cache('field')->set('field_info:instances', $this->bundleInstances, CacheBackendInterface::CACHE_PERMANENT, array('field_info' => TRUE)); + $this->cacheBackend->set('field_info:instances', $this->bundleInstances, CacheBackendInterface::CACHE_PERMANENT, array('field_info' => TRUE)); } $this->loadedAllInstances = TRUE; @@ -357,7 +396,7 @@ public function getBundleInstances($entity_type, $bundle) { } // Read from the persistent cache. - if ($cached = cache('field')->get("field_info:bundle:$entity_type:$bundle")) { + if ($cached = $this->cacheBackend->get("field_info:bundle:$entity_type:$bundle")) { $info = $cached->data; // Extract the field definitions and save them in the "static" cache. @@ -440,7 +479,7 @@ public function getBundleInstances($entity_type, $bundle) { foreach ($instances as $instance) { $cache['fields'][] = $this->fieldsById[$instance['field_id']]; } - cache('field')->set("field_info:bundle:$entity_type:$bundle", $cache, CacheBackendInterface::CACHE_PERMANENT, array('field_info' => TRUE)); + $this->cacheBackend->set("field_info:bundle:$entity_type:$bundle", $cache, CacheBackendInterface::CACHE_PERMANENT, array('field_info' => TRUE)); return $instances; } @@ -463,7 +502,7 @@ public function getBundleExtraFields($entity_type, $bundle) { } // Read from the persistent cache. - if ($cached = cache('field')->get("field_info:bundle_extra:$entity_type:$bundle")) { + if ($cached = $this->cacheBackend->get("field_info:bundle_extra:$entity_type:$bundle")) { $this->bundleExtraFields[$entity_type][$bundle] = $cached->data; return $this->bundleExtraFields[$entity_type][$bundle]; } @@ -472,7 +511,7 @@ public function getBundleExtraFields($entity_type, $bundle) { // shape of the hook, we have no other way than collecting extra fields on // all bundles. $info = array(); - $extra = module_invoke_all('field_extra_fields'); + $extra = $this->moduleHandler->invokeAll('field_extra_fields'); drupal_alter('field_extra_fields', $extra); // Merge in saved settings. if (isset($extra[$entity_type][$bundle])) { @@ -481,7 +520,7 @@ public function getBundleExtraFields($entity_type, $bundle) { // Store in the 'static' and persistent caches. $this->bundleExtraFields[$entity_type][$bundle] = $info; - cache('field')->set("field_info:bundle_extra:$entity_type:$bundle", $info, CacheBackendInterface::CACHE_PERMANENT, array('field_info' => TRUE)); + $this->cacheBackend->set("field_info:bundle_extra:$entity_type:$bundle", $info, CacheBackendInterface::CACHE_PERMANENT, array('field_info' => TRUE)); return $this->bundleExtraFields[$entity_type][$bundle]; }