diff -u b/core/lib/Drupal/Core/Config/Entity/ConfigEntityInterface.php b/core/lib/Drupal/Core/Config/Entity/ConfigEntityInterface.php --- b/core/lib/Drupal/Core/Config/Entity/ConfigEntityInterface.php +++ b/core/lib/Drupal/Core/Config/Entity/ConfigEntityInterface.php @@ -28,23 +28,21 @@ * @param string $id * The new ID to set as original ID. * - * @return self + * @return $this */ public function setOriginalId($id); /** * Enables the configuration entity. * - * @return \Drupal\Core\Config\Entity\ConfigEntityInterface - * The configuration entity. + * @return $this */ public function enable(); /** * Disables the configuration entity. * - * @return \Drupal\Core\Config\Entity\ConfigEntityInterface - * The configuration entity. + * @return $this */ public function disable(); @@ -54,8 +52,7 @@ * @param bool $status * The status of the configuration entity. * - * @return \Drupal\Core\Config\Entity\ConfigEntityInterface - * The class instance that this method is called on. + * @return $this */ public function setStatus($status); @@ -65,7 +62,7 @@ * @param bool $status * The status of the sync flag. * - * @return self + * @return $this */ public function setSyncing($status); @@ -112,7 +109,7 @@ * @param mixed $value * The value the property should be set to. * - * @return self + * @return $this */ public function set($property_name, $value); diff -u b/core/lib/Drupal/Core/Entity/ContentEntityBase.php b/core/lib/Drupal/Core/Entity/ContentEntityBase.php --- b/core/lib/Drupal/Core/Entity/ContentEntityBase.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityBase.php @@ -129,6 +129,13 @@ protected $isDefaultRevision = TRUE; /** + * The typed data manager. + * + * @var \Drupal\Core\TypedData\TypedDataManager + */ + protected $typedDataManager; + + /** * Overrides Entity::__construct(). */ public function __construct(array $values, $entity_type, $bundle = FALSE, $translations = array()) { @@ -174,7 +181,11 @@ * @return \Drupal\Core\TypedData\TypedDataManager */ protected function typedDataManager() { - return \Drupal::typedDataManager(); + if (!$this->typedDataManager) { + $this->typedDataManager = \Drupal::typedDataManager(); + } + + return $this->typedDataManager; } /** @@ -213,7 +224,6 @@ * {@inheritdoc} */ public function isTranslatable() { - // @todo Inject the entity manager and retrieve bundle info from it. $bundles = $this->entityManager()->getBundleInfo($this->entityTypeId); return !empty($bundles[$this->bundle()]['translatable']); } @@ -922,7 +932,6 @@ // Check if the entity type supports UUIDs and generate a new one if so. if ($entity_info->hasKey('uuid')) { - // @todo Inject the UUID service into the Entity class once possible. $duplicate->{$entity_info->getKey('uuid')}->value = $this->uuidGenerator()->generate(); } interdiff impossible; taking evasive action reverted: --- b/core/lib/Drupal/Core/Entity/Entity.php +++ a/core/lib/Drupal/Core/Entity/Entity.php @@ -24,20 +24,6 @@ public $langcode = Language::LANGCODE_NOT_SPECIFIED; /** - * The entity manager. - * - * @var \Drupal\Core\Entity\EntityManagerInterface - */ - protected $entityManager; - - /** - * The UUID generator. - * - * @var \Drupal\Component\Uuid\UuidInterface - */ - protected $uuidGenerator; - - /** * The entity type. * * @var string @@ -83,39 +69,6 @@ } /** - * Wraps language_load(). - */ - protected function languageLoad($langcode) { - return language_load($langcode); - } - - /** - * Returns the entity manager. - * - * @return \Drupal\Core\Entity\EntityManagerInterface - */ - protected function entityManager() { - if (!$this->entityManager) { - $this->entityManager = \Drupal::entityManager(); - } - - return $this->entityManager; - } - - /** - * Returns the UUID generator. - * - * @return \Drupal\Component\Uuid\UuidInterface - */ - protected function uuidGenerator() { - if (!$this->uuidGenerator) { - $this->uuidGenerator = \Drupal::service('uuid'); - } - - return $this->uuidGenerator; - } - - /** * {@inheritdoc} */ public function id() { @@ -141,8 +94,6 @@ */ public function enforceIsNew($value = TRUE) { $this->enforceIsNew = $value; - - return $this; } /** @@ -165,8 +116,9 @@ public function label() { $label = NULL; $entity_info = $this->getEntityType(); + // @todo Convert to is_callable() and call_user_func(). + if (($label_callback = $entity_info->getLabelCallback()) && function_exists($label_callback)) { + $label = $label_callback($this); - if (($label_callback = $entity_info->getLabelCallback()) && is_callable($label_callback)) { - $label = call_user_func($label_callback, $this); } elseif (($label_key = $entity_info->getKey('label')) && isset($this->{$label_key})) { $label = $this->{$label_key}; @@ -237,7 +189,7 @@ $bundle = $this->bundle(); // A bundle-specific callback takes precedence over the generic one for // the entity type. + $bundles = entity_get_bundles($this->entityTypeId); - $bundles = $this->entityManager()->getBundleInfo($this->entityTypeId); if (isset($bundles[$bundle]['uri_callback'])) { $uri_callback = $bundles[$bundle]['uri_callback']; } @@ -247,8 +199,9 @@ // Invoke the callback to get the URI. If there is no callback, use the // default URI format. + // @todo Convert to is_callable() and call_user_func(). + if (isset($uri_callback) && function_exists($uri_callback)) { + $uri = $uri_callback($this); - if (isset($uri_callback) && is_callable($uri_callback)) { - $uri = call_user_func($uri_callback, $this); } // Only use these defaults for a canonical link (that is, a link to self). // Other relationship types are not supported by this logic. @@ -315,13 +268,13 @@ /** * {@inheritdoc} */ + public function access($operation = 'view', AccountInterface $account = NULL) { - public function access($operation, AccountInterface $account = NULL) { if ($operation == 'create') { + return \Drupal::entityManager() - return $this->entityManager() ->getAccessController($this->entityTypeId) ->createAccess($this->bundle(), $account); } + return \Drupal::entityManager() - return $this->entityManager() ->getAccessController($this->entityTypeId) ->access($this, $operation, Language::LANGCODE_DEFAULT, $account); } @@ -330,7 +283,7 @@ * {@inheritdoc} */ public function language() { + $language = language_load($this->langcode); - $language = $this->languageLoad($this->langcode); if (!$language) { // Make sure we return a proper language object. $language = new Language(array('id' => Language::LANGCODE_NOT_SPECIFIED)); @@ -342,7 +295,7 @@ * {@inheritdoc} */ public function save() { + return \Drupal::entityManager()->getStorageController($this->entityTypeId)->save($this); - return $this->entityManager()->getStorageController($this->entityTypeId)->save($this); } /** @@ -350,7 +303,7 @@ */ public function delete() { if (!$this->isNew()) { + \Drupal::entityManager()->getStorageController($this->entityTypeId)->delete(array($this->id() => $this)); - $this->entityManager()->getStorageController($this->entityTypeId)->delete(array($this->id() => $this)); } } @@ -364,7 +317,8 @@ // Check if the entity type supports UUIDs and generate a new one if so. if ($entity_info->hasKey('uuid')) { + // @todo Inject the UUID service into the Entity class once possible. + $duplicate->{$entity_info->getKey('uuid')} = \Drupal::service('uuid')->generate(); - $duplicate->{$entity_info->getKey('uuid')} = $this->uuidGenerator()->generate(); } return $duplicate; } @@ -373,7 +327,7 @@ * {@inheritdoc} */ public function getEntityType() { + return \Drupal::entityManager()->getDefinition($this->getEntityTypeId()); - return $this->entityManager()->getDefinition($this->getEntityTypeId()); } /** @@ -442,8 +396,8 @@ } foreach ($referenced_entities as $entity_type => $entities) { + if (\Drupal::entityManager()->hasController($entity_type, 'view_builder')) { + \Drupal::entityManager()->getViewBuilder($entity_type)->resetCache($entities); - if ($this->entityManager()->hasController($entity_type, 'view_builder')) { - $this->entityManager()->getViewBuilder($entity_type)->resetCache($entities); } } } unchanged: --- a/core/lib/Drupal/Core/Entity/Entity.php +++ b/core/lib/Drupal/Core/Entity/Entity.php @@ -23,6 +23,20 @@ public $langcode = Language::LANGCODE_NOT_SPECIFIED; /** + * The entity manager. + * + * @var \Drupal\Core\Entity\EntityManagerInterface + */ + protected $entityManager; + + /** + * The UUID generator. + * + * @var \Drupal\Component\Uuid\UuidInterface + */ + protected $uuidGenerator; + + /** * The entity type. * * @var string @@ -61,6 +75,39 @@ public function __construct(array $values, $entity_type) { } /** + * Wraps language_load(). + */ + protected function languageLoad($langcode) { + return language_load($langcode); + } + + /** + * Returns the entity manager. + * + * @return \Drupal\Core\Entity\EntityManagerInterface + */ + protected function entityManager() { + if (!$this->entityManager) { + $this->entityManager = \Drupal::entityManager(); + } + + return $this->entityManager; + } + + /** + * Returns the UUID generator. + * + * @return \Drupal\Component\Uuid\UuidInterface + */ + protected function uuidGenerator() { + if (!$this->uuidGenerator) { + $this->uuidGenerator = \Drupal::service('uuid'); + } + + return $this->uuidGenerator; + } + + /** * {@inheritdoc} */ public function id() { @@ -86,6 +133,8 @@ public function isNew() { */ public function enforceIsNew($value = TRUE) { $this->enforceIsNew = $value; + + return $this; } /** @@ -108,9 +157,8 @@ public function bundle() { public function label() { $label = NULL; $entity_info = $this->getEntityType(); - // @todo Convert to is_callable() and call_user_func(). - if (($label_callback = $entity_info->getLabelCallback()) && function_exists($label_callback)) { - $label = $label_callback($this); + if (($label_callback = $entity_info->getLabelCallback()) && is_callable($label_callback)) { + $label = call_user_func($label_callback, $this); } elseif (($label_key = $entity_info->getKey('label')) && isset($this->{$label_key})) { $label = $this->{$label_key}; @@ -138,7 +186,7 @@ public function urlInfo($rel = 'canonical') { $bundle = $this->bundle(); // A bundle-specific callback takes precedence over the generic one for // the entity type. - $bundles = \Drupal::entityManager()->getBundleInfo($this->getEntityTypeId()); + $bundles = $this->entityManager()->getBundleInfo($this->getEntityTypeId()); if (isset($bundles[$bundle]['uri_callback'])) { $uri_callback = $bundles[$bundle]['uri_callback']; } @@ -148,9 +196,8 @@ public function urlInfo($rel = 'canonical') { // Invoke the callback to get the URI. If there is no callback, use the // default URI format. - // @todo Convert to is_callable() and call_user_func(). - if (isset($uri_callback) && function_exists($uri_callback)) { - $uri = $uri_callback($this); + if (isset($uri_callback) && is_callable($uri_callback)) { + $uri = call_user_func($uri_callback, $this); } else { return array(); @@ -247,13 +294,13 @@ public function uriRelationships() { /** * {@inheritdoc} */ - public function access($operation = 'view', AccountInterface $account = NULL) { + public function access($operation, AccountInterface $account = NULL) { if ($operation == 'create') { - return \Drupal::entityManager() + return $this->entityManager() ->getAccessController($this->entityTypeId) ->createAccess($this->bundle(), $account); } - return \Drupal::entityManager() + return $this->entityManager() ->getAccessController($this->entityTypeId) ->access($this, $operation, Language::LANGCODE_DEFAULT, $account); } @@ -262,7 +309,7 @@ public function access($operation = 'view', AccountInterface $account = NULL) { * {@inheritdoc} */ public function language() { - $language = language_load($this->langcode); + $language = $this->languageLoad($this->langcode); if (!$language) { // Make sure we return a proper language object. $language = new Language(array('id' => Language::LANGCODE_NOT_SPECIFIED)); @@ -274,7 +321,7 @@ public function language() { * {@inheritdoc} */ public function save() { - return \Drupal::entityManager()->getStorageController($this->entityTypeId)->save($this); + return $this->entityManager()->getStorageController($this->entityTypeId)->save($this); } /** @@ -282,7 +329,7 @@ public function save() { */ public function delete() { if (!$this->isNew()) { - \Drupal::entityManager()->getStorageController($this->entityTypeId)->delete(array($this->id() => $this)); + $this->entityManager()->getStorageController($this->entityTypeId)->delete(array($this->id() => $this)); } } @@ -296,8 +343,7 @@ public function createDuplicate() { // Check if the entity type supports UUIDs and generate a new one if so. if ($entity_info->hasKey('uuid')) { - // @todo Inject the UUID service into the Entity class once possible. - $duplicate->{$entity_info->getKey('uuid')} = \Drupal::service('uuid')->generate(); + $duplicate->{$entity_info->getKey('uuid')} = $this->uuidGenerator()->generate(); } return $duplicate; } @@ -306,7 +352,7 @@ public function createDuplicate() { * {@inheritdoc} */ public function getEntityType() { - return \Drupal::entityManager()->getDefinition($this->getEntityTypeId()); + return $this->entityManager()->getDefinition($this->getEntityTypeId()); } /** @@ -344,6 +390,7 @@ public static function preDelete(EntityStorageControllerInterface $storage_contr * {@inheritdoc} */ public static function postDelete(EntityStorageControllerInterface $storage_controller, array $entities) { + /** @var self[] $entities */ foreach ($entities as $entity) { $entity->onSaveOrDelete(); } @@ -375,8 +422,8 @@ protected function onSaveOrDelete() { } foreach ($referenced_entities as $entity_type => $entities) { - if (\Drupal::entityManager()->hasController($entity_type, 'view_builder')) { - \Drupal::entityManager()->getViewBuilder($entity_type)->resetCache($entities); + if ($this->entityManager()->hasController($entity_type, 'view_builder')) { + $this->entityManager()->getViewBuilder($entity_type)->resetCache($entities); } } } diff -u b/core/lib/Drupal/Core/Entity/EntityInterface.php b/core/lib/Drupal/Core/Entity/EntityInterface.php --- b/core/lib/Drupal/Core/Entity/EntityInterface.php +++ b/core/lib/Drupal/Core/Entity/EntityInterface.php @@ -258,7 +258,7 @@ * * @param EntityStorageControllerInterface $storage_controller * The entity storage controller object. - * @param array $entities + * @param \Drupal\Core\Entity\EntityInterface[] $entities * An array of entities. */ public static function postDelete(EntityStorageControllerInterface $storage_controller, array $entities); @@ -293,8 +293,7 @@ /** * Returns a list of entities referenced by this entity. * - * @return array - * An array of entities. + * @return \Drupal\Core\Entity\EntityInterface[] */ public function referencedEntities(); reverted: --- b/core/modules/system/lib/Drupal/system/Tests/Entity/FieldSqlStorageTest.php +++ a/core/modules/system/lib/Drupal/system/Tests/Entity/FieldSqlStorageTest.php @@ -258,7 +258,7 @@ */ function testLongNames() { // Use one of the longest entity_type names in core. + $entity_type = $bundle = 'entity_test_label_callback'; - $entity_type = $bundle = 'entity_test_label'; $storage_controller = $this->container->get('entity.manager')->getStorageController($entity_type); // Create two fields with instances, and generate randome values. diff -u b/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityBaseUnitTest.php b/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityBaseUnitTest.php --- b/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityBaseUnitTest.php +++ b/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityBaseUnitTest.php @@ -7,6 +7,7 @@ namespace Drupal\Tests\Core\Config\Entity; +use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Tests\UnitTestCase; /** @@ -45,13 +46,6 @@ protected $entityType; /** - * The route provider used for testing. - * - * @var \Drupal\Core\Routing\RouteProviderInterface|\PHPUnit_Framework_MockObject_MockObject - */ - protected $routeProvider; - - /** * The UUID generator used for testing. * * @var \Drupal\Component\Uuid\UuidInterface|\PHPUnit_Framework_MockObject_MockObject @@ -84,24 +78,18 @@ ->with($this->entityType) ->will($this->returnValue($this->entityInfo)); - $this->routeProvider = $this->getMock('\Drupal\Core\Routing\RouteProviderInterface'); - $this->uuid = $this->getMock('\Drupal\Component\Uuid\UuidInterface'); + $container = new ContainerBuilder(); + $container->set('entity.manager', $this->entityManager); + $container->set('uuid', $this->uuid); + \Drupal::setContainer($container); + $this->entity = $this->getMockBuilder('\Drupal\Core\Config\Entity\ConfigEntityBase') ->setConstructorArgs(array($values, $this->entityType)) - ->setMethods(array('entityManager', 'languageLoad', 'routeProvider', 'uuidGenerator')) + ->setMethods(array('languageLoad')) ->getMock(); $this->entity->expects($this->any()) - ->method('entityManager') - ->will($this->returnValue($this->entityManager)); - $this->entity->expects($this->any()) - ->method('routeProvider') - ->will($this->returnValue($this->routeProvider)); - $this->entity->expects($this->any()) - ->method('uuidGenerator') - ->will($this->returnValue($this->uuid)); - $this->entity->expects($this->any()) ->method('languageLoad') ->will($this->returnValue(NULL)); } @@ -238,35 +226,2 @@ } - - /** - * @covers ::uri - */ - public function testUri() { - // The only difference between ConfigEntityBase::uri() and Entity::uri() is - // how link templates are processed, so only test those. - $this->entity->id = $this->randomName(); - $expected_template_path = 'foo/' . $this->entity->id(); - $route_name = 'foo.bar'; - $route_path = 'foo/{id}'; - - $this->entityInfo->expects($this->any()) - ->method('getLinkTemplates') - ->will($this->returnValue(array( - 'edit-form' => $route_name, - ))); - - $route = $this->getMockBuilder('\Symfony\Component\Routing\Route') - ->disableOriginalConstructor() - ->getMock(); - $route->expects($this->once()) - ->method('getPath') - ->will($this->returnValue($route_path)); - - $this->routeProvider->expects($this->any()) - ->method('getRouteByName') - ->with($route_name) - ->will($this->returnValue($route)); - - $actual_template_uri = $this->entity->uri(); - $this->assertSame($expected_template_path, $actual_template_uri['path']); - } } diff -u b/core/tests/Drupal/Tests/Core/Entity/EntityUnitTest.php b/core/tests/Drupal/Tests/Core/Entity/EntityUnitTest.php --- b/core/tests/Drupal/Tests/Core/Entity/EntityUnitTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/EntityUnitTest.php @@ -7,6 +7,7 @@ namespace Drupal\Tests\Core\Entity; +use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Tests\UnitTestCase; /** @@ -84,28 +85,20 @@ ->with($this->entityTypeId) ->will($this->returnValue($this->entityInfo)); - $this->routeProvider = $this->getMockBuilder('\Drupal\Core\Routing\RouteProvider') - ->disableOriginalConstructor() - ->getMock(); - $this->uuid = $this->getMock('\Drupal\Component\Uuid\UuidInterface'); + $container = new ContainerBuilder(); + $container->set('entity.manager', $this->entityManager); + $container->set('uuid', $this->uuid); + \Drupal::setContainer($container); + $this->entity = $this->getMockBuilder('\Drupal\Core\Entity\Entity') ->setConstructorArgs(array($values, $this->entityTypeId)) - ->setMethods(array('entityManager', 'uuidGenerator', 'languageLoad', 'routeProvider')) + ->setMethods(array('languageLoad')) ->getMock(); $this->entity->expects($this->any()) - ->method('entityManager') - ->will($this->returnValue($this->entityManager)); - $this->entity->expects($this->any()) ->method('languageLoad') ->will($this->returnValue(NULL)); - $this->entity->expects($this->any()) - ->method('routeProvider') - ->will($this->returnValue($this->routeProvider)); - $this->entity->expects($this->any()) - ->method('uuidGenerator') - ->will($this->returnValue($this->uuid)); } /** only in patch2: unchanged: --- a/core/lib/Drupal/Core/TypedData/ComplexDataInterface.php +++ b/core/lib/Drupal/Core/TypedData/ComplexDataInterface.php @@ -61,7 +61,7 @@ public function set($property_name, $value, $notify = TRUE); * @param bool $include_computed * If set to TRUE, computed properties are included. Defaults to FALSE. * - * @return array + * @return \Drupal\Core\TypedData\TypedDataInterface[] * An array of property objects implementing the TypedDataInterface, keyed * by property name. */