Update to Drupal 8.0.0-rc3. For more information, see https://www.drupal.org/node/2608078

This commit is contained in:
Pantheon Automation 2015-11-04 11:11:27 -08:00 committed by Greg Anderson
parent 6419a031d7
commit 4afb23bbd3
762 changed files with 20080 additions and 6368 deletions

View file

@ -166,7 +166,7 @@ abstract class ContentEntityBase extends Entity implements \IteratorAggregate, C
protected $validationRequired = FALSE;
/**
* Overrides Entity::__construct().
* {@inheritdoc}
*/
public function __construct(array $values, $entity_type, $bundle = FALSE, $translations = array()) {
$this->entityTypeId = $entity_type;
@ -998,7 +998,7 @@ abstract class ContentEntityBase extends Entity implements \IteratorAggregate, C
}
/**
* Overrides Entity::createDuplicate().
* {@inheritdoc}
*/
public function createDuplicate() {
if ($this->translations[$this->activeLangcode]['status'] == static::TRANSLATION_REMOVED) {

View file

@ -91,11 +91,11 @@ class EntityRouteEnhancer implements RouteEnhancerInterface {
* @param \Symfony\Component\HttpFoundation\Request $request
* The Request instance.
*
* @throws \RuntimeException
* Thrown when an entity of a type cannot be found in a route.
*
* @return array
* The modified defaults.
*
* @throws \RuntimeException
* Thrown when an entity of a type cannot be found in a route.
*/
protected function enhanceEntityView(array $defaults, Request $request) {
$defaults['_controller'] = '\Drupal\Core\Entity\Controller\EntityViewController::view';

View file

@ -159,7 +159,7 @@ abstract class Entity implements EntityInterface {
*/
public function urlInfo($rel = 'canonical', array $options = []) {
if ($this->id() === NULL) {
throw new EntityMalformedException(sprintf('The "%s" entity cannot have a URI as it does have an ID', $this->getEntityTypeId()));
throw new EntityMalformedException(sprintf('The "%s" entity cannot have a URI as it does not have an ID', $this->getEntityTypeId()));
}
// The links array might contain URI templates set in annotations.

View file

@ -13,6 +13,7 @@ use Drupal\Core\Entity\EntityDisplayPluginCollection;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Entity\EntityDisplayBase;
use Drupal\Core\TypedData\TranslatableInterface;
/**
* Configuration entity that contains display options for all components of a
@ -249,7 +250,18 @@ class EntityViewDisplay extends EntityDisplayBase implements EntityViewDisplayIn
$items = $grouped_items[$id];
/** @var \Drupal\Core\Access\AccessResultInterface $field_access */
$field_access = $items->access('view', NULL, TRUE);
$build_list[$id][$name] = $field_access->isAllowed() ? $formatter->view($items, $entity->language()->getId()) : [];
// The language of the field values to display is already determined
// in the incoming $entity. The formatter should build its output of
// those values using:
// - the entity language if the entity is translatable,
// - the current "content language" otherwise.
if ($entity instanceof TranslatableInterface && $entity->isTranslatable()) {
$view_langcode = $entity->language()->getId();
}
else {
$view_langcode = NULL;
}
$build_list[$id][$name] = $field_access->isAllowed() ? $formatter->view($items, $view_langcode) : [];
// Apply the field access cacheability metadata to the render array.
$this->renderer->addCacheableDependency($build_list[$id][$name], $field_access);
}

View file

@ -45,13 +45,13 @@ class EntityAutocompleteMatcher {
* @param string $string
* (optional) The label of the entity to query by.
*
* @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
* Thrown when the current user doesn't have access to the specifies entity.
*
* @return array
* An array of matched entity labels, in the format required by the AJAX
* autocomplete API (e.g. array('value' => $value, 'label' => $label)).
*
* @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
* Thrown when the current user doesn't have access to the specified entity.
*
* @see \Drupal\system\Controller\EntityAutocompleteController
*/
public function getMatches($target_type, $selection_handler, $selection_settings, $string = '') {

View file

@ -0,0 +1,93 @@
<?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();
}
}

View file

@ -0,0 +1,251 @@
<?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;
}
}

View file

@ -0,0 +1,108 @@
<?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();
}

View file

@ -0,0 +1,601 @@
<?php
/**
* @file
* Contains \Drupal\Core\Entity\EntityFieldManager.
*/
namespace Drupal\Core\Entity;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Cache\UseCacheBackendTrait;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\KeyValueStore\KeyValueFactoryInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\TypedData\TypedDataManagerInterface;
/**
* Manages the discovery of entity fields.
*
* This includes field definitions, base field definitions, and field storage
* definitions.
*/
class EntityFieldManager implements EntityFieldManagerInterface {
use UseCacheBackendTrait;
use StringTranslationTrait;
/**
* Extra fields by bundle.
*
* @var array
*/
protected $extraFields = [];
/**
* Static cache of base field definitions.
*
* @var array
*/
protected $baseFieldDefinitions;
/**
* Static cache of field definitions per bundle and entity type.
*
* @var array
*/
protected $fieldDefinitions;
/**
* Static cache of field storage definitions per entity type.
*
* Elements of the array:
* - $entity_type_id: \Drupal\Core\Field\BaseFieldDefinition[]
*
* @var array
*/
protected $fieldStorageDefinitions;
/**
* An array keyed by entity type. Each value is an array whose keys are
* field names and whose value is an array with two entries:
* - type: The field type.
* - bundles: The bundles in which the field appears.
*
* @return array
*/
protected $fieldMap = [];
/**
* An array keyed by field type. Each value is an array whose key are entity
* types including arrays in the same form that $fieldMap.
*
* It helps access the mapping between types and fields by the field type.
*
* @var array
*/
protected $fieldMapByFieldType = [];
/**
* The typed data manager.
*
* @var \Drupal\Core\TypedData\TypedDataManagerInterface
*/
protected $typedDataManager;
/**
* The language manager.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* The key-value factory.
*
* @var \Drupal\Core\KeyValueStore\KeyValueFactoryInterface
*/
protected $keyValueFactory;
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* 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 display repository.
*
* @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface
*/
protected $entityDisplayRepository;
/**
* Constructs a new EntityFieldManager.
*
* @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\EntityDisplayRepositoryInterface $entity_display_repository
* The entity display repository.
* @param \Drupal\Core\TypedData\TypedDataManagerInterface $typed_data_manager
* The typed data manager.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
* @param \Drupal\Core\KeyValueStore\KeyValueFactoryInterface $key_value_factory
* The key-value factory.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
* @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
* The cache backend.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info, EntityDisplayRepositoryInterface $entity_display_repository, TypedDataManagerInterface $typed_data_manager, LanguageManagerInterface $language_manager, KeyValueFactoryInterface $key_value_factory, ModuleHandlerInterface $module_handler, CacheBackendInterface $cache_backend) {
$this->entityTypeManager = $entity_type_manager;
$this->entityTypeBundleInfo = $entity_type_bundle_info;
$this->entityDisplayRepository = $entity_display_repository;
$this->typedDataManager = $typed_data_manager;
$this->languageManager = $language_manager;
$this->keyValueFactory = $key_value_factory;
$this->moduleHandler = $module_handler;
$this->cacheBackend = $cache_backend;
}
/**
* {@inheritdoc}
*/
public function getBaseFieldDefinitions($entity_type_id) {
// Check the static cache.
if (!isset($this->baseFieldDefinitions[$entity_type_id])) {
// Not prepared, try to load from cache.
$cid = 'entity_base_field_definitions:' . $entity_type_id . ':' . $this->languageManager->getCurrentLanguage()->getId();
if ($cache = $this->cacheGet($cid)) {
$this->baseFieldDefinitions[$entity_type_id] = $cache->data;
}
else {
// Rebuild the definitions and put it into the cache.
$this->baseFieldDefinitions[$entity_type_id] = $this->buildBaseFieldDefinitions($entity_type_id);
$this->cacheSet($cid, $this->baseFieldDefinitions[$entity_type_id], Cache::PERMANENT, ['entity_types', 'entity_field_info']);
}
}
return $this->baseFieldDefinitions[$entity_type_id];
}
/**
* Builds base field definitions for an entity type.
*
* @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[]
* An array of field definitions, keyed by field name.
*
* @throws \LogicException
* Thrown if a config entity type is given or if one of the entity keys is
* flagged as translatable.
*/
protected function buildBaseFieldDefinitions($entity_type_id) {
$entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
$class = $entity_type->getClass();
$keys = array_filter($entity_type->getKeys());
// Fail with an exception for non-fieldable entity types.
if (!$entity_type->isSubclassOf(FieldableEntityInterface::class)) {
throw new \LogicException("Getting the base fields is not supported for entity type {$entity_type->getLabel()}.");
}
// Retrieve base field definitions.
/** @var \Drupal\Core\Field\FieldStorageDefinitionInterface[] $base_field_definitions */
$base_field_definitions = $class::baseFieldDefinitions($entity_type);
// Make sure translatable entity types are correctly defined.
if ($entity_type->isTranslatable()) {
// The langcode field should always be translatable if the entity type is.
if (isset($keys['langcode']) && isset($base_field_definitions[$keys['langcode']])) {
$base_field_definitions[$keys['langcode']]->setTranslatable(TRUE);
}
// A default_langcode field should always be defined.
if (!isset($base_field_definitions[$keys['default_langcode']])) {
$base_field_definitions[$keys['default_langcode']] = BaseFieldDefinition::create('boolean')
->setLabel($this->t('Default translation'))
->setDescription($this->t('A flag indicating whether this is the default translation.'))
->setTranslatable(TRUE)
->setRevisionable(TRUE)
->setDefaultValue(TRUE);
}
}
// Assign base field definitions the entity type provider.
$provider = $entity_type->getProvider();
foreach ($base_field_definitions as $definition) {
// @todo Remove this check once FieldDefinitionInterface exposes a proper
// provider setter. See https://www.drupal.org/node/2225961.
if ($definition instanceof BaseFieldDefinition) {
$definition->setProvider($provider);
}
}
// Retrieve base field definitions from modules.
foreach ($this->moduleHandler->getImplementations('entity_base_field_info') as $module) {
$module_definitions = $this->moduleHandler->invoke($module, 'entity_base_field_info', [$entity_type]);
if (!empty($module_definitions)) {
// Ensure the provider key actually matches the name of the provider
// defining the field.
foreach ($module_definitions as $field_name => $definition) {
// @todo Remove this check once FieldDefinitionInterface exposes a
// proper provider setter. See https://www.drupal.org/node/2225961.
if ($definition instanceof BaseFieldDefinition && $definition->getProvider() == NULL) {
$definition->setProvider($module);
}
$base_field_definitions[$field_name] = $definition;
}
}
}
// Automatically set the field name, target entity type and bundle
// for non-configurable fields.
foreach ($base_field_definitions as $field_name => $base_field_definition) {
if ($base_field_definition instanceof BaseFieldDefinition) {
$base_field_definition->setName($field_name);
$base_field_definition->setTargetEntityTypeId($entity_type_id);
$base_field_definition->setTargetBundle(NULL);
}
}
// Invoke alter hook.
$this->moduleHandler->alter('entity_base_field_info', $base_field_definitions, $entity_type);
// Ensure defined entity keys are there and have proper revisionable and
// translatable values.
foreach (array_intersect_key($keys, array_flip(['id', 'revision', 'uuid', 'bundle'])) as $key => $field_name) {
if (!isset($base_field_definitions[$field_name])) {
throw new \LogicException("The $field_name field definition does not exist and it is used as $key entity key.");
}
if ($base_field_definitions[$field_name]->isRevisionable()) {
throw new \LogicException("The {$base_field_definitions[$field_name]->getLabel()} field cannot be revisionable as it is used as $key entity key.");
}
if ($base_field_definitions[$field_name]->isTranslatable()) {
throw new \LogicException("The {$base_field_definitions[$field_name]->getLabel()} field cannot be translatable as it is used as $key entity key.");
}
}
// Make sure translatable entity types define the "langcode" field properly.
if ($entity_type->isTranslatable() && (!isset($keys['langcode']) || !isset($base_field_definitions[$keys['langcode']]) || !$base_field_definitions[$keys['langcode']]->isTranslatable())) {
throw new \LogicException("The {$entity_type->getLabel()} entity type cannot be translatable as it does not define a translatable \"langcode\" field.");
}
return $base_field_definitions;
}
/**
* {@inheritdoc}
*/
public function getFieldDefinitions($entity_type_id, $bundle) {
if (!isset($this->fieldDefinitions[$entity_type_id][$bundle])) {
$base_field_definitions = $this->getBaseFieldDefinitions($entity_type_id);
// Not prepared, try to load from cache.
$cid = 'entity_bundle_field_definitions:' . $entity_type_id . ':' . $bundle . ':' . $this->languageManager->getCurrentLanguage()->getId();
if ($cache = $this->cacheGet($cid)) {
$bundle_field_definitions = $cache->data;
}
else {
// Rebuild the definitions and put it into the cache.
$bundle_field_definitions = $this->buildBundleFieldDefinitions($entity_type_id, $bundle, $base_field_definitions);
$this->cacheSet($cid, $bundle_field_definitions, Cache::PERMANENT, ['entity_types', 'entity_field_info']);
}
// Field definitions consist of the bundle specific overrides and the
// base fields, merge them together. Use array_replace() to replace base
// fields with by bundle overrides and keep them in order, append
// additional by bundle fields.
$this->fieldDefinitions[$entity_type_id][$bundle] = array_replace($base_field_definitions, $bundle_field_definitions);
}
return $this->fieldDefinitions[$entity_type_id][$bundle];
}
/**
* Builds field definitions for a specific bundle within an entity type.
*
* @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.
* @param \Drupal\Core\Field\FieldDefinitionInterface[] $base_field_definitions
* The list of base field definitions.
*
* @return \Drupal\Core\Field\FieldDefinitionInterface[]
* An array of bundle field definitions, keyed by field name. Does
* not include base fields.
*/
protected function buildBundleFieldDefinitions($entity_type_id, $bundle, array $base_field_definitions) {
$entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
$class = $entity_type->getClass();
// Allow the entity class to provide bundle fields and bundle-specific
// overrides of base fields.
$bundle_field_definitions = $class::bundleFieldDefinitions($entity_type, $bundle, $base_field_definitions);
// Load base field overrides from configuration. These take precedence over
// base field overrides returned above.
$base_field_override_ids = array_map(function($field_name) use ($entity_type_id, $bundle) {
return $entity_type_id . '.' . $bundle . '.' . $field_name;
}, array_keys($base_field_definitions));
$base_field_overrides = $this->entityTypeManager->getStorage('base_field_override')->loadMultiple($base_field_override_ids);
foreach ($base_field_overrides as $base_field_override) {
/** @var \Drupal\Core\Field\Entity\BaseFieldOverride $base_field_override */
$field_name = $base_field_override->getName();
$bundle_field_definitions[$field_name] = $base_field_override;
}
$provider = $entity_type->getProvider();
foreach ($bundle_field_definitions as $definition) {
// @todo Remove this check once FieldDefinitionInterface exposes a proper
// provider setter. See https://www.drupal.org/node/2225961.
if ($definition instanceof BaseFieldDefinition) {
$definition->setProvider($provider);
}
}
// Retrieve base field definitions from modules.
foreach ($this->moduleHandler->getImplementations('entity_bundle_field_info') as $module) {
$module_definitions = $this->moduleHandler->invoke($module, 'entity_bundle_field_info', [$entity_type, $bundle, $base_field_definitions]);
if (!empty($module_definitions)) {
// Ensure the provider key actually matches the name of the provider
// defining the field.
foreach ($module_definitions as $field_name => $definition) {
// @todo Remove this check once FieldDefinitionInterface exposes a
// proper provider setter. See https://www.drupal.org/node/2225961.
if ($definition instanceof BaseFieldDefinition) {
$definition->setProvider($module);
}
$bundle_field_definitions[$field_name] = $definition;
}
}
}
// Automatically set the field name, target entity type and bundle
// for non-configurable fields.
foreach ($bundle_field_definitions as $field_name => $field_definition) {
if ($field_definition instanceof BaseFieldDefinition) {
$field_definition->setName($field_name);
$field_definition->setTargetEntityTypeId($entity_type_id);
$field_definition->setTargetBundle($bundle);
}
}
// Invoke 'per bundle' alter hook.
$this->moduleHandler->alter('entity_bundle_field_info', $bundle_field_definitions, $entity_type, $bundle);
return $bundle_field_definitions;
}
/**
* {@inheritdoc}
*/
public function getFieldStorageDefinitions($entity_type_id) {
if (!isset($this->fieldStorageDefinitions[$entity_type_id])) {
$this->fieldStorageDefinitions[$entity_type_id] = [];
// Add all non-computed base fields.
foreach ($this->getBaseFieldDefinitions($entity_type_id) as $field_name => $definition) {
if (!$definition->isComputed()) {
$this->fieldStorageDefinitions[$entity_type_id][$field_name] = $definition;
}
}
// Not prepared, try to load from cache.
$cid = 'entity_field_storage_definitions:' . $entity_type_id . ':' . $this->languageManager->getCurrentLanguage()->getId();
if ($cache = $this->cacheGet($cid)) {
$field_storage_definitions = $cache->data;
}
else {
// Rebuild the definitions and put it into the cache.
$field_storage_definitions = $this->buildFieldStorageDefinitions($entity_type_id);
$this->cacheSet($cid, $field_storage_definitions, Cache::PERMANENT, ['entity_types', 'entity_field_info']);
}
$this->fieldStorageDefinitions[$entity_type_id] += $field_storage_definitions;
}
return $this->fieldStorageDefinitions[$entity_type_id];
}
/**
* {@inheritdoc}
*/
public function setFieldMap(array $field_map) {
$this->fieldMap = $field_map;
return $this;
}
/**
* {@inheritdoc}
*/
public function getFieldMap() {
if (!$this->fieldMap) {
// Not prepared, try to load from cache.
$cid = 'entity_field_map';
if ($cache = $this->cacheGet($cid)) {
$this->fieldMap = $cache->data;
}
else {
// The field map is built in two steps. First, add all base fields, by
// looping over all fieldable entity types. They always exist for all
// bundles, and we do not expect to have so many different entity
// types for this to become a bottleneck.
foreach ($this->entityTypeManager->getDefinitions() as $entity_type_id => $entity_type) {
if ($entity_type->isSubclassOf(FieldableEntityInterface::class)) {
$bundles = array_keys($this->entityTypeBundleInfo->getBundleInfo($entity_type_id));
foreach ($this->getBaseFieldDefinitions($entity_type_id) as $field_name => $base_field_definition) {
$this->fieldMap[$entity_type_id][$field_name] = [
'type' => $base_field_definition->getType(),
'bundles' => array_combine($bundles, $bundles),
];
}
}
}
// In the second step, the per-bundle fields are added, based on the
// persistent bundle field map stored in a key value collection. This
// data is managed in the EntityManager::onFieldDefinitionCreate()
// and EntityManager::onFieldDefinitionDelete() methods. Rebuilding this
// information in the same way as base fields would not scale, as the
// time to query would grow exponentially with more fields and bundles.
// A cache would be deleted during cache clears, which is the only time
// it is needed, so a key value collection is used.
$bundle_field_maps = $this->keyValueFactory->get('entity.definitions.bundle_field_map')->getAll();
foreach ($bundle_field_maps as $entity_type_id => $bundle_field_map) {
foreach ($bundle_field_map as $field_name => $map_entry) {
if (!isset($this->fieldMap[$entity_type_id][$field_name])) {
$this->fieldMap[$entity_type_id][$field_name] = $map_entry;
}
else {
$this->fieldMap[$entity_type_id][$field_name]['bundles'] += $map_entry['bundles'];
}
}
}
$this->cacheSet($cid, $this->fieldMap, Cache::PERMANENT, ['entity_types']);
}
}
return $this->fieldMap;
}
/**
* {@inheritdoc}
*/
public function getFieldMapByFieldType($field_type) {
if (!isset($this->fieldMapByFieldType[$field_type])) {
$filtered_map = [];
$map = $this->getFieldMap();
foreach ($map as $entity_type => $fields) {
foreach ($fields as $field_name => $field_info) {
if ($field_info['type'] == $field_type) {
$filtered_map[$entity_type][$field_name] = $field_info;
}
}
}
$this->fieldMapByFieldType[$field_type] = $filtered_map;
}
return $this->fieldMapByFieldType[$field_type];
}
/**
* Builds field storage definitions for an entity type.
*
* @param string $entity_type_id
* The entity type ID. Only entity types that implement
* \Drupal\Core\Entity\FieldableEntityInterface are supported
*
* @return \Drupal\Core\Field\FieldStorageDefinitionInterface[]
* An array of field storage definitions, keyed by field name.
*/
protected function buildFieldStorageDefinitions($entity_type_id) {
$entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
$field_definitions = [];
// Retrieve base field definitions from modules.
foreach ($this->moduleHandler->getImplementations('entity_field_storage_info') as $module) {
$module_definitions = $this->moduleHandler->invoke($module, 'entity_field_storage_info', [$entity_type]);
if (!empty($module_definitions)) {
// Ensure the provider key actually matches the name of the provider
// defining the field.
foreach ($module_definitions as $field_name => $definition) {
// @todo Remove this check once FieldDefinitionInterface exposes a
// proper provider setter. See https://www.drupal.org/node/2225961.
if ($definition instanceof BaseFieldDefinition) {
$definition->setProvider($module);
}
$field_definitions[$field_name] = $definition;
}
}
}
// Invoke alter hook.
$this->moduleHandler->alter('entity_field_storage_info', $field_definitions, $entity_type);
return $field_definitions;
}
/**
* {@inheritdoc}
*/
public function clearCachedFieldDefinitions() {
$this->baseFieldDefinitions = [];
$this->fieldDefinitions = [];
$this->fieldStorageDefinitions = [];
$this->fieldMap = [];
$this->fieldMapByFieldType = [];
$this->entityDisplayRepository->clearDisplayModeInfo();
$this->extraFields = [];
Cache::invalidateTags(['entity_field_info']);
// The typed data manager statically caches prototype objects with injected
// definitions, clear those as well.
$this->typedDataManager->clearCachedDefinitions();
}
/**
* {@inheritdoc}
*/
public function useCaches($use_caches = FALSE) {
$this->useCaches = $use_caches;
if (!$use_caches) {
$this->fieldDefinitions = [];
$this->baseFieldDefinitions = [];
$this->fieldStorageDefinitions = [];
}
}
/**
* {@inheritdoc}
*/
public function getExtraFields($entity_type_id, $bundle) {
// Read from the "static" cache.
if (isset($this->extraFields[$entity_type_id][$bundle])) {
return $this->extraFields[$entity_type_id][$bundle];
}
// Read from the persistent cache. Since hook_entity_extra_field_info() and
// hook_entity_extra_field_info_alter() might contain t() calls, we cache
// per language.
$cache_id = 'entity_bundle_extra_fields:' . $entity_type_id . ':' . $bundle . ':' . $this->languageManager->getCurrentLanguage()->getId();
$cached = $this->cacheGet($cache_id);
if ($cached) {
$this->extraFields[$entity_type_id][$bundle] = $cached->data;
return $this->extraFields[$entity_type_id][$bundle];
}
$extra = $this->moduleHandler->invokeAll('entity_extra_field_info');
$this->moduleHandler->alter('entity_extra_field_info', $extra);
$info = isset($extra[$entity_type_id][$bundle]) ? $extra[$entity_type_id][$bundle] : [];
$info += [
'form' => [],
'display' => [],
];
// Store in the 'static' and persistent caches.
$this->extraFields[$entity_type_id][$bundle] = $info;
$this->cacheSet($cache_id, $info, Cache::PERMANENT, [
'entity_field_info',
]);
return $this->extraFields[$entity_type_id][$bundle];
}
}

View file

@ -0,0 +1,150 @@
<?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);
/**
* Gets the "extra fields" for a bundle.
*
* @param string $entity_type_id
* The entity type ID.
* @param string $bundle
* The bundle name.
*
* @return array
* A nested array of 'pseudo-field' elements. Each list is nested within the
* following keys: entity type, bundle name, context (either 'form' or
* 'display'). The keys are the name of the elements as appearing in the
* renderable array (either the entity form or the displayed entity). The
* value is an associative array:
* - label: The human readable name of the element. Make sure you sanitize
* this appropriately.
* - description: A short description of the element contents.
* - weight: The default weight of the element.
* - visible: (optional) The default visibility of the element. Defaults to
* TRUE.
* - edit: (optional) String containing markup (normally a link) used as the
* element's 'edit' operation in the administration interface. Only for
* 'form' context.
* - delete: (optional) String containing markup (normally a link) used as the
* element's 'delete' operation in the administration interface. Only for
* 'form' context.
*/
public function getExtraFields($entity_type_id, $bundle);
}

View file

@ -41,9 +41,18 @@ class EntityForm extends FormBase implements EntityFormInterface {
* The entity manager.
*
* @var \Drupal\Core\Entity\EntityManagerInterface
*
* @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0.
*/
protected $entityManager;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The entity being used by this form.
*
@ -404,4 +413,12 @@ class EntityForm extends FormBase implements EntityFormInterface {
return $this;
}
/**
* {@inheritdoc}
*/
public function setEntityTypeManager(EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
return $this;
}
}

View file

@ -137,7 +137,21 @@ interface EntityFormInterface extends BaseFormIdInterface {
* The entity manager.
*
* @return $this
*
* @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0.
*
* @todo Remove this set call in https://www.drupal.org/node/2603542.
*/
public function setEntityManager(EntityManagerInterface $entity_manager);
/**
* Sets the entity type manager for this form.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*
* @return $this
*/
public function setEntityTypeManager(EntityTypeManagerInterface $entity_type_manager);
}

View file

@ -0,0 +1,97 @@
<?php
/**
* @file
* Contains \Drupal\Core\Entity\EntityLastInstalledSchemaRepository.
*/
namespace Drupal\Core\Entity;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\KeyValueStore\KeyValueFactoryInterface;
/**
* Provides a repository for installed entity definitions.
*/
class EntityLastInstalledSchemaRepository implements EntityLastInstalledSchemaRepositoryInterface {
/**
* The key-value factory.
*
* @var \Drupal\Core\KeyValueStore\KeyValueFactoryInterface
*/
protected $keyValueFactory;
/**
* Constructs a new EntityLastInstalledSchemaRepository.
*
* @param \Drupal\Core\KeyValueStore\KeyValueFactoryInterface $key_value_factory
* The key-value factory.
*/
public function __construct(KeyValueFactoryInterface $key_value_factory) {
$this->keyValueFactory = $key_value_factory;
}
/**
* {@inheritdoc}
*/
public function getLastInstalledDefinition($entity_type_id) {
return $this->keyValueFactory->get('entity.definitions.installed')->get($entity_type_id . '.entity_type');
}
/**
* {@inheritdoc}
*/
public function setLastInstalledDefinition(EntityTypeInterface $entity_type) {
$entity_type_id = $entity_type->id();
$this->keyValueFactory->get('entity.definitions.installed')->set($entity_type_id . '.entity_type', $entity_type);
return $this;
}
/**
* {@inheritdoc}
*/
public function deleteLastInstalledDefinition($entity_type_id) {
$this->keyValueFactory->get('entity.definitions.installed')->delete($entity_type_id . '.entity_type');
// Clean up field storage definitions as well. Even if the entity type
// isn't currently fieldable, there might be legacy definitions or an
// empty array stored from when it was.
$this->keyValueFactory->get('entity.definitions.installed')->delete($entity_type_id . '.field_storage_definitions');
return $this;
}
/**
* {@inheritdoc}
*/
public function getLastInstalledFieldStorageDefinitions($entity_type_id) {
return $this->keyValueFactory->get('entity.definitions.installed')->get($entity_type_id . '.field_storage_definitions', []);
}
/**
* {@inheritdoc}
*/
public function setLastInstalledFieldStorageDefinitions($entity_type_id, array $storage_definitions) {
$this->keyValueFactory->get('entity.definitions.installed')->set($entity_type_id . '.field_storage_definitions', $storage_definitions);
}
/**
* {@inheritdoc}
*/
public function setLastInstalledFieldStorageDefinition(FieldStorageDefinitionInterface $storage_definition) {
$entity_type_id = $storage_definition->getTargetEntityTypeId();
$definitions = $this->getLastInstalledFieldStorageDefinitions($entity_type_id);
$definitions[$storage_definition->getName()] = $storage_definition;
$this->setLastInstalledFieldStorageDefinitions($entity_type_id, $definitions);
}
/**
* {@inheritdoc}
*/
public function deleteLastInstalledFieldStorageDefinition(FieldStorageDefinitionInterface $storage_definition) {
$entity_type_id = $storage_definition->getTargetEntityTypeId();
$definitions = $this->getLastInstalledFieldStorageDefinitions($entity_type_id);
unset($definitions[$storage_definition->getName()]);
$this->setLastInstalledFieldStorageDefinitions($entity_type_id, $definitions);
}
}

View file

@ -0,0 +1,130 @@
<?php
/**
* @file
* Contains \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface.
*/
namespace Drupal\Core\Entity;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
/**
* Provides an interface for an installed entity definition repository.
*/
interface EntityLastInstalledSchemaRepositoryInterface {
/**
* Gets the entity type definition in its most recently installed state.
*
* During the application lifetime, entity type definitions can change. For
* example, updated code can be deployed. The getDefinition() method will
* always return the definition as determined by the current codebase. This
* method, however, returns what the definition was when the last time that
* one of the \Drupal\Core\Entity\EntityTypeListenerInterface events was last
* fired and completed successfully. In other words, the definition that
* the entity type's handlers have incorporated into the application state.
* For example, if the entity type's storage handler is SQL-based, the
* definition for which database tables were created.
*
* Application management code can check if getDefinition() differs from
* getLastInstalledDefinition() and decide whether to:
* - Invoke the appropriate \Drupal\Core\Entity\EntityTypeListenerInterface
* event so that handlers react to the new definition.
* - Raise a warning that the application state is incompatible with the
* codebase.
* - Perform some other action.
*
* @param string $entity_type_id
* The entity type ID.
*
* @return \Drupal\Core\Entity\EntityTypeInterface|null
* The installed entity type definition, or NULL if the entity type has
* not yet been installed via onEntityTypeCreate().
*
* @see \Drupal\Core\Entity\EntityTypeListenerInterface
*/
public function getLastInstalledDefinition($entity_type_id);
/**
* Stores the entity type definition in the application state.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type definition.
*
* @return $this
*/
public function setLastInstalledDefinition(EntityTypeInterface $entity_type);
/**
* Deletes the entity type definition from the application state.
*
* @param string $entity_type_id
* The entity type definition identifier.
*
* @return $this
*/
public function deleteLastInstalledDefinition($entity_type_id);
/**
* Gets the entity type's most recently installed field storage definitions.
*
* During the application lifetime, field storage definitions can change. For
* example, updated code can be deployed. The getFieldStorageDefinitions()
* method will always return the definitions as determined by the current
* codebase. This method, however, returns what the definitions were when the
* last time that one of the
* \Drupal\Core\Field\FieldStorageDefinitionListenerInterface events was last
* fired and completed successfully. In other words, the definitions that
* the entity type's handlers have incorporated into the application state.
* For example, if the entity type's storage handler is SQL-based, the
* definitions for which database tables were created.
*
* Application management code can check if getFieldStorageDefinitions()
* differs from getLastInstalledFieldStorageDefinitions() and decide whether
* to:
* - Invoke the appropriate
* \Drupal\Core\Field\FieldStorageDefinitionListenerInterface
* events so that handlers react to the new definitions.
* - Raise a warning that the application state is incompatible with the
* codebase.
* - Perform some other action.
*
* @param string $entity_type_id
* The entity type ID.
*
* @return \Drupal\Core\Field\FieldStorageDefinitionInterface[]
* The array of installed field storage definitions for the entity type,
* keyed by field name.
*
* @see \Drupal\Core\Entity\EntityTypeListenerInterface
*/
public function getLastInstalledFieldStorageDefinitions($entity_type_id);
/**
* Stores the entity type's field storage definitions in the application state.
*
* @param string $entity_type_id
* The entity type identifier.
* @param \Drupal\Core\Field\FieldStorageDefinitionInterface[] $storage_definitions
* An array of field storage definitions.
*/
public function setLastInstalledFieldStorageDefinitions($entity_type_id, array $storage_definitions);
/**
* Stores the field storage definition in the application state.
*
* @param \Drupal\Core\Field\FieldStorageDefinitionInterface $storage_definition
* The field storage definition.
*/
public function setLastInstalledFieldStorageDefinition(FieldStorageDefinitionInterface $storage_definition);
/**
* Deletes the field storage definition from the application state.
*
* @param \Drupal\Core\Field\FieldStorageDefinitionInterface $storage_definition
* The field storage definition.
*/
public function deleteLastInstalledFieldStorageDefinition(FieldStorageDefinitionInterface $storage_definition);
}

File diff suppressed because it is too large Load diff

View file

@ -7,534 +7,28 @@
namespace Drupal\Core\Entity;
use Drupal\Component\Plugin\Discovery\CachedDiscoveryInterface;
use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\Core\Field\FieldDefinitionListenerInterface;
use Drupal\Core\Field\FieldStorageDefinitionListenerInterface;
/**
* Provides an interface for entity type managers.
*
* @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0.
*/
interface EntityManagerInterface extends PluginManagerInterface, EntityTypeListenerInterface, EntityBundleListenerInterface, FieldStorageDefinitionListenerInterface, FieldDefinitionListenerInterface, CachedDiscoveryInterface {
interface EntityManagerInterface extends EntityTypeListenerInterface, EntityBundleListenerInterface, FieldStorageDefinitionListenerInterface, FieldDefinitionListenerInterface, EntityTypeManagerInterface, EntityTypeRepositoryInterface, EntityTypeBundleInfoInterface, EntityDisplayRepositoryInterface, EntityFieldManagerInterface, EntityRepositoryInterface {
/**
* Builds a list of entity type labels suitable for a Form API options list.
* @see \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface::getLastInstalledDefinition()
*
* @param bool $group
* (optional) Whether to group entity types by plugin group (e.g. 'content',
* 'config'). Defaults to FALSE.
*
* @return array
* An array of entity type labels, keyed by entity type name.
*/
public function getEntityTypeLabels($group = FALSE);
/**
* 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 the entity type's most recently installed field storage definitions.
*
* During the application lifetime, field storage definitions can change. For
* example, updated code can be deployed. The getFieldStorageDefinitions()
* method will always return the definitions as determined by the current
* codebase. This method, however, returns what the definitions were when the
* last time that one of the
* \Drupal\Core\Field\FieldStorageDefinitionListenerInterface events was last
* fired and completed successfully. In other words, the definitions that
* the entity type's handlers have incorporated into the application state.
* For example, if the entity type's storage handler is SQL-based, the
* definitions for which database tables were created.
*
* Application management code can check if getFieldStorageDefinitions()
* differs from getLastInstalledFieldStorageDefinitions() and decide whether
* to:
* - Invoke the appropriate
* \Drupal\Core\Field\FieldStorageDefinitionListenerInterface
* events so that handlers react to the new definitions.
* - Raise a warning that the application state is incompatible with the
* codebase.
* - Perform some other action.
*
* @param string $entity_type_id
* The entity type ID.
*
* @return \Drupal\Core\Field\FieldStorageDefinitionInterface[]
* The array of installed field storage definitions for the entity type,
* keyed by field name.
*
* @see \Drupal\Core\Entity\EntityTypeListenerInterface
*/
public function getLastInstalledFieldStorageDefinitions($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();
/**
* 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);
/**
* Creates a new access control handler instance.
*
* @param string $entity_type
* The entity type for this access control handler.
*
* @return \Drupal\Core\Entity\EntityAccessControlHandlerInterface.
* A access control handler instance.
*/
public function getAccessControlHandler($entity_type);
/**
* Creates a new storage instance.
*
* @param string $entity_type
* The entity type for this storage.
*
* @return \Drupal\Core\Entity\EntityStorageInterface
* A storage instance.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
*/
public function getStorage($entity_type);
/**
* Get the bundle info of all entity types.
*
* @return array
* An array of all bundle information.
*/
public function getAllBundleInfo();
/**
* {@inheritdoc}
*/
public function clearCachedDefinitions();
/**
* Clears static and persistent field definition caches.
*/
public function clearCachedFieldDefinitions();
/**
* Clears static and persistent bundles.
*/
public function clearCachedBundles();
/**
* Creates a new view builder instance.
*
* @param string $entity_type
* The entity type for this view builder.
*
* @return \Drupal\Core\Entity\EntityViewBuilderInterface.
* A view builder instance.
*/
public function getViewBuilder($entity_type);
/**
* Creates a new entity list builder.
*
* @param string $entity_type
* The entity type for this list builder.
*
* @return \Drupal\Core\Entity\EntityListBuilderInterface
* An entity list builder instance.
*/
public function getListBuilder($entity_type);
/**
* Creates a new form instance.
*
* @param string $entity_type
* The entity type for this form.
* @param string $operation
* The name of the operation to use, e.g., 'default'.
*
* @return \Drupal\Core\Entity\EntityFormInterface
* A form instance.
*/
public function getFormObject($entity_type, $operation);
/**
* Gets all route provider instances.
*
* @param string $entity_type
* The entity type for this route providers.
*
* @return \Drupal\Core\Entity\Routing\EntityRouteProviderInterface[]
*/
public function getRouteProviders($entity_type);
/**
* Checks whether a certain entity type has a certain handler.
*
* @param string $entity_type
* The name of the entity type.
* @param string $handler_type
* The name of the handler.
*
* @return bool
* Returns TRUE if the entity type has the handler, else FALSE.
*/
public function hasHandler($entity_type, $handler_type);
/**
* Creates a new handler instance for a entity type and handler type.
*
* @param string $entity_type
* The entity type for this handler.
* @param string $handler_type
* The handler type to create an instance for.
*
* @return object
* A handler instance.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
*/
public function getHandler($entity_type, $handler_type);
/**
* Creates new handler instance.
*
* Usually \Drupal\Core\Entity\EntityManagerInterface::getHandler() is
* preferred since that method has additional checking that the class exists
* and has static caches.
*
* @param mixed $class
* The handler class to instantiate.
* @param \Drupal\Core\Entity\EntityTypeInterface $definition
* The entity type definition.
*
* @return object
* A handler instance.
*/
public function createHandlerInstance($class, EntityTypeInterface $definition = null);
/**
* Gets the bundle info of an entity type.
*
* @param string $entity_type
* The entity type.
*
* @return array
* Returns the bundle information for the specified entity type.
*/
public function getBundleInfo($entity_type);
/**
* Gets the "extra fields" for a bundle.
*
* @param string $entity_type_id
* The entity type ID.
* @param string $bundle
* The bundle name.
*
* @return array
* A nested array of 'pseudo-field' elements. Each list is nested within the
* following keys: entity type, bundle name, context (either 'form' or
* 'display'). The keys are the name of the elements as appearing in the
* renderable array (either the entity form or the displayed entity). The
* value is an associative array:
* - label: The human readable name of the element. Make sure you sanitize
* this appropriately.
* - description: A short description of the element contents.
* - weight: The default weight of the element.
* - visible: (optional) The default visibility of the element. Defaults to
* TRUE.
* - edit: (optional) String containing markup (normally a link) used as the
* element's 'edit' operation in the administration interface. Only for
* 'form' context.
* - delete: (optional) String containing markup (normally a link) used as the
* element's 'delete' operation in the administration interface. Only for
* 'form' context.
*/
public function getExtraFields($entity_type_id, $bundle);
/**
* Gets the entity translation to be used in the given context.
*
* This will check whether a translation for the desired language is available
* and if not, it will fall back to the most appropriate translation based on
* the provided context.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity whose translation will be returned.
* @param string $langcode
* (optional) The language of the current context. Defaults to the current
* content language.
* @param array $context
* (optional) An associative array of arbitrary data that can be useful to
* determine the proper fallback sequence.
*
* @return \Drupal\Core\Entity\EntityInterface
* An entity object for the translated data.
*
* @see \Drupal\Core\Language\LanguageManagerInterface::getFallbackCandidates()
*/
public function getTranslationFromContext(EntityInterface $entity, $langcode = NULL, $context = array());
/**
* {@inheritdoc}
*
* @return \Drupal\Core\Entity\EntityTypeInterface|null
*/
public function getDefinition($entity_type_id, $exception_on_invalid = TRUE);
/**
* Gets the entity type definition in its most recently installed state.
*
* During the application lifetime, entity type definitions can change. For
* example, updated code can be deployed. The getDefinition() method will
* always return the definition as determined by the current codebase. This
* method, however, returns what the definition was when the last time that
* one of the \Drupal\Core\Entity\EntityTypeListenerInterface events was last
* fired and completed successfully. In other words, the definition that
* the entity type's handlers have incorporated into the application state.
* For example, if the entity type's storage handler is SQL-based, the
* definition for which database tables were created.
*
* Application management code can check if getDefinition() differs from
* getLastInstalledDefinition() and decide whether to:
* - Invoke the appropriate \Drupal\Core\Entity\EntityTypeListenerInterface
* event so that handlers react to the new definition.
* - Raise a warning that the application state is incompatible with the
* codebase.
* - Perform some other action.
*
* @param string $entity_type_id
* The entity type ID.
*
* @return \Drupal\Core\Entity\EntityTypeInterface|null
* The installed entity type definition, or NULL if the entity type has
* not yet been installed via onEntityTypeCreate().
*
* @see \Drupal\Core\Entity\EntityTypeListenerInterface
* @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0.
*/
public function getLastInstalledDefinition($entity_type_id);
/**
* {@inheritdoc}
* @see \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface::getLastInstalledFieldStorageDefinitions()
*
* @return \Drupal\Core\Entity\EntityTypeInterface[]
* @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0.
*/
public function getDefinitions();
/**
* 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);
/**
* Loads an entity by UUID.
*
* Note that some entity types may not support UUIDs.
*
* @param string $entity_type_id
* The entity type ID to load from.
* @param string $uuid
* The UUID of the entity to load.
*
* @return \Drupal\Core\Entity\EntityInterface|null
* The entity object, or NULL if there is no entity with the given UUID.
*
* @throws \Drupal\Core\Entity\EntityStorageException
* Thrown in case the requested entity type does not support UUIDs.
*/
public function loadEntityByUuid($entity_type_id, $uuid);
/**
* Loads an entity by the config target identifier.
*
* @param string $entity_type_id
* The entity type ID to load from.
* @param string $target
* The configuration target to load, as returned from
* \Drupal\Core\Entity\EntityInterface::getConfigTarget().
*
* @return \Drupal\Core\Entity\EntityInterface|null
* The entity object, or NULL if there is no entity with the given config
* target identifier.
*
* @throws \Drupal\Core\Entity\EntityStorageException
* Thrown if the target identifier is a UUID but the entity type does not
* support UUIDs.
*
* @see \Drupal\Core\Entity\EntityInterface::getConfigTarget()
*/
public function loadEntityByConfigTarget($entity_type_id, $target);
/**
* Gets the entity type ID based on the class that is called on.
*
* Compares the class this is called on against the known entity classes
* and returns the entity type ID of a direct match or a subclass as fallback,
* to support entity type definitions that were altered.
*
* @param string $class_name
* Class name to use for searching the entity type ID.
*
* @return string
* The entity type ID.
*
* @throws \Drupal\Core\Entity\Exception\AmbiguousEntityClassException
* Thrown when multiple subclasses correspond to the called class.
* @throws \Drupal\Core\Entity\Exception\NoCorrespondingEntityClassException
* Thrown when no entity class corresponds to the called class.
*
* @see \Drupal\Core\Entity\Entity::load()
* @see \Drupal\Core\Entity\Entity::loadMultiple()
*/
public function getEntityTypeFromClass($class_name);
public function getLastInstalledFieldStorageDefinitions($entity_type_id);
}

View file

@ -0,0 +1,120 @@
<?php
/**
* @file
* Contains \Drupal\Core\Entity\EntityRepository.
*/
namespace Drupal\Core\Entity;
use Drupal\Core\Config\Entity\ConfigEntityTypeInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\TypedData\TranslatableInterface;
/**
* Provides several mechanisms for retrieving entities.
*/
class EntityRepository implements EntityRepositoryInterface {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The language manager.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* Constructs a new EntityRepository.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, LanguageManagerInterface $language_manager) {
$this->entityTypeManager = $entity_type_manager;
$this->languageManager = $language_manager;
}
/**
* {@inheritdoc}
*/
public function loadEntityByUuid($entity_type_id, $uuid) {
$entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
if (!$uuid_key = $entity_type->getKey('uuid')) {
throw new EntityStorageException("Entity type $entity_type_id does not support UUIDs.");
}
$entities = $this->entityTypeManager->getStorage($entity_type_id)->loadByProperties([$uuid_key => $uuid]);
return reset($entities);
}
/**
* {@inheritdoc}
*/
public function loadEntityByConfigTarget($entity_type_id, $target) {
$entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
// For configuration entities, the config target is given by the entity ID.
// @todo Consider adding a method to allow entity types to indicate the
// target identifier key rather than hard-coding this check. Issue:
// https://www.drupal.org/node/2412983.
if ($entity_type instanceof ConfigEntityTypeInterface) {
$entity = $this->entityTypeManager->getStorage($entity_type_id)->load($target);
}
// For content entities, the config target is given by the UUID.
else {
$entity = $this->loadEntityByUuid($entity_type_id, $target);
}
return $entity;
}
/**
* {@inheritdoc}
*/
public function getTranslationFromContext(EntityInterface $entity, $langcode = NULL, $context = array()) {
$translation = $entity;
if ($entity instanceof TranslatableInterface && count($entity->getTranslationLanguages()) > 1) {
if (empty($langcode)) {
$langcode = $this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId();
$entity->addCacheContexts(['languages:' . LanguageInterface::TYPE_CONTENT]);
}
// Retrieve language fallback candidates to perform the entity language
// negotiation, unless the current translation is already the desired one.
if ($entity->language()->getId() != $langcode) {
$context['data'] = $entity;
$context += array('operation' => 'entity_view', 'langcode' => $langcode);
$candidates = $this->languageManager->getFallbackCandidates($context);
// Ensure the default language has the proper language code.
$default_language = $entity->getUntranslated()->language();
$candidates[$default_language->getId()] = LanguageInterface::LANGCODE_DEFAULT;
// Return the most fitting entity translation.
foreach ($candidates as $candidate) {
if ($entity->hasTranslation($candidate)) {
$translation = $entity->getTranslation($candidate);
break;
}
}
}
}
return $translation;
}
}

View file

@ -0,0 +1,77 @@
<?php
/**
* @file
* Contains \Drupal\Core\Entity\EntityRepositoryInterface.
*/
namespace Drupal\Core\Entity;
/**
* Provides an interface for an entity repository.
*/
interface EntityRepositoryInterface {
/**
* Loads an entity by UUID.
*
* Note that some entity types may not support UUIDs.
*
* @param string $entity_type_id
* The entity type ID to load from.
* @param string $uuid
* The UUID of the entity to load.
*
* @return \Drupal\Core\Entity\EntityInterface|null
* The entity object, or NULL if there is no entity with the given UUID.
*
* @throws \Drupal\Core\Entity\EntityStorageException
* Thrown in case the requested entity type does not support UUIDs.
*/
public function loadEntityByUuid($entity_type_id, $uuid);
/**
* Loads an entity by the config target identifier.
*
* @param string $entity_type_id
* The entity type ID to load from.
* @param string $target
* The configuration target to load, as returned from
* \Drupal\Core\Entity\EntityInterface::getConfigTarget().
*
* @return \Drupal\Core\Entity\EntityInterface|null
* The entity object, or NULL if there is no entity with the given config
* target identifier.
*
* @throws \Drupal\Core\Entity\EntityStorageException
* Thrown if the target identifier is a UUID but the entity type does not
* support UUIDs.
*
* @see \Drupal\Core\Entity\EntityInterface::getConfigTarget()
*/
public function loadEntityByConfigTarget($entity_type_id, $target);
/**
* Gets the entity translation to be used in the given context.
*
* This will check whether a translation for the desired language is available
* and if not, it will fall back to the most appropriate translation based on
* the provided context.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity whose translation will be returned.
* @param string $langcode
* (optional) The language of the current context. Defaults to the current
* content language.
* @param array $context
* (optional) An associative array of arbitrary data that can be useful to
* determine the proper fallback sequence.
*
* @return \Drupal\Core\Entity\EntityInterface
* An entity object for the translated data.
*
* @see \Drupal\Core\Language\LanguageManagerInterface::getFallbackCandidates()
*/
public function getTranslationFromContext(EntityInterface $entity, $langcode = NULL, $context = array());
}

View file

@ -0,0 +1,133 @@
<?php
/**
* @file
* Contains \Drupal\Core\Entity\EntityTypeBundleInfo.
*/
namespace Drupal\Core\Entity;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Cache\UseCacheBackendTrait;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\TypedData\TypedDataManagerInterface;
/**
* Provides discovery and retrieval of entity type bundles.
*/
class EntityTypeBundleInfo implements EntityTypeBundleInfoInterface {
use UseCacheBackendTrait;
/**
* Static cache of bundle information.
*
* @var array
*/
protected $bundleInfo;
/**
* The language manager.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* The typed data manager.
*
* @var \Drupal\Core\TypedData\TypedDataManagerInterface
*/
protected $typedDataManager;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Constructs a new EntityTypeBundleInfo.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
* @param \Drupal\Core\TypedData\TypedDataManagerInterface $typed_data_manager
* The typed data manager.
* @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
* The cache backend.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, LanguageManagerInterface $language_manager, ModuleHandlerInterface $module_handler, TypedDataManagerInterface $typed_data_manager, CacheBackendInterface $cache_backend) {
$this->entityTypeManager = $entity_type_manager;
$this->languageManager = $language_manager;
$this->moduleHandler = $module_handler;
$this->typedDataManager = $typed_data_manager;
$this->cacheBackend = $cache_backend;
}
/**
* {@inheritdoc}
*/
public function getBundleInfo($entity_type) {
$bundle_info = $this->getAllBundleInfo();
return isset($bundle_info[$entity_type]) ? $bundle_info[$entity_type] : [];
}
/**
* {@inheritdoc}
*/
public function getAllBundleInfo() {
if (empty($this->bundleInfo)) {
$langcode = $this->languageManager->getCurrentLanguage()->getId();
if ($cache = $this->cacheGet("entity_bundle_info:$langcode")) {
$this->bundleInfo = $cache->data;
}
else {
$this->bundleInfo = $this->moduleHandler->invokeAll('entity_bundle_info');
// First look for entity types that act as bundles for others, load them
// and add them as bundles.
foreach ($this->entityTypeManager->getDefinitions() as $type => $entity_type) {
if ($entity_type->getBundleOf()) {
foreach ($this->entityTypeManager->getStorage($type)->loadMultiple() as $entity) {
$this->bundleInfo[$entity_type->getBundleOf()][$entity->id()]['label'] = $entity->label();
}
}
}
foreach ($this->entityTypeManager->getDefinitions() as $type => $entity_type) {
// If no bundles are provided, use the entity type name and label.
if (!isset($this->bundleInfo[$type])) {
$this->bundleInfo[$type][$type]['label'] = $entity_type->getLabel();
}
}
$this->moduleHandler->alter('entity_bundle_info', $this->bundleInfo);
$this->cacheSet("entity_bundle_info:$langcode", $this->bundleInfo, Cache::PERMANENT, ['entity_types', 'entity_bundles']);
}
}
return $this->bundleInfo;
}
/**
* {@inheritdoc}
*/
public function clearCachedBundles() {
$this->bundleInfo = [];
Cache::invalidateTags(['entity_bundles']);
// Entity bundles are exposed as data types, clear that cache too.
$this->typedDataManager->clearCachedDefinitions();
}
}

View file

@ -0,0 +1,39 @@
<?php
/**
* @file
* Contains \Drupal\Core\Entity\EntityTypeBundleInfoInterface.
*/
namespace Drupal\Core\Entity;
/**
* Provides an interface for an entity type bundle info.
*/
interface EntityTypeBundleInfoInterface {
/**
* Get the bundle info of all entity types.
*
* @return array
* An array of all bundle information.
*/
public function getAllBundleInfo();
/**
* Gets the bundle info of an entity type.
*
* @param string $entity_type
* The entity type.
*
* @return array
* Returns the bundle information for the specified entity type.
*/
public function getBundleInfo($entity_type);
/**
* Clears static and persistent bundles.
*/
public function clearCachedBundles();
}

View file

@ -0,0 +1,123 @@
<?php
/**
* @file
* Contains \Drupal\Core\Entity\EntityTypeListener.
*/
namespace Drupal\Core\Entity;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
/**
* Reacts to entity type CRUD on behalf of the Entity system.
*
* @see \Drupal\Core\Entity\EntityTypeEvents
*/
class EntityTypeListener implements EntityTypeListenerInterface {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The entity field manager.
*
* @var \Drupal\Core\Entity\EntityFieldManagerInterface
*/
protected $entityFieldManager;
/**
* The event dispatcher.
*
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
*/
protected $eventDispatcher;
/**
* The entity last installed schema repository.
*
* @var \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface
*/
protected $entityLastInstalledSchemaRepository;
/**
* Constructs a new EntityTypeListener.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
* The entity field manager.
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
* The event dispatcher.
* @param \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface $entity_last_installed_schema_repository
* The entity last installed schema repository.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, EventDispatcherInterface $event_dispatcher, EntityLastInstalledSchemaRepositoryInterface $entity_last_installed_schema_repository) {
$this->entityTypeManager = $entity_type_manager;
$this->entityFieldManager = $entity_field_manager;
$this->eventDispatcher = $event_dispatcher;
$this->entityLastInstalledSchemaRepository = $entity_last_installed_schema_repository;
}
/**
* {@inheritdoc}
*/
public function onEntityTypeCreate(EntityTypeInterface $entity_type) {
$entity_type_id = $entity_type->id();
// @todo Forward this to all interested handlers, not only storage, once
// iterating handlers is possible: https://www.drupal.org/node/2332857.
$storage = $this->entityTypeManager->getStorage($entity_type_id);
if ($storage instanceof EntityTypeListenerInterface) {
$storage->onEntityTypeCreate($entity_type);
}
$this->eventDispatcher->dispatch(EntityTypeEvents::CREATE, new EntityTypeEvent($entity_type));
$this->entityLastInstalledSchemaRepository->setLastInstalledDefinition($entity_type);
if ($entity_type->isSubclassOf(FieldableEntityInterface::class)) {
$this->entityLastInstalledSchemaRepository->setLastInstalledFieldStorageDefinitions($entity_type_id, $this->entityFieldManager->getFieldStorageDefinitions($entity_type_id));
}
}
/**
* {@inheritdoc}
*/
public function onEntityTypeUpdate(EntityTypeInterface $entity_type, EntityTypeInterface $original) {
$entity_type_id = $entity_type->id();
// @todo Forward this to all interested handlers, not only storage, once
// iterating handlers is possible: https://www.drupal.org/node/2332857.
$storage = $this->entityTypeManager->getStorage($entity_type_id);
if ($storage instanceof EntityTypeListenerInterface) {
$storage->onEntityTypeUpdate($entity_type, $original);
}
$this->eventDispatcher->dispatch(EntityTypeEvents::UPDATE, new EntityTypeEvent($entity_type, $original));
$this->entityLastInstalledSchemaRepository->setLastInstalledDefinition($entity_type);
}
/**
* {@inheritdoc}
*/
public function onEntityTypeDelete(EntityTypeInterface $entity_type) {
$entity_type_id = $entity_type->id();
// @todo Forward this to all interested handlers, not only storage, once
// iterating handlers is possible: https://www.drupal.org/node/2332857.
$storage = $this->entityTypeManager->getStorage($entity_type_id);
if ($storage instanceof EntityTypeListenerInterface) {
$storage->onEntityTypeDelete($entity_type);
}
$this->eventDispatcher->dispatch(EntityTypeEvents::DELETE, new EntityTypeEvent($entity_type));
$this->entityLastInstalledSchemaRepository->deleteLastInstalledDefinition($entity_type_id);
}
}

View file

@ -0,0 +1,266 @@
<?php
/**
* @file
* Contains \Drupal\Core\Entity\EntityTypeManager.
*/
namespace Drupal\Core\Entity;
use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\DependencyInjection\ClassResolverInterface;
use Drupal\Core\Entity\Exception\InvalidLinkTemplateException;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery;
use Drupal\Core\StringTranslation\TranslationInterface;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
/**
* Manages entity type plugin definitions.
*
* Each entity type definition array is set in the entity type's
* annotation and altered by hook_entity_type_alter().
*
* @see \Drupal\Core\Entity\Annotation\EntityType
* @see \Drupal\Core\Entity\EntityInterface
* @see \Drupal\Core\Entity\EntityTypeInterface
* @see hook_entity_type_alter()
*/
class EntityTypeManager extends DefaultPluginManager implements EntityTypeManagerInterface, ContainerAwareInterface {
use ContainerAwareTrait;
/**
* Contains instantiated handlers keyed by handler type and entity type.
*
* @var array
*/
protected $handlers = [];
/**
* The string translation service.
*
* @var \Drupal\Core\StringTranslation\TranslationInterface
*/
protected $stringTranslation;
/**
* The class resolver.
*
* @var \Drupal\Core\DependencyInjection\ClassResolverInterface
*/
protected $classResolver;
/**
* Constructs a new Entity plugin manager.
*
* @param \Traversable $namespaces
* An object that implements \Traversable which contains the root paths
* keyed by the corresponding namespace to look for plugin implementations,
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
* @param \Drupal\Core\Cache\CacheBackendInterface $cache
* The cache backend to use.
* @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
* The string translation.
* @param \Drupal\Core\DependencyInjection\ClassResolverInterface $class_resolver
* The class resolver.
*/
public function __construct(\Traversable $namespaces, ModuleHandlerInterface $module_handler, CacheBackendInterface $cache, TranslationInterface $string_translation, ClassResolverInterface $class_resolver) {
parent::__construct('Entity', $namespaces, $module_handler, 'Drupal\Core\Entity\EntityInterface');
$this->setCacheBackend($cache, 'entity_type', ['entity_types']);
$this->alterInfo('entity_type');
$this->discovery = new AnnotatedClassDiscovery('Entity', $namespaces, 'Drupal\Core\Entity\Annotation\EntityType');
$this->stringTranslation = $string_translation;
$this->classResolver = $class_resolver;
}
/**
* {@inheritdoc}
*/
public function processDefinition(&$definition, $plugin_id) {
/** @var \Drupal\Core\Entity\EntityTypeInterface $definition */
parent::processDefinition($definition, $plugin_id);
// All link templates must have a leading slash.
foreach ((array) $definition->getLinkTemplates() as $link_relation_name => $link_template) {
if ($link_template[0] != '/') {
throw new InvalidLinkTemplateException("Link template '$link_relation_name' for entity type '$plugin_id' must start with a leading slash, the current link template is '$link_template'");
}
}
}
/**
* {@inheritdoc}
*/
protected function findDefinitions() {
$definitions = $this->getDiscovery()->getDefinitions();
// Directly call the hook implementations to pass the definitions to them
// by reference, so new entity types can be added.
foreach ($this->moduleHandler->getImplementations('entity_type_build') as $module) {
$function = $module . '_' . 'entity_type_build';
$function($definitions);
}
foreach ($definitions as $plugin_id => $definition) {
$this->processDefinition($definition, $plugin_id);
}
$this->alterDefinitions($definitions);
return $definitions;
}
/**
* {@inheritdoc}
*/
public function getDefinition($entity_type_id, $exception_on_invalid = TRUE) {
if (($entity_type = parent::getDefinition($entity_type_id, FALSE)) && class_exists($entity_type->getClass())) {
return $entity_type;
}
elseif (!$exception_on_invalid) {
return NULL;
}
throw new PluginNotFoundException($entity_type_id, sprintf('The "%s" entity type does not exist.', $entity_type_id));
}
/**
* {@inheritdoc}
*/
public function clearCachedDefinitions() {
parent::clearCachedDefinitions();
$this->handlers = [];
}
/**
* {@inheritdoc}
*/
public function useCaches($use_caches = FALSE) {
parent::useCaches($use_caches);
if (!$use_caches) {
$this->handlers = [];
}
}
/**
* {@inheritdoc}
*/
public function hasHandler($entity_type, $handler_type) {
if ($definition = $this->getDefinition($entity_type, FALSE)) {
return $definition->hasHandlerClass($handler_type);
}
return FALSE;
}
/**
* {@inheritdoc}
*/
public function getStorage($entity_type) {
return $this->getHandler($entity_type, 'storage');
}
/**
* {@inheritdoc}
*/
public function getListBuilder($entity_type) {
return $this->getHandler($entity_type, 'list_builder');
}
/**
* {@inheritdoc}
*/
public function getFormObject($entity_type, $operation) {
if (!isset($this->handlers['form'][$operation][$entity_type])) {
if (!$class = $this->getDefinition($entity_type, TRUE)->getFormClass($operation)) {
throw new InvalidPluginDefinitionException($entity_type, sprintf('The "%s" entity type did not specify a "%s" form class.', $entity_type, $operation));
}
$form_object = $this->classResolver->getInstanceFromDefinition($class);
$form_object
->setStringTranslation($this->stringTranslation)
->setModuleHandler($this->moduleHandler)
->setEntityTypeManager($this)
->setOperation($operation)
// The entity manager cannot be injected due to a circular dependency.
// @todo Remove this set call in https://www.drupal.org/node/2603542.
->setEntityManager(\Drupal::entityManager());
$this->handlers['form'][$operation][$entity_type] = $form_object;
}
return $this->handlers['form'][$operation][$entity_type];
}
/**
* {@inheritdoc}
*/
public function getRouteProviders($entity_type) {
if (!isset($this->handlers['route_provider'][$entity_type])) {
$route_provider_classes = $this->getDefinition($entity_type, TRUE)->getRouteProviderClasses();
foreach ($route_provider_classes as $type => $class) {
$this->handlers['route_provider'][$entity_type][$type] = $this->createHandlerInstance($class, $this->getDefinition($entity_type));
}
}
return isset($this->handlers['route_provider'][$entity_type]) ? $this->handlers['route_provider'][$entity_type] : [];
}
/**
* {@inheritdoc}
*/
public function getViewBuilder($entity_type) {
return $this->getHandler($entity_type, 'view_builder');
}
/**
* {@inheritdoc}
*/
public function getAccessControlHandler($entity_type) {
return $this->getHandler($entity_type, 'access');
}
/**
* {@inheritdoc}
*/
public function getHandler($entity_type, $handler_type) {
if (!isset($this->handlers[$handler_type][$entity_type])) {
$definition = $this->getDefinition($entity_type);
$class = $definition->getHandlerClass($handler_type);
if (!$class) {
throw new InvalidPluginDefinitionException($entity_type, sprintf('The "%s" entity type did not specify a %s handler.', $entity_type, $handler_type));
}
$this->handlers[$handler_type][$entity_type] = $this->createHandlerInstance($class, $definition);
}
return $this->handlers[$handler_type][$entity_type];
}
/**
* {@inheritdoc}
*/
public function createHandlerInstance($class, EntityTypeInterface $definition = NULL) {
if (is_subclass_of($class, 'Drupal\Core\Entity\EntityHandlerInterface')) {
$handler = $class::createInstance($this->container, $definition);
}
else {
$handler = new $class($definition);
}
if (method_exists($handler, 'setModuleHandler')) {
$handler->setModuleHandler($this->moduleHandler);
}
if (method_exists($handler, 'setStringTranslation')) {
$handler->setStringTranslation($this->stringTranslation);
}
return $handler;
}
}

View file

@ -0,0 +1,146 @@
<?php
/**
* @file
* Contains \Drupal\Core\Entity\EntityTypeManagerInterface.
*/
namespace Drupal\Core\Entity;
use Drupal\Component\Plugin\Discovery\CachedDiscoveryInterface;
use Drupal\Component\Plugin\PluginManagerInterface;
/**
* Provides an interface for entity type managers.
*/
interface EntityTypeManagerInterface extends PluginManagerInterface, CachedDiscoveryInterface {
/**
* Creates a new access control handler instance.
*
* @param string $entity_type
* The entity type for this access control handler.
*
* @return \Drupal\Core\Entity\EntityAccessControlHandlerInterface.
* A access control handler instance.
*/
public function getAccessControlHandler($entity_type);
/**
* Creates a new storage instance.
*
* @param string $entity_type
* The entity type for this storage.
*
* @return \Drupal\Core\Entity\EntityStorageInterface
* A storage instance.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
*/
public function getStorage($entity_type);
/**
* Creates a new view builder instance.
*
* @param string $entity_type
* The entity type for this view builder.
*
* @return \Drupal\Core\Entity\EntityViewBuilderInterface.
* A view builder instance.
*/
public function getViewBuilder($entity_type);
/**
* Creates a new entity list builder.
*
* @param string $entity_type
* The entity type for this list builder.
*
* @return \Drupal\Core\Entity\EntityListBuilderInterface
* An entity list builder instance.
*/
public function getListBuilder($entity_type);
/**
* Creates a new form instance.
*
* @param string $entity_type
* The entity type for this form.
* @param string $operation
* The name of the operation to use, e.g., 'default'.
*
* @return \Drupal\Core\Entity\EntityFormInterface
* A form instance.
*/
public function getFormObject($entity_type, $operation);
/**
* Gets all route provider instances.
*
* @param string $entity_type
* The entity type for this route providers.
*
* @return \Drupal\Core\Entity\Routing\EntityRouteProviderInterface[]
*/
public function getRouteProviders($entity_type);
/**
* Checks whether a certain entity type has a certain handler.
*
* @param string $entity_type
* The name of the entity type.
* @param string $handler_type
* The name of the handler.
*
* @return bool
* Returns TRUE if the entity type has the handler, else FALSE.
*/
public function hasHandler($entity_type, $handler_type);
/**
* Creates a new handler instance for a entity type and handler type.
*
* @param string $entity_type
* The entity type for this handler.
* @param string $handler_type
* The handler type to create an instance for.
*
* @return object
* A handler instance.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
*/
public function getHandler($entity_type, $handler_type);
/**
* Creates new handler instance.
*
* Usually \Drupal\Core\Entity\EntityManagerInterface::getHandler() is
* preferred since that method has additional checking that the class exists
* and has static caches.
*
* @param mixed $class
* The handler class to instantiate.
* @param \Drupal\Core\Entity\EntityTypeInterface $definition
* The entity type definition.
*
* @return object
* A handler instance.
*/
public function createHandlerInstance($class, EntityTypeInterface $definition = NULL);
/**
* {@inheritdoc}
*
* @return \Drupal\Core\Entity\EntityTypeInterface|null
*/
public function getDefinition($entity_type_id, $exception_on_invalid = TRUE);
/**
* {@inheritdoc}
*
* @return \Drupal\Core\Entity\EntityTypeInterface[]
*/
public function getDefinitions();
}

View file

@ -0,0 +1,113 @@
<?php
/**
* @file
* Contains \Drupal\Core\Entity\EntityTypeRepository.
*/
namespace Drupal\Core\Entity;
use Drupal\Core\Entity\Exception\AmbiguousEntityClassException;
use Drupal\Core\Entity\Exception\NoCorrespondingEntityClassException;
use Drupal\Core\StringTranslation\StringTranslationTrait;
/**
* Provides helper methods for loading entity types.
*
* @see \Drupal\Core\Entity\EntityTypeManagerInterface
*/
class EntityTypeRepository implements EntityTypeRepositoryInterface {
use StringTranslationTrait;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Contains cached mappings of class names to entity types.
*
* @var array
*/
protected $classNameEntityTypeMap = [];
/**
* Constructs a new EntityTypeRepository.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public function getEntityTypeLabels($group = FALSE) {
$options = [];
$definitions = $this->entityTypeManager->getDefinitions();
foreach ($definitions as $entity_type_id => $definition) {
if ($group) {
$options[(string) $definition->getGroupLabel()][$entity_type_id] = $definition->getLabel();
}
else {
$options[$entity_type_id] = $definition->getLabel();
}
}
if ($group) {
foreach ($options as &$group_options) {
// Sort the list alphabetically by group label.
array_multisort($group_options, SORT_ASC, SORT_NATURAL);
}
// Make sure that the 'Content' group is situated at the top.
$content = $this->t('Content', array(), array('context' => 'Entity type group'));
$options = array((string) $content => $options[(string) $content]) + $options;
}
return $options;
}
/**
* {@inheritdoc}
*/
public function getEntityTypeFromClass($class_name) {
// Check the already calculated classes first.
if (isset($this->classNameEntityTypeMap[$class_name])) {
return $this->classNameEntityTypeMap[$class_name];
}
$same_class = 0;
$entity_type_id = NULL;
foreach ($this->entityTypeManager->getDefinitions() as $entity_type) {
if ($entity_type->getOriginalClass() == $class_name || $entity_type->getClass() == $class_name) {
$entity_type_id = $entity_type->id();
if ($same_class++) {
throw new AmbiguousEntityClassException($class_name);
}
}
}
// Return the matching entity type ID if there is one.
if ($entity_type_id) {
$this->classNameEntityTypeMap[$class_name] = $entity_type_id;
return $entity_type_id;
}
throw new NoCorrespondingEntityClassException($class_name);
}
/**
* {@inheritdoc}
*/
public function clearCachedDefinitions() {
$this->classNameEntityTypeMap = [];
}
}

View file

@ -0,0 +1,59 @@
<?php
/**
* @file
* Contains \Drupal\Core\Entity\EntityTypeRepositoryInterface.
*/
namespace Drupal\Core\Entity;
/**
* Provides an interface for helper methods for loading entity types.
*/
interface EntityTypeRepositoryInterface {
/**
* Builds a list of entity type labels suitable for a Form API options list.
*
* @param bool $group
* (optional) Whether to group entity types by plugin group (e.g. 'content',
* 'config'). Defaults to FALSE.
*
* @return array
* An array of entity type labels, keyed by entity type name.
*/
public function getEntityTypeLabels($group = FALSE);
/**
* Gets the entity type ID based on the class that is called on.
*
* Compares the class this is called on against the known entity classes
* and returns the entity type ID of a direct match or a subclass as fallback,
* to support entity type definitions that were altered.
*
* @param string $class_name
* Class name to use for searching the entity type ID.
*
* @return string
* The entity type ID.
*
* @throws \Drupal\Core\Entity\Exception\AmbiguousEntityClassException
* Thrown when multiple subclasses correspond to the called class.
* @throws \Drupal\Core\Entity\Exception\NoCorrespondingEntityClassException
* Thrown when no entity class corresponds to the called class.
*
* @see \Drupal\Core\Entity\Entity::load()
* @see \Drupal\Core\Entity\Entity::loadMultiple()
*/
public function getEntityTypeFromClass($class_name);
/**
* Clear the static cache.
*
* @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 clearCachedDefinitions();
}

View file

@ -147,11 +147,11 @@ interface FieldableEntityInterface extends EntityInterface {
* @param string $field_name
* The name of the field to get; e.g., 'title' or 'name'.
*
* @throws \InvalidArgumentException
* If an invalid field name is given.
*
* @return \Drupal\Core\Field\FieldItemListInterface
* The field item list, containing the field items.
*
* @throws \InvalidArgumentException
* If an invalid field name is given.
*/
public function get($field_name);
@ -167,10 +167,10 @@ interface FieldableEntityInterface extends EntityInterface {
* TRUE. If the update stems from the entity, set it to FALSE to avoid
* being notified again.
*
* @return $this
*
* @throws \InvalidArgumentException
* If the specified field does not exist.
*
* @return $this
*/
public function set($field_name, $value, $notify = TRUE);

View file

@ -48,14 +48,14 @@ class BundleConstraint extends Constraint {
}
/**
* Overrides Constraint::getDefaultOption().
* {@inheritdoc}
*/
public function getDefaultOption() {
return 'bundle';
}
/**
* Overrides Constraint::getRequiredOptions().
* {@inheritdoc}
*/
public function getRequiredOptions() {
return array('bundle');

View file

@ -35,14 +35,14 @@ class EntityTypeConstraint extends Constraint {
public $type;
/**
* Overrides Constraint::getDefaultOption().
* {@inheritdoc}
*/
public function getDefaultOption() {
return 'type';
}
/**
* Overrides Constraint::getRequiredOptions().
* {@inheritdoc}
*/
public function getRequiredOptions() {
return array('type');

View file

@ -13,7 +13,7 @@ namespace Drupal\Core\Entity\Query;
abstract class ConditionAggregateBase extends ConditionFundamentals implements ConditionAggregateInterface {
/**
* Implements \Drupal\Core\Entity\Query\ConditionAggregateInterface::condition().
* {@inheritdoc}
*/
public function condition($field, $function = NULL, $value = NULL, $operator = NULL, $langcode = NULL) {
$this->conditions[] = array(

View file

@ -13,7 +13,7 @@ namespace Drupal\Core\Entity\Query;
abstract class ConditionBase extends ConditionFundamentals implements ConditionInterface {
/**
* Implements \Drupal\Core\Entity\Query\ConditionInterface::compile().
* {@inheritdoc}
*/
public function condition($field, $value = NULL, $operator = NULL, $langcode = NULL) {
$this->conditions[] = array(

View file

@ -60,21 +60,21 @@ abstract class ConditionFundamentals {
}
/**
* Implements \Drupal\Core\Entity\Query\ConditionInterface::getConjunction().
* {@inheritdoc}
*/
public function getConjunction() {
return $this->conjunction;
}
/**
* Implements \Countable::count().
* {@inheritdoc}
*/
public function count() {
return count($this->conditions) - 1;
}
/**
* Implements \Drupal\Core\Entity\Query\ConditionInterface::conditions().
* {@inheritdoc}
*/
public function &conditions() {
return $this->conditions;

View file

@ -15,20 +15,20 @@ use Drupal\Core\Entity\Query\ConditionBase;
class Condition extends ConditionBase {
/**
* Implements \Drupal\Core\Entity\Query\ConditionInterface::compile().
* {@inheritdoc}
*/
public function compile($query) {
}
/**
* Implements \Drupal\Core\Entity\Query\ConditionInterface::exists().
* {@inheritdoc}
*/
public function exists($field, $langcode = NULL) {
return $this->condition($field, NULL, 'IS NOT NULL', $langcode);
}
/**
* Implements \Drupal\Core\Entity\Query\ConditionInterface::notExists().
* {@inheritdoc}
*/
public function notExists($field, $langcode = NULL) {
return $this->condition($field, NULL, 'IS NULL', $langcode);

View file

@ -18,7 +18,7 @@ use Drupal\Core\Entity\Query\Sql\ConditionAggregate;
class Query extends QueryBase implements QueryInterface, QueryAggregateInterface {
/**
* Implements \Drupal\Core\Entity\Query\QueryInterface::execute().
* {@inheritdoc}
*/
public function execute() {
if ($this->count) {

View file

@ -159,7 +159,7 @@ abstract class QueryBase implements QueryInterface {
}
/**
* Implements \Drupal\Core\Entity\Query\QueryInterface::condition().
* {@inheritdoc}
*/
public function condition($property, $value = NULL, $operator = NULL, $langcode = NULL) {
$this->condition->condition($property, $value, $operator, $langcode);
@ -167,7 +167,7 @@ abstract class QueryBase implements QueryInterface {
}
/**
* Implements \Drupal\Core\Entity\Query\QueryInterface::exists().
* {@inheritdoc}
*/
public function exists($property, $langcode = NULL) {
$this->condition->exists($property, $langcode);
@ -175,7 +175,7 @@ abstract class QueryBase implements QueryInterface {
}
/**
* Implements \Drupal\Core\Entity\Query\QueryInterface::notExists().
* {@inheritdoc}
*/
public function notExists($property, $langcode = NULL) {
$this->condition->notExists($property, $langcode);
@ -183,7 +183,7 @@ abstract class QueryBase implements QueryInterface {
}
/**
* Implements \Drupal\Core\Entity\Query\QueryInterface::range().
* {@inheritdoc}
*/
public function range($start = NULL, $length = NULL) {
$this->range = array(
@ -211,21 +211,21 @@ abstract class QueryBase implements QueryInterface {
}
/**
* Implements \Drupal\Core\Entity\Query\QueryInterface::andConditionGroup().
* {@inheritdoc}
*/
public function andConditionGroup() {
return $this->conditionGroupFactory('and');
}
/**
* Implements \Drupal\Core\Entity\Query\QueryInterface::orConditionGroup().
* {@inheritdoc}
*/
public function orConditionGroup() {
return $this->conditionGroupFactory('or');
}
/**
* Implements \Drupal\Core\Entity\Query\QueryInterface::sort().
* {@inheritdoc}
*/
public function sort($field, $direction = 'ASC', $langcode = NULL) {
$this->sort[] = array(
@ -237,7 +237,7 @@ abstract class QueryBase implements QueryInterface {
}
/**
* Implements \Drupal\Core\Entity\Query\QueryInterface::count().
* {@inheritdoc}
*/
public function count() {
$this->count = TRUE;
@ -245,7 +245,7 @@ abstract class QueryBase implements QueryInterface {
}
/**
* Implements \Drupal\Core\Entity\Query\QueryInterface::accessCheck().
* {@inheritdoc}
*/
public function accessCheck($access_check = TRUE) {
$this->accessCheck = $access_check;
@ -269,7 +269,7 @@ abstract class QueryBase implements QueryInterface {
}
/**
* Implements \Drupal\Core\Entity\Query\QueryInterface::pager().
* {@inheritdoc}
*/
public function pager($limit = 10, $element = NULL) {
// Even when not using SQL, storing the element PagerSelectExtender is as
@ -306,7 +306,7 @@ abstract class QueryBase implements QueryInterface {
}
/**
* Implements \Drupal\Core\Entity\Query\QueryInterface::tableSort().
* {@inheritdoc}
*/
public function tableSort(&$headers) {
// If 'field' is not initialized, the header columns aren't clickable.
@ -335,7 +335,7 @@ abstract class QueryBase implements QueryInterface {
}
/**
* Implements \Drupal\Core\Database\Query\AlterableInterface::addTag().
* {@inheritdoc}
*/
public function addTag($tag) {
$this->alterTags[$tag] = 1;
@ -343,28 +343,28 @@ abstract class QueryBase implements QueryInterface {
}
/**
* Implements \Drupal\Core\Database\Query\AlterableInterface::hasTag().
* {@inheritdoc}
*/
public function hasTag($tag) {
return isset($this->alterTags[$tag]);
}
/**
* Implements \Drupal\Core\Database\Query\AlterableInterface::hasAllTags().
* {@inheritdoc}
*/
public function hasAllTags() {
return !(boolean)array_diff(func_get_args(), array_keys($this->alterTags));
}
/**
* Implements \Drupal\Core\Database\Query\AlterableInterface::hasAnyTag().
* {@inheritdoc}
*/
public function hasAnyTag() {
return (boolean)array_intersect(func_get_args(), array_keys($this->alterTags));
}
/**
* Implements \Drupal\Core\Database\Query\AlterableInterface::addMetaData().
* {@inheritdoc}
*/
public function addMetaData($key, $object) {
$this->alterMetaData[$key] = $object;
@ -372,14 +372,14 @@ abstract class QueryBase implements QueryInterface {
}
/**
* Implements \Drupal\Core\Database\Query\AlterableInterface::getMetaData().
* {@inheritdoc}
*/
public function getMetaData($key) {
return isset($this->alterMetaData[$key]) ? $this->alterMetaData[$key] : NULL;
}
/**
* Implements \Drupal\Core\Entity\Query\QueryAggregateInterface::aggregate()
* {@inheritdoc}
*/
public function aggregate($field, $function, $langcode = NULL, &$alias = NULL) {
if (!isset($alias)) {
@ -397,7 +397,7 @@ abstract class QueryBase implements QueryInterface {
}
/**
* Implements \Drupal\Core\Entity\Query\QueryAggregateInterface::conditionAggregate().
* {@inheritdoc}
*/
public function conditionAggregate($field, $function = NULL, $value = NULL, $operator = '=', $langcode = NULL) {
$this->aggregate($field, $function, $langcode);
@ -407,7 +407,7 @@ abstract class QueryBase implements QueryInterface {
}
/**
* Implements \Drupal\Core\Entity\Query\QueryAggregateInterface::sortAggregate().
* {@inheritdoc}
*/
public function sortAggregate($field, $function, $direction = 'ASC', $langcode = NULL) {
$alias = $this->getAggregationAlias($field, $function);
@ -424,7 +424,7 @@ abstract class QueryBase implements QueryInterface {
}
/**
* Implements \Drupal\Core\Entity\Query\QueryAggregateInterface::execute().
* {@inheritdoc}
*/
public function groupBy($field, $langcode = NULL) {
$this->groupBy[] = array(

View file

@ -35,9 +35,10 @@ interface QueryFactoryInterface {
* - AND: all of the conditions on the query need to match.
* - OR: at least one of the conditions on the query need to match.
*
* @throws \Drupal\Core\Entity\Query\QueryException
* @return \Drupal\Core\Entity\Query\QueryAggregateInterface
* The query object that can query the given entity type.
*
* @throws \Drupal\Core\Entity\Query\QueryException
*/
public function getAggregate(EntityTypeInterface $entity_type, $conjunction);

View file

@ -19,7 +19,7 @@ use Drupal\Core\Entity\Query\QueryBase;
class ConditionAggregate extends ConditionAggregateBase {
/**
* Implements \Drupal\Core\Entity\Query\ConditionInterface::compile().
* {@inheritdoc}
*/
public function compile($conditionContainer) {
// If this is not the top level condition group then the sql query is
@ -50,14 +50,14 @@ class ConditionAggregate extends ConditionAggregateBase {
}
/**
* Implements \Drupal\Core\Entity\Query\ConditionInterface::exists().
* {@inheritdoc}
*/
public function exists($field, $function, $langcode = NULL) {
return $this->condition($field, $function, NULL, 'IS NOT NULL', $langcode);
}
/**
* Implements \Drupal\Core\Entity\Query\ConditionInterface::notExists().
* {@inheritdoc}
*/
public function notExists($field, $function, $langcode = NULL) {
return $this->condition($field, $function, NULL, 'IS NULL', $langcode);

View file

@ -78,7 +78,7 @@ class Query extends QueryBase implements QueryInterface {
/**
* Implements \Drupal\Core\Entity\Query\QueryInterface::execute().
* {@inheritdoc}
*/
public function execute() {
return $this
@ -92,11 +92,11 @@ class Query extends QueryBase implements QueryInterface {
/**
* Prepares the basic query with proper metadata/tags and base fields.
*
* @throws \Drupal\Core\Entity\Query\QueryException
* Thrown if the base table does not exists.
*
* @return \Drupal\Core\Entity\Query\Sql\Query
* Returns the called object.
*
* @throws \Drupal\Core\Entity\Query\QueryException
* Thrown if the base table does not exist.
*/
protected function prepare() {
if ($this->allRevisions) {

View file

@ -23,7 +23,7 @@ class QueryAggregate extends Query implements QueryAggregateInterface {
protected $sqlExpressions = array();
/**
* Implements \Drupal\Core\Entity\Query\QueryAggregateInterface::execute().
* {@inheritdoc}
*/
public function execute() {
return $this
@ -39,7 +39,7 @@ class QueryAggregate extends Query implements QueryAggregateInterface {
}
/**
* Overrides \Drupal\Core\Entity\Query\Sql::prepare().
* {@inheritdoc}
*/
public function prepare() {
parent::prepare();
@ -49,7 +49,7 @@ class QueryAggregate extends Query implements QueryAggregateInterface {
}
/**
* Implements \Drupal\Core\Entity\Query\QueryAggregateInterface::conditionAggregateGroupFactory().
* {@inheritdoc}
*/
public function conditionAggregateGroupFactory($conjunction = 'AND') {
$class = static::getClass($this->namespaces, 'ConditionAggregate');
@ -57,14 +57,14 @@ class QueryAggregate extends Query implements QueryAggregateInterface {
}
/**
* Overrides \Drupal\Core\Entity\QueryBase::exists().
* {@inheritdoc}
*/
public function existsAggregate($field, $function, $langcode = NULL) {
return $this->conditionAggregate->exists($field, $function, $langcode);
}
/**
* Overrides \Drupal\Core\Entity\QueryBase::notExists().
* {@inheritdoc}
*/
public function notExistsAggregate($field, $function, $langcode = NULL) {
return $this->conditionAggregate->notExists($field, $function, $langcode);

View file

@ -235,7 +235,9 @@ class Tables implements TablesInterface {
* Join entity table if necessary and return the alias for it.
*
* @param string $property
*
* @return string
*
* @throws \Drupal\Core\Entity\Query\QueryException
*/
protected function ensureEntityTable($index_prefix, $property, $type, $langcode, $base_table, $id_field, $entity_tables) {

View file

@ -23,13 +23,13 @@ interface TablesInterface {
* @param string $langcode
* The language code the field values are to be queried in.
*
* @throws \Drupal\Core\Entity\Query\QueryException
* If $field specifies an invalid relationship.
*
* @return string
* The return value is a string containing the alias of the table, a dot
* and the appropriate SQL column as passed in. This allows the direct use
* of this in a query for a condition or sort.
*
* @throws \Drupal\Core\Entity\Query\QueryException
* If $field specifies an invalid relationship.
*/
public function addField($field, $type, $langcode);

View file

@ -684,7 +684,7 @@ class SqlContentEntityStorage extends ContentEntityStorageBase implements SqlEnt
}
/**
* Implements \Drupal\Core\Entity\EntityStorageInterface::delete().
* {@inheritdoc}
*/
public function delete(array $entities) {
if (!$entities) {
@ -1453,9 +1453,11 @@ class SqlContentEntityStorage extends ContentEntityStorageBase implements SqlEnt
$callback();
}
catch (SchemaException $e) {
$message .= ' ' . $e->getMessage();
throw new EntityStorageException($message, 0, $e);
}
catch (DatabaseExceptionWrapper $e) {
$message .= ' ' . $e->getMessage();
throw new EntityStorageException($message, 0, $e);
}
}

View file

@ -591,22 +591,36 @@ class SqlContentEntityStorageSchema implements DynamicallyFieldableEntityStorage
* storage definitions.
*/
protected function getEntitySchemaData(ContentEntityTypeInterface $entity_type, array $schema) {
$schema_data = array();
$entity_type_id = $entity_type->id();
$keys = array('indexes', 'unique keys');
$unused_keys = array_flip(array('description', 'fields', 'foreign keys'));
// Collect all possible field schema identifiers for shared table fields.
// These will be used to detect entity schema data in the subsequent loop.
$field_schema_identifiers = [];
$storage_definitions = $this->entityManager->getFieldStorageDefinitions($entity_type_id);
$table_mapping = $this->storage->getTableMapping($storage_definitions);
foreach ($storage_definitions as $field_name => $storage_definition) {
if ($table_mapping->allowsSharedTableStorage($storage_definition)) {
// Make sure both base identifier names and suffixed names are listed.
$name = $this->getFieldSchemaIdentifierName($entity_type_id, $field_name);
$field_schema_identifiers[$name] = $name;
foreach ($storage_definition->getColumns() as $key => $columns) {
$name = $this->getFieldSchemaIdentifierName($entity_type_id, $field_name, $key);
$field_schema_identifiers[$name] = $name;
}
}
}
// Extract entity schema data from the Schema API definition.
$schema_data = [];
$keys = ['indexes', 'unique keys'];
$unused_keys = array_flip(['description', 'fields', 'foreign keys']);
foreach ($schema as $table_name => $table_schema) {
$table_schema = array_diff_key($table_schema, $unused_keys);
foreach ($keys as $key) {
// Exclude data generated from field storage definitions, we will check
// that separately.
if (!empty($table_schema[$key])) {
$data_keys = array_keys($table_schema[$key]);
$entity_keys = array_filter($data_keys, function ($key) use ($entity_type_id) {
return strpos($key, $entity_type_id . '_field__') !== 0;
});
$table_schema[$key] = array_intersect_key($table_schema[$key], array_flip($entity_keys));
if ($field_schema_identifiers && !empty($table_schema[$key])) {
$table_schema[$key] = array_diff_key($table_schema[$key], $field_schema_identifiers);
}
}
$schema_data[$table_name] = array_filter($table_schema);