Issue #1950088 by swentel: Move FieldInfo class to the DIC.
parent
c54ae0f51c
commit
c285bbcc43
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\field\Field.
|
||||
*/
|
||||
|
||||
namespace Drupal\field;
|
||||
|
||||
use Drupal;
|
||||
|
||||
/**
|
||||
* Static service container wrapper for Field.
|
||||
*/
|
||||
class Field {
|
||||
|
||||
/**
|
||||
* Returns the field info service.
|
||||
*
|
||||
* @return \Drupal\field\FieldInfo
|
||||
* Returns a field info object.
|
||||
*/
|
||||
public static function fieldInfo() {
|
||||
return Drupal::service('field.info');
|
||||
}
|
||||
|
||||
}
|
|
@ -1,21 +1,19 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
/**
|
||||
* @file
|
||||
* Definition of Drupal\field\FieldInfo.
|
||||
* Contains \Drupal\field\FieldInfo.
|
||||
*/
|
||||
|
||||
namespace Drupal\field;
|
||||
|
||||
use Drupal\Core\Cache\CacheBackendInterface;
|
||||
use Drupal\Core\Config\ConfigFactory;
|
||||
use Drupal\Core\Extension\ModuleHandlerInterface;
|
||||
|
||||
/**
|
||||
* Provides field and instance definitions for the current runtime environment.
|
||||
*
|
||||
* A Drupal\field\FieldInfo object is created and statically persisted through
|
||||
* the request by the _field_info_field_cache() function. The object properties
|
||||
* act as a "static cache" of fields and instances definitions.
|
||||
*
|
||||
* The preferred way to access definitions is through the getBundleInstances()
|
||||
* method, which keeps cache entries per bundle, storing both fields and
|
||||
* instances for a given bundle. Fields used in multiple bundles are duplicated
|
||||
|
@ -32,6 +30,27 @@ use Drupal\Core\Cache\CacheBackendInterface;
|
|||
*/
|
||||
class FieldInfo {
|
||||
|
||||
/**
|
||||
* The cache backend to use.
|
||||
*
|
||||
* @var \Drupal\Core\Cache\CacheBackendInterface
|
||||
*/
|
||||
protected $cacheBackend;
|
||||
|
||||
/**
|
||||
* Stores a module manager to invoke hooks.
|
||||
*
|
||||
* @var \Drupal\Core\Extension\ModuleHandlerInterface
|
||||
*/
|
||||
protected $moduleHandler;
|
||||
|
||||
/**
|
||||
* The config factory.
|
||||
*
|
||||
* @var \Drupal\Core\Config\ConfigFactory
|
||||
*/
|
||||
protected $config;
|
||||
|
||||
/**
|
||||
* Lightweight map of fields across entity types and bundles.
|
||||
*
|
||||
|
@ -95,6 +114,22 @@ class FieldInfo {
|
|||
*/
|
||||
protected $bundleExtraFields = array();
|
||||
|
||||
/**
|
||||
* Constructs this FieldInfo object.
|
||||
*
|
||||
* @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
|
||||
* The cache backend to use.
|
||||
* @param \Drupal\Core\Config\ConfigFactory $config
|
||||
* The configuration factory object to use.
|
||||
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
|
||||
* The module handler class to use for invoking hooks.
|
||||
*/
|
||||
public function __construct(CacheBackendInterface $cache_backend, ConfigFactory $config, ModuleHandlerInterface $module_handler) {
|
||||
$this->cacheBackend = $cache_backend;
|
||||
$this->moduleHandler = $module_handler;
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears the "static" and persistent caches.
|
||||
*/
|
||||
|
@ -112,7 +147,7 @@ class FieldInfo {
|
|||
|
||||
$this->bundleExtraFields = array();
|
||||
|
||||
cache('field')->deleteTags(array('field_info' => TRUE));
|
||||
$this->cacheBackend->deleteTags(array('field_info' => TRUE));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -131,7 +166,7 @@ class FieldInfo {
|
|||
}
|
||||
|
||||
// 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 +179,14 @@ class FieldInfo {
|
|||
|
||||
// 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 +199,7 @@ class FieldInfo {
|
|||
|
||||
// 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 +217,7 @@ class FieldInfo {
|
|||
}
|
||||
|
||||
// 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 +227,7 @@ class FieldInfo {
|
|||
}
|
||||
|
||||
// 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 +258,7 @@ class FieldInfo {
|
|||
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 +275,7 @@ class FieldInfo {
|
|||
}
|
||||
|
||||
// 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 +392,7 @@ class FieldInfo {
|
|||
}
|
||||
|
||||
// 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 +475,7 @@ class FieldInfo {
|
|||
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 +498,7 @@ class FieldInfo {
|
|||
}
|
||||
|
||||
// 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 +507,7 @@ class FieldInfo {
|
|||
// 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 +516,7 @@ class FieldInfo {
|
|||
|
||||
// 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];
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue