diff --git a/core/lib/Drupal/Core/Config/Entity/ConfigStorageController.php b/core/lib/Drupal/Core/Config/Entity/ConfigStorageController.php index 6f58ce1..134a5c2 100644 --- a/core/lib/Drupal/Core/Config/Entity/ConfigStorageController.php +++ b/core/lib/Drupal/Core/Config/Entity/ConfigStorageController.php @@ -307,7 +307,7 @@ protected function attachLoad(&$queried_entities, $revision_id = FALSE) { } /** - * Implements Drupal\Core\Entity\EntityStorageControllerInterface::createEntity(). + * {@inheritdoc} */ public function createEntity(array $values) { $class = $this->entityInfo['class']; @@ -316,7 +316,7 @@ public function createEntity(array $values) { // Set default language to site default if not provided. $values += array('langcode' => language_default()->id); - $entity = new $class($values, $this->entityType); + $entity = $class::create(\Drupal::getContainer(), $values, $this->entityType); // Mark this entity as new, so isNew() returns TRUE. This does not check // whether a configuration entity with the same ID (if any) already exists. $entity->enforceIsNew(); diff --git a/core/lib/Drupal/Core/Entity/DatabaseStorageController.php b/core/lib/Drupal/Core/Entity/DatabaseStorageController.php index 45524e0..0c71d08 100644 --- a/core/lib/Drupal/Core/Entity/DatabaseStorageController.php +++ b/core/lib/Drupal/Core/Entity/DatabaseStorageController.php @@ -360,13 +360,13 @@ protected function attachLoad(&$queried_entities, $load_revision = FALSE) { } /** - * Implements \Drupal\Core\Entity\EntityStorageControllerInterface::createEntity(). + * {@inheritdoc} */ public function createEntity(array $values) { $entity_class = $this->entityInfo['class']; $entity_class::preCreate($this, $values); - $entity = new $entity_class($values, $this->entityType); + $entity = $entity_class::create(\Drupal::getContainer(), $values, $this->entityType); // Assign a new UUID if there is none yet. if ($this->uuidKey && !isset($entity->{$this->uuidKey})) { @@ -383,7 +383,7 @@ public function createEntity(array $values) { } /** - * Implements \Drupal\Core\Entity\EntityStorageControllerInterface::delete(). + * {@inheritdoc} */ public function delete(array $entities) { if (!$entities) { diff --git a/core/lib/Drupal/Core/Entity/DatabaseStorageControllerNG.php b/core/lib/Drupal/Core/Entity/DatabaseStorageControllerNG.php index 240f5cb..e93259a 100644 --- a/core/lib/Drupal/Core/Entity/DatabaseStorageControllerNG.php +++ b/core/lib/Drupal/Core/Entity/DatabaseStorageControllerNG.php @@ -71,7 +71,7 @@ public function __construct($entity_type, array $entity_info, Connection $databa } /** - * Overrides DatabaseStorageController::createEntity(). + * {@inheritdoc} * * @param array $values * An array of values to set, keyed by field name. The value has to be @@ -108,7 +108,7 @@ public function createEntity(array $values) { } $bundle = $values[$this->bundleKey]; } - $entity = new $this->entityClass(array(), $this->entityType, $bundle); + $entity = $entity_class::create(\Drupal::getContainer(), array(), $this->entityType, $bundle); foreach ($entity as $name => $field) { if (isset($values[$name])) { diff --git a/core/lib/Drupal/Core/Entity/Entity.php b/core/lib/Drupal/Core/Entity/Entity.php index 3fc660d..93734e8 100644 --- a/core/lib/Drupal/Core/Entity/Entity.php +++ b/core/lib/Drupal/Core/Entity/Entity.php @@ -60,6 +60,13 @@ class Entity implements IteratorAggregate, EntityInterface { protected $isDefaultRevision = TRUE; /** + * EntityManager service. + * + * @var Drupal\Core\Entity\EntityManager + */ + protected $entityManager; + + /** * Constructs an Entity object. * * @param array $values @@ -68,8 +75,9 @@ class Entity implements IteratorAggregate, EntityInterface { * @param string $entity_type * The type of the entity to create. */ - public function __construct(array $values, $entity_type) { + public function __construct($entityManager, array $values, $entity_type) { $this->entityType = $entity_type; + $this->entityManager = $entityManager; // Set initial values. foreach ($values as $key => $value) { $this->$key = $value; @@ -77,28 +85,39 @@ public function __construct(array $values, $entity_type) { } /** - * Implements \Drupal\Core\Entity\EntityInterface::id(). + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $values, $entity_type) { + return new static( + $container->get('plugin.manager.entity'), + $values, + $entity_type + ); + } + + /** + * {@inheritdoc} */ public function id() { return isset($this->id) ? $this->id : NULL; } /** - * Implements \Drupal\Core\Entity\EntityInterface::uuid(). + * {@inheritdoc} */ public function uuid() { return isset($this->uuid) ? $this->uuid : NULL; } /** - * Implements \Drupal\Core\Entity\EntityInterface::isNew(). + * {@inheritdoc} */ public function isNew() { return !empty($this->enforceIsNew) || !$this->id(); } /** - * Implements \Drupal\Core\Entity\EntityInterface::isNewRevision(). + * {@inheritdoc} */ public function isNewRevision() { $info = $this->entityInfo(); @@ -106,35 +125,35 @@ public function isNewRevision() { } /** - * Implements \Drupal\Core\Entity\EntityInterface::enforceIsNew(). + * {@inheritdoc} */ public function enforceIsNew($value = TRUE) { $this->enforceIsNew = $value; } /** - * Implements \Drupal\Core\Entity\EntityInterface::setNewRevision(). + * {@inheritdoc} */ public function setNewRevision($value = TRUE) { $this->newRevision = $value; } /** - * Implements \Drupal\Core\Entity\EntityInterface::entityType(). + * {@inheritdoc} */ public function entityType() { return $this->entityType; } /** - * Implements \Drupal\Core\Entity\EntityInterface::bundle(). + * {@inheritdoc} */ public function bundle() { return $this->entityType; } /** - * Implements \Drupal\Core\Entity\EntityInterface::label(). + * {@inheritdoc} */ public function label($langcode = NULL) { $label = NULL; @@ -149,7 +168,7 @@ public function label($langcode = NULL) { } /** - * Implements \Drupal\Core\Entity\EntityInterface::uri(). + * {@inheritdoc} */ public function uri() { $bundle = $this->bundle(); @@ -195,7 +214,7 @@ public function uriRelationships() { } /** - * Implements \Drupal\Core\Entity\EntityInterface::get(). + * {@inheritdoc} */ public function get($property_name, $langcode = NULL) { // @todo: Replace by EntityNG implementation once all entity types have been @@ -204,7 +223,7 @@ public function get($property_name, $langcode = NULL) { } /** - * Implements \Drupal\Core\TypedData\ComplexDataInterface::set(). + * {@inheritdoc} */ public function set($property_name, $value, $notify = TRUE) { // @todo: Replace by EntityNG implementation once all entity types have been @@ -213,7 +232,7 @@ public function set($property_name, $value, $notify = TRUE) { } /** - * Implements \Drupal\Core\TypedData\ComplexDataInterface::getProperties(). + * {@inheritdoc} */ public function getProperties($include_computed = FALSE) { // @todo: Replace by EntityNG implementation once all entity types have been @@ -221,7 +240,7 @@ public function getProperties($include_computed = FALSE) { } /** - * Implements \Drupal\Core\TypedData\ComplexDataInterface::getPropertyValues(). + * {@inheritdoc} */ public function getPropertyValues() { // @todo: Replace by EntityNG implementation once all entity types have been @@ -229,7 +248,7 @@ public function getPropertyValues() { } /** - * Implements \Drupal\Core\TypedData\ComplexDataInterface::setPropertyValues(). + * {@inheritdoc} */ public function setPropertyValues($values) { // @todo: Replace by EntityNG implementation once all entity types have been @@ -237,7 +256,7 @@ public function setPropertyValues($values) { } /** - * Implements \Drupal\Core\TypedData\ComplexDataInterface::getPropertyDefinition(). + * {@inheritdoc} */ public function getPropertyDefinition($name) { // @todo: Replace by EntityNG implementation once all entity types have been @@ -245,7 +264,7 @@ public function getPropertyDefinition($name) { } /** - * Implements \Drupal\Core\TypedData\ComplexDataInterface::getPropertyDefinitions(). + * {@inheritdoc} */ public function getPropertyDefinitions() { // @todo: Replace by EntityNG implementation once all entity types have been @@ -253,7 +272,7 @@ public function getPropertyDefinitions() { } /** - * Implements \Drupal\Core\TypedData\ComplexDataInterface::isEmpty(). + * {@inheritdoc} */ public function isEmpty() { // @todo: Replace by EntityNG implementation once all entity types have been @@ -261,7 +280,7 @@ public function isEmpty() { } /** - * Implements \Drupal\Core\TypedData\ComplexDataInterface::getIterator(). + * {@inheritdoc} */ public function getIterator() { // @todo: Replace by EntityNG implementation once all entity types have been @@ -270,21 +289,21 @@ public function getIterator() { } /** - * Implements \Drupal\Core\TypedData\AccessibleInterface::access(). + * {@inheritdoc} */ public function access($operation = 'view', AccountInterface $account = NULL) { if ($operation == 'create') { - return \Drupal::entityManager() + return $this->entityManager ->getAccessController($this->entityType) ->createAccess($this->bundle(), $account); } - return \Drupal::entityManager() + return $this->entityManager ->getAccessController($this->entityType) ->access($this, $operation, Language::LANGCODE_DEFAULT, $account); } /** - * Implements \Drupal\Core\TypedData\TranslatableInterface::language(). + * {@inheritdoc} */ public function language() { // @todo: Replace by EntityNG implementation once all entity types have been @@ -298,9 +317,7 @@ public function language() { } /** - * Implements \Drupal\Core\TypedData\TranslatableInterface::getTranslation(). - * - * @return \Drupal\Core\Entity\EntityInterface + * {@inheritdoc} */ public function getTranslation($langcode) { // @todo: Replace by EntityNG implementation once all entity types have been @@ -320,7 +337,7 @@ public function translations() { } /** - * Implements \Drupal\Core\TypedData\TranslatableInterface::getTranslationLanguages(). + * {@inheritdoc} */ public function getTranslationLanguages($include_default = TRUE) { // @todo: Replace by EntityNG implementation once all entity types have been @@ -351,23 +368,23 @@ public function getTranslationLanguages($include_default = TRUE) { } /** - * Implements \Drupal\Core\Entity\EntityInterface::save(). + * {@inheritdoc} */ public function save() { - return \Drupal::entityManager()->getStorageController($this->entityType)->save($this); + return $this->entityManager->getStorageController($this->entityType)->save($this); } /** - * Implements \Drupal\Core\Entity\EntityInterface::delete(). + * {@inheritdoc} */ public function delete() { if (!$this->isNew()) { - \Drupal::entityManager()->getStorageController($this->entityType)->delete(array($this->id() => $this)); + $this->entityManager->getStorageController($this->entityType)->delete(array($this->id() => $this)); } } /** - * Implements \Drupal\Core\Entity\EntityInterface::createDuplicate(). + * {@inheritdoc} */ public function createDuplicate() { $duplicate = clone $this; @@ -383,21 +400,21 @@ public function createDuplicate() { } /** - * Implements \Drupal\Core\Entity\EntityInterface::entityInfo(). + * {@inheritdoc} */ public function entityInfo() { - return \Drupal::entityManager()->getDefinition($this->entityType()); + return $this->entityManager->getDefinition($this->entityType()); } /** - * Implements \Drupal\Core\Entity\EntityInterface::getRevisionId(). + * {@inheritdoc} */ public function getRevisionId() { return NULL; } /** - * Implements \Drupal\Core\Entity\EntityInterface::isDefaultRevision(). + * {@inheritdoc} */ public function isDefaultRevision($new_value = NULL) { $return = $this->isDefaultRevision; @@ -408,21 +425,21 @@ public function isDefaultRevision($new_value = NULL) { } /** - * Implements \Drupal\Core\Entity\EntityInterface::getExportProperties(). + * {@inheritdoc} */ public function getExportProperties() { return array(); } /** - * Implements \Drupal\Core\Entity\EntityInterface::getBCEntity(). + * {@inheritdoc} */ public function getBCEntity() { return $this; } /** - * Implements \Drupal\Core\Entity\EntityInterface::getNGEntity(). + * {@inheritdoc} */ public function getNGEntity() { return $this; @@ -493,42 +510,42 @@ public function applyDefaultValue($notify = TRUE) { } /** - * Implements \Drupal\Core\TypedData\ComplexDataInterface::onChange(). + * {@inheritdoc} */ public function onChange($property_name) { // Nothing to do. } /** - * Implements \Drupal\Core\TypedData\TypedDataInterface::getName(). + * {@inheritdoc} */ public function getName() { return NULL; } /** - * Implements \Drupal\Core\TypedData\TypedDataInterface::getRoot(). + * {@inheritdoc} */ public function getRoot() { return $this; } /** - * Implements \Drupal\Core\TypedData\TypedDataInterface::getPropertyPath(). + * {@inheritdoc} */ public function getPropertyPath() { return ''; } /** - * Implements \Drupal\Core\TypedData\TypedDataInterface::getParent(). + * {@inheritdoc} */ public function getParent() { return NULL; } /** - * Implements \Drupal\Core\TypedData\TypedDataInterface::setContext(). + * {@inheritdoc} */ public function setContext($name = NULL, TypedDataInterface $parent = NULL) { // As entities are always the root of the tree of typed data, we do not need @@ -536,7 +553,7 @@ public function setContext($name = NULL, TypedDataInterface $parent = NULL) { } /** - * Implements \Drupal\Core\Entity\EntityInterface::isTranslatable(). + * {@inheritdoc} */ public function isTranslatable() { // @todo Inject the entity manager and retrieve bundle info from it. diff --git a/core/lib/Drupal/Core/Entity/EntityInterface.php b/core/lib/Drupal/Core/Entity/EntityInterface.php index e965b8e..fed1849 100644 --- a/core/lib/Drupal/Core/Entity/EntityInterface.php +++ b/core/lib/Drupal/Core/Entity/EntityInterface.php @@ -155,6 +155,22 @@ public function save(); public function delete(); /** + * Instantiates a new instance of this entity form controller. + * + * This is a factory method that returns a new instance of this object. The + * factory should pass any needed dependencies into the constructor of this + * object, but not the container itself. Every call to this method must return + * a new instance of this object; that is, it may not implement a singleton. + * + * @param \Symfony\Component\DependencyInjection\ContainerInterface $container + * The service container this object should use. + * + * @return static + * A new instance of the entity form controller. + */ + public static function create(ContainerInterface $container); + + /** * Acts on an entity before the presave hook is invoked. * * Used before the entity is saved and before invoking the presave hook. diff --git a/core/lib/Drupal/Core/Entity/EntityNG.php b/core/lib/Drupal/Core/Entity/EntityNG.php index 8ab78e4..0884de3 100644 --- a/core/lib/Drupal/Core/Entity/EntityNG.php +++ b/core/lib/Drupal/Core/Entity/EntityNG.php @@ -135,10 +135,11 @@ class EntityNG extends Entity { protected $translationInitialize = FALSE; /** - * Overrides Entity::__construct(). + * {@inheritdoc} */ - public function __construct(array $values, $entity_type, $bundle = FALSE, $translations = array()) { + public function __construct($entityManager, array $values, $entity_type, $bundle = FALSE, $translations = array()) { $this->entityType = $entity_type; + $this->entityManager = $entityManager; $this->bundle = $bundle ? $bundle : $this->entityType; $this->languages = language_list(Language::STATE_ALL); @@ -168,6 +169,19 @@ public function __construct(array $values, $entity_type, $bundle = FALSE, $trans } /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $values, $entity_type, $bundle = FALSE, $translations = array()) { + return new static( + $container->get('plugin.manager.entity'), + $values, + $entity_type, + $bundle, + $translations + ); + } + + /** * Initialize the object. Invoked upon construction and wake up. */ protected function init() { @@ -369,7 +383,7 @@ public function getPropertyDefinition($name) { public function getPropertyDefinitions() { if (!isset($this->fieldDefinitions)) { $bundle = $this->bundle != $this->entityType ? $this->bundle : NULL; - $this->fieldDefinitions = \Drupal::entityManager()->getFieldDefinitions($this->entityType, $bundle); + $this->fieldDefinitions = $this->entityManager->getFieldDefinitions($this->entityType, $bundle); } return $this->fieldDefinitions; } @@ -414,11 +428,11 @@ public function isEmpty() { */ public function access($operation = 'view', AccountInterface $account = NULL) { if ($operation == 'create') { - return \Drupal::entityManager() + return $this->entityManager ->getAccessController($this->entityType) ->createAccess($this->bundle(), $account); } - return \Drupal::entityManager() + return $this->entityManager ->getAccessController($this->entityType) ->access($this, $operation, $this->activeLangcode, $account); } @@ -587,7 +601,7 @@ public function addTranslation($langcode, array $values = array()) { // specified language. $info = $this->entityInfo(); $default_values = array($info['entity_keys']['bundle'] => $this->bundle, 'langcode' => $langcode); - $entity = \Drupal::entityManager() + $entity = $this->entityManager ->getStorageController($this->entityType()) ->createEntity($default_values); diff --git a/core/modules/block/lib/Drupal/block/BlockAccessController.php b/core/modules/block/lib/Drupal/block/BlockAccessController.php index 26716c2..c223bdc 100644 --- a/core/modules/block/lib/Drupal/block/BlockAccessController.php +++ b/core/modules/block/lib/Drupal/block/BlockAccessController.php @@ -43,7 +43,7 @@ public function __construct($entity_type, AliasManagerInterface $alias_manager) /** * {@inheritdoc} */ - public static function createInstance(ContainerInterface $container, $entity_type, array $entity_info) { + public static function create(ContainerInterface $container, $entity_type, array $entity_info) { return new static( $entity_type, $container->get('path.alias_manager')