Commit f0928e53 authored by alexpott's avatar alexpott

Issue #2337191 by tim.plunkett, dawehner, jibran, yched, Wim Leers, catch:...

Issue #2337191 by tim.plunkett, dawehner, jibran, yched, Wim Leers, catch: Split up EntityManager into many services
parent 6513bac6
......@@ -496,10 +496,37 @@ services:
arguments: ['@theme_handler', '@config.factory', '@config.installer', '@module_handler', '@config.manager', '@asset.css.collection_optimizer', '@router.builder', '@logger.channel.default', '@state']
entity.manager:
class: Drupal\Core\Entity\EntityManager
arguments: ['@container.namespaces', '@module_handler', '@cache.discovery', '@language_manager', '@string_translation', '@class_resolver', '@typed_data_manager', '@keyvalue', '@event_dispatcher']
parent: container.trait
# @todo Remove this tag in https://www.drupal.org/node/2549143.
tags:
- { name: plugin_manager_cache_clear }
entity_type.manager:
class: Drupal\Core\Entity\EntityTypeManager
arguments: ['@container.namespaces', '@module_handler', '@cache.discovery', '@string_translation', '@class_resolver']
parent: container.trait
tags:
- { name: plugin_manager_cache_clear }
entity_type.repository:
class: Drupal\Core\Entity\EntityTypeRepository
arguments: ['@entity_type.manager']
entity_type.bundle.info:
class: Drupal\Core\Entity\EntityTypeBundleInfo
arguments: ['@entity_type.manager', '@language_manager', '@module_handler', '@typed_data_manager', '@cache.discovery']
entity.repository:
class: Drupal\Core\Entity\EntityRepository
arguments: ['@entity_type.manager', '@language_manager']
entity_display.repository:
class: Drupal\Core\Entity\EntityDisplayRepository
arguments: ['@entity_type.manager', '@module_handler', '@cache.discovery', '@language_manager']
entity_field.manager:
class: Drupal\Core\Entity\EntityFieldManager
arguments: ['@entity_type.manager', '@entity_type.bundle.info', '@entity_display.repository', '@typed_data_manager', '@language_manager', '@keyvalue', '@module_handler', '@cache.discovery']
entity_type.listener:
class: Drupal\Core\Entity\EntityTypeListener
arguments: ['@entity_type.manager', '@entity_field.manager', '@event_dispatcher', '@entity.last_installed_schema.repository']
entity_bundle.listener:
class: Drupal\Core\Entity\EntityBundleListener
arguments: ['@entity_type.manager', '@entity_type.bundle.info', '@entity_field.manager', '@module_handler']
entity_route_subscriber:
class: Drupal\Core\EventSubscriber\EntityRouteProviderSubscriber
arguments: ['@entity.manager']
......@@ -508,6 +535,15 @@ services:
entity.definition_update_manager:
class: Drupal\Core\Entity\EntityDefinitionUpdateManager
arguments: ['@entity.manager']
entity.last_installed_schema.repository:
class: Drupal\Core\Entity\EntityLastInstalledSchemaRepository
arguments: ['@keyvalue']
field_storage_definition.listener:
class: Drupal\Core\Field\FieldStorageDefinitionListener
arguments: ['@entity_type.manager', '@event_dispatcher', '@entity.last_installed_schema.repository', '@entity_field.manager']
field_definition.listener:
class: Drupal\Core\Field\FieldDefinitionListener
arguments: ['@entity_type.manager', '@entity_field.manager', '@keyvalue', '@cache.discovery']
entity.form_builder:
class: Drupal\Core\Entity\EntityFormBuilder
arguments: ['@entity.manager', '@form_builder']
......
<?php
/**
* @file
* Contains \Drupal\Core\Cache\UseCacheBackendTrait.
*/
namespace Drupal\Core\Cache;
/**
* Provides methods to use a cache backend while respecting a 'use caches' flag.
*/
trait UseCacheBackendTrait {
/**
* Cache backend instance.
*
* @var \Drupal\Core\Cache\CacheBackendInterface
*/
protected $cacheBackend;
/**
* Flag whether caches should be used or skipped.
*
* @var bool
*/
protected $useCaches = TRUE;
/**
* Fetches from the cache backend, respecting the use caches flag.
*
* @param string $cid
* The cache ID of the data to retrieve.
*
* @return object|false
* The cache item or FALSE on failure.
*
* @see \Drupal\Core\Cache\CacheBackendInterface::get()
*/
protected function cacheGet($cid) {
if ($this->useCaches && $this->cacheBackend) {
return $this->cacheBackend->get($cid);
}
return FALSE;
}
/**
* Stores data in the persistent cache, respecting the use caches flag.
*
* @param string $cid
* The cache ID of the data to store.
* @param mixed $data
* The data to store in the cache.
* Some storage engines only allow objects up to a maximum of 1MB in size to
* be stored by default. When caching large arrays or similar, take care to
* ensure $data does not exceed this size.
* @param int $expire
* One of the following values:
* - CacheBackendInterface::CACHE_PERMANENT: Indicates that the item should
* not be removed unless it is deleted explicitly.
* - A Unix timestamp: Indicates that the item will be considered invalid
* after this time, i.e. it will not be returned by get() unless
* $allow_invalid has been set to TRUE. When the item has expired, it may
* be permanently deleted by the garbage collector at any time.
* @param array $tags
* An array of tags to be stored with the cache item. These should normally
* identify objects used to build the cache item, which should trigger
* cache invalidation when updated. For example if a cached item represents
* a node, both the node ID and the author's user ID might be passed in as
* tags. For example array('node' => array(123), 'user' => array(92)).
*
* @see \Drupal\Core\Cache\CacheBackendInterface::set()
*/
protected function cacheSet($cid, $data, $expire = Cache::PERMANENT, array $tags = []) {
if ($this->cacheBackend && $this->useCaches) {
$this->cacheBackend->set($cid, $data, $expire, $tags);
}
}
}
<?php
/**
* @file
* Contains \Drupal\Core\Entity\EntityBundleListener.
*/
namespace Drupal\Core\Entity;
use Drupal\Core\Extension\ModuleHandlerInterface;
/**
* Reacts to entity bundle CRUD on behalf of the Entity system.
*/
class EntityBundleListener implements EntityBundleListenerInterface {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The entity type bundle info.
*
* @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
*/
protected $entityTypeBundleInfo;
/**
* The entity field manager.
*
* @var \Drupal\Core\Entity\EntityFieldManagerInterface
*/
protected $entityFieldManager;
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* Constructs a new EntityBundleListener.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
* The entity type bundle info.
* @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
* The entity field manager.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info, EntityFieldManagerInterface $entity_field_manager, ModuleHandlerInterface $module_handler) {
$this->entityTypeManager = $entity_type_manager;
$this->entityTypeBundleInfo = $entity_type_bundle_info;
$this->entityFieldManager = $entity_field_manager;
$this->moduleHandler = $module_handler;
}
/**
* {@inheritdoc}
*/
public function onBundleCreate($bundle, $entity_type_id) {
$this->entityTypeBundleInfo->clearCachedBundles();
// Notify the entity storage.
$storage = $this->entityTypeManager->getStorage($entity_type_id);
if ($storage instanceof EntityBundleListenerInterface) {
$storage->onBundleCreate($bundle, $entity_type_id);
}
// Invoke hook_entity_bundle_create() hook.
$this->moduleHandler->invokeAll('entity_bundle_create', [$entity_type_id, $bundle]);
}
/**
* {@inheritdoc}
*/
public function onBundleDelete($bundle, $entity_type_id) {
$this->entityTypeBundleInfo->clearCachedBundles();
// Notify the entity storage.
$storage = $this->entityTypeManager->getStorage($entity_type_id);
if ($storage instanceof EntityBundleListenerInterface) {
$storage->onBundleDelete($bundle, $entity_type_id);
}
// Invoke hook_entity_bundle_delete() hook.
$this->moduleHandler->invokeAll('entity_bundle_delete', [$entity_type_id, $bundle]);
$this->entityFieldManager->clearCachedFieldDefinitions();
}
}
<?php
/**
* @file
* Contains \Drupal\Core\Entity\EntityDisplayRepository.
*/
namespace Drupal\Core\Entity;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Cache\UseCacheBackendTrait;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
/**
* Provides a repository for entity display objects (view modes and form modes).
*/
class EntityDisplayRepository implements EntityDisplayRepositoryInterface {
use UseCacheBackendTrait;
use StringTranslationTrait;
/**
* Static cache of display modes information.
*
* @var array
*/
protected $displayModeInfo = [];
/**
* The language manager.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* Constructs a new EntityDisplayRepository.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
* @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
* The cache backend.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, ModuleHandlerInterface $module_handler, CacheBackendInterface $cache_backend, LanguageManagerInterface $language_manager) {
$this->entityTypeManager = $entity_type_manager;
$this->moduleHandler = $module_handler;
$this->cacheBackend = $cache_backend;
$this->languageManager = $language_manager;
}
/**
* {@inheritdoc}
*/
public function getAllViewModes() {
return $this->getAllDisplayModesByEntityType('view_mode');
}
/**
* {@inheritdoc}
*/
public function getViewModes($entity_type_id) {
return $this->getDisplayModesByEntityType('view_mode', $entity_type_id);
}
/**
* {@inheritdoc}
*/
public function getAllFormModes() {
return $this->getAllDisplayModesByEntityType('form_mode');
}
/**
* {@inheritdoc}
*/
public function getFormModes($entity_type_id) {
return $this->getDisplayModesByEntityType('form_mode', $entity_type_id);
}
/**
* Gets the entity display mode info for all entity types.
*
* @param string $display_type
* The display type to be retrieved. It can be "view_mode" or "form_mode".
*
* @return array
* The display mode info for all entity types.
*/
protected function getAllDisplayModesByEntityType($display_type) {
if (!isset($this->displayModeInfo[$display_type])) {
$key = 'entity_' . $display_type . '_info';
$entity_type_id = 'entity_' . $display_type;
$langcode = $this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_INTERFACE)->getId();
if ($cache = $this->cacheGet("$key:$langcode")) {
$this->displayModeInfo[$display_type] = $cache->data;
}
else {
$this->displayModeInfo[$display_type] = [];
foreach ($this->entityTypeManager->getStorage($entity_type_id)->loadMultiple() as $display_mode) {
list($display_mode_entity_type, $display_mode_name) = explode('.', $display_mode->id(), 2);
$this->displayModeInfo[$display_type][$display_mode_entity_type][$display_mode_name] = $display_mode->toArray();
}
$this->moduleHandler->alter($key, $this->displayModeInfo[$display_type]);
$this->cacheSet("$key:$langcode", $this->displayModeInfo[$display_type], CacheBackendInterface::CACHE_PERMANENT, ['entity_types', 'entity_field_info']);
}
}
return $this->displayModeInfo[$display_type];
}
/**
* Gets the entity display mode info for a specific entity type.
*
* @param string $display_type
* The display type to be retrieved. It can be "view_mode" or "form_mode".
* @param string $entity_type_id
* The entity type whose display mode info should be returned.
*
* @return array
* The display mode info for a specific entity type.
*/
protected function getDisplayModesByEntityType($display_type, $entity_type_id) {
if (isset($this->displayModeInfo[$display_type][$entity_type_id])) {
return $this->displayModeInfo[$display_type][$entity_type_id];
}
else {
$display_modes = $this->getAllDisplayModesByEntityType($display_type);
if (isset($display_modes[$entity_type_id])) {
return $display_modes[$entity_type_id];
}
}
return [];
}
/**
* {@inheritdoc}
*/
public function getViewModeOptions($entity_type) {
return $this->getDisplayModeOptions('view_mode', $entity_type);
}
/**
* {@inheritdoc}
*/
public function getFormModeOptions($entity_type_id) {
return $this->getDisplayModeOptions('form_mode', $entity_type_id);
}
/**
* {@inheritdoc}
*/
public function getViewModeOptionsByBundle($entity_type_id, $bundle) {
return $this->getDisplayModeOptionsByBundle('view_mode', $entity_type_id, $bundle);
}
/**
* {@inheritdoc}
*/
public function getFormModeOptionsByBundle($entity_type_id, $bundle) {
return $this->getDisplayModeOptionsByBundle('form_mode', $entity_type_id, $bundle);
}
/**
* Gets an array of display mode options.
*
* @param string $display_type
* The display type to be retrieved. It can be "view_mode" or "form_mode".
* @param string $entity_type_id
* The entity type whose display mode options should be returned.
*
* @return array
* An array of display mode labels, keyed by the display mode ID.
*/
protected function getDisplayModeOptions($display_type, $entity_type_id) {
$options = array('default' => t('Default'));
foreach ($this->getDisplayModesByEntityType($display_type, $entity_type_id) as $mode => $settings) {
$options[$mode] = $settings['label'];
}
return $options;
}
/**
* Returns an array of enabled display mode options by bundle.
*
* @param $display_type
* The display type to be retrieved. It can be "view_mode" or "form_mode".
* @param string $entity_type_id
* The entity type whose display mode options should be returned.
* @param string $bundle
* The name of the bundle.
*
* @return array
* An array of display mode labels, keyed by the display mode ID.
*/
protected function getDisplayModeOptionsByBundle($display_type, $entity_type_id, $bundle) {
// Collect all the entity's display modes.
$options = $this->getDisplayModeOptions($display_type, $entity_type_id);
// Filter out modes for which the entity display is disabled
// (or non-existent).
$load_ids = array();
// Get the list of available entity displays for the current bundle.
foreach (array_keys($options) as $mode) {
$load_ids[] = $entity_type_id . '.' . $bundle . '.' . $mode;
}
// Load the corresponding displays.
$displays = $this->entityTypeManager
->getStorage($display_type == 'form_mode' ? 'entity_form_display' : 'entity_view_display')
->loadMultiple($load_ids);
// Unset the display modes that are not active or do not exist.
foreach (array_keys($options) as $mode) {
$display_id = $entity_type_id . '.' . $bundle . '.' . $mode;
if (!isset($displays[$display_id]) || !$displays[$display_id]->status()) {
unset($options[$mode]);
}
}
return $options;
}
/**
* {@inheritdoc}
*/
public function clearDisplayModeInfo() {
$this->displayModeInfo = [];
return $this;
}
}
<?php
/**
* @file
* Contains \Drupal\Core\Entity\EntityDisplayRepositoryInterface.
*/
namespace Drupal\Core\Entity;
/**
* Provides an interface for an entity display repository.
*/
interface EntityDisplayRepositoryInterface {
/**
* Gets the entity view mode info for all entity types.
*
* @return array
* The view mode info for all entity types.
*/
public function getAllViewModes();
/**
* Gets the entity view mode info for a specific entity type.
*
* @param string $entity_type_id
* The entity type whose view mode info should be returned.
*
* @return array
* The view mode info for a specific entity type.
*/
public function getViewModes($entity_type_id);
/**
* Gets the entity form mode info for all entity types.
*
* @return array
* The form mode info for all entity types.
*/
public function getAllFormModes();
/**
* Gets the entity form mode info for a specific entity type.
*
* @param string $entity_type_id
* The entity type whose form mode info should be returned.
*
* @return array
* The form mode info for a specific entity type.
*/
public function getFormModes($entity_type_id);
/**
* Gets an array of view mode options.
*
* @param string $entity_type_id
* The entity type whose view mode options should be returned.
*
* @return array
* An array of view mode labels, keyed by the display mode ID.
*/
public function getViewModeOptions($entity_type_id);
/**
* Gets an array of form mode options.
*
* @param string $entity_type_id
* The entity type whose form mode options should be returned.
*
* @return array
* An array of form mode labels, keyed by the display mode ID.
*/
public function getFormModeOptions($entity_type_id);
/**
* Returns an array of enabled view mode options by bundle.
*
* @param string $entity_type_id
* The entity type whose view mode options should be returned.
* @param string $bundle
* The name of the bundle.
*
* @return array
* An array of view mode labels, keyed by the display mode ID.
*/
public function getViewModeOptionsByBundle($entity_type_id, $bundle);
/**
* Returns an array of enabled form mode options by bundle.
*
* @param string $entity_type_id
* The entity type whose form mode options should be returned.
* @param string $bundle
* The name of the bundle.
*
* @return array
* An array of form mode labels, keyed by the display mode ID.
*/
public function getFormModeOptionsByBundle($entity_type_id, $bundle);
/**
* Clears the gathered display mode info.
*
* @return $this
*/
public function clearDisplayModeInfo();
}
This diff is collapsed.
<
<?php
/**
* @file
* Contains \Drupal\Core\Entity\EntityFieldManagerInterface.
*/
namespace Drupal\Core\Entity;
/**
* Provides an interface for an entity field manager.
*/
interface EntityFieldManagerInterface {
/**
* Gets the base field definitions for a content entity type.
*
* Only fields that are not specific to a given bundle or set of bundles are
* returned. This excludes configurable fields, as they are always attached
* to a specific bundle.
*
* @param string $entity_type_id
* The entity type ID. Only entity types that implement
* \Drupal\Core\Entity\FieldableEntityInterface are supported.
*
* @return \Drupal\Core\Field\FieldDefinitionInterface[]
* The array of base field definitions for the entity type, keyed by field
* name.
*
* @throws \LogicException
* Thrown if one of the entity keys is flagged as translatable.
*/
public function getBaseFieldDefinitions($entity_type_id);
/**
* Gets the field definitions for a specific bundle.
*
* @param string $entity_type_id
* The entity type ID. Only entity types that implement
* \Drupal\Core\Entity\FieldableEntityInterface are supported.
* @param string $bundle
* The bundle.
*
* @return \Drupal\Core\Field\FieldDefinitionInterface[]
* The array of field definitions for the bundle, keyed by field name.
*/
public function getFieldDefinitions($entity_type_id, $bundle);
/**
* Gets the field storage definitions for a content entity type.
*
* This returns all field storage definitions for base fields and bundle
* fields of an entity type. Note that field storage definitions of a base
* field equal the full base field definition (i.e. they implement
* FieldDefinitionInterface), while the storage definitions for bundle fields
* may implement FieldStorageDefinitionInterface only.
*
* @param string $entity_type_id
* The entity type ID. Only content entities are supported.
*
* @return \Drupal\Core\Field\FieldStorageDefinitionInterface[]
* The array of field storage definitions for the entity type, keyed by
* field name.
*
* @see \Drupal\Core\Field\FieldStorageDefinitionInterface
*/
public function getFieldStorageDefinitions($entity_type_id);
/**
* Gets a lightweight map of fields across bundles.
*
* @return array
* An array keyed by entity type. Each value is an array which keys are
* field names and value is an array with two entries:
* - type: The field type.
* - bundles: The bundles in which the field appears.
*/
public function getFieldMap();
/**
* Sets a lightweight map of fields across bundles.
*
* @param array[] $field_map
* See the return value of self::getFieldMap().
*
* @return $this
*/
public function setFieldMap(array $field_map);
/**
* Gets a lightweight map of fields across bundles filtered by field type.
*
* @param string $field_type
* The field type to filter by.
*
* @return array
* An array keyed by entity type. Each value is an array which keys are
* field names and value is an array with two entries:
* - type: The field type.
* - bundles: The bundles in which the field appears.
*/
public function getFieldMapByFieldType($field_type);
/**
* Clears static and persistent field definition caches.
*/
public function clearCachedFieldDefinitions();
/**
* Disable the use of caches.
*
* @param bool $use_caches
* FALSE to not use any caches.
*
* @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0.
*
* @todo Remove in https://www.drupal.org/node/2549143.
*/
public function useCaches($use_caches = FALSE);