Issue #3052233 by mikelutz, Berdir, alexpott: Properly deprecate EntityFormInterface::setEntityManager() and trigger an error on use
parent
cda61a5764
commit
668e08a7bf
|
@ -547,10 +547,9 @@ services:
|
||||||
# @deprecated in Drupal 8.0.x and will be removed before 9.0.0. Use the other
|
# @deprecated in Drupal 8.0.x and will be removed before 9.0.0. Use the other
|
||||||
# entity* services instead.
|
# entity* services instead.
|
||||||
entity.manager:
|
entity.manager:
|
||||||
# We cannot set the deprecated property here because many test cases still
|
|
||||||
# rely on this service and they would fail with deprecation warnings.
|
|
||||||
class: Drupal\Core\Entity\EntityManager
|
class: Drupal\Core\Entity\EntityManager
|
||||||
parent: container.trait
|
parent: container.trait
|
||||||
|
deprecated: The "%service_id%" service is deprecated. You should use the 'entity_type.manager' service instead.
|
||||||
entity.memory_cache:
|
entity.memory_cache:
|
||||||
class: Drupal\Core\Cache\MemoryCache\MemoryCache
|
class: Drupal\Core\Cache\MemoryCache\MemoryCache
|
||||||
entity_type.manager:
|
entity_type.manager:
|
||||||
|
|
|
@ -35,13 +35,14 @@ class EntityForm extends FormBase implements EntityFormInterface {
|
||||||
/**
|
/**
|
||||||
* The entity manager.
|
* The entity manager.
|
||||||
*
|
*
|
||||||
* @var \Drupal\Core\Entity\EntityManagerInterface
|
* This member exists for BC reasons and should be removed when the
|
||||||
|
* drupal:9.0.0 branch opens.
|
||||||
*
|
*
|
||||||
* @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0.
|
* @var \Drupal\Core\Entity\EntityManagerInterface
|
||||||
*
|
*
|
||||||
* @see https://www.drupal.org/node/2549139
|
* @see https://www.drupal.org/node/2549139
|
||||||
*/
|
*/
|
||||||
protected $entityManager;
|
private $privateEntityManager;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The entity type manager.
|
* The entity type manager.
|
||||||
|
@ -57,6 +58,32 @@ class EntityForm extends FormBase implements EntityFormInterface {
|
||||||
*/
|
*/
|
||||||
protected $entity;
|
protected $entity;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function __get($name) {
|
||||||
|
// Removing core's usage of ::setEntityManager means that this deprecated
|
||||||
|
// service wont be set. We provide it here for backwards compatibility.
|
||||||
|
if ($name === 'entityManager') {
|
||||||
|
@trigger_error('EntityForm::entityManager is deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use EntityForm::entityTypeManager instead. See https://www.drupal.org/node/2549139', E_USER_DEPRECATED);
|
||||||
|
return $this->privateEntityManager ?: \Drupal::entityManager();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function __set($name, $value) {
|
||||||
|
// We've changed the entityManager property from protected to private so
|
||||||
|
// access is funnelled through __get above. This method is provided for BC
|
||||||
|
// purposes, in case any extended class attempts to set the previously
|
||||||
|
// accessible property directly.
|
||||||
|
if ($name === 'entityManager') {
|
||||||
|
@trigger_error('EntityForm::entityManager is deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use EntityForm::entityTypeManager instead. See https://www.drupal.org/node/2549139', E_USER_DEPRECATED);
|
||||||
|
$this->privateEntityManager = $value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
|
@ -429,7 +456,8 @@ class EntityForm extends FormBase implements EntityFormInterface {
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function setEntityManager(EntityManagerInterface $entity_manager) {
|
public function setEntityManager(EntityManagerInterface $entity_manager) {
|
||||||
$this->entityManager = $entity_manager;
|
@trigger_error('EntityForm::setEntityTypeManager() is deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use EntityFormInterface::setEntityTypeManager() instead. See https://www.drupal.org/node/2549139', E_USER_DEPRECATED);
|
||||||
|
$this->privateEntityManager = $entity_manager;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -133,9 +133,10 @@ interface EntityFormInterface extends BaseFormIdInterface {
|
||||||
*
|
*
|
||||||
* @return $this
|
* @return $this
|
||||||
*
|
*
|
||||||
* @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0.
|
* @deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use
|
||||||
|
* EntityFormInterface::setEntityTypeManager() instead.
|
||||||
*
|
*
|
||||||
* @todo Remove this set call in https://www.drupal.org/node/2603542.
|
* @see https://www.drupal.org/node/2549139
|
||||||
*/
|
*/
|
||||||
public function setEntityManager(EntityManagerInterface $entity_manager);
|
public function setEntityManager(EntityManagerInterface $entity_manager);
|
||||||
|
|
||||||
|
|
|
@ -229,10 +229,7 @@ class EntityTypeManager extends DefaultPluginManager implements EntityTypeManage
|
||||||
->setStringTranslation($this->stringTranslation)
|
->setStringTranslation($this->stringTranslation)
|
||||||
->setModuleHandler($this->moduleHandler)
|
->setModuleHandler($this->moduleHandler)
|
||||||
->setEntityTypeManager($this)
|
->setEntityTypeManager($this)
|
||||||
->setOperation($operation)
|
->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::service('entity.manager'));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -15,19 +15,18 @@ use Drupal\Tests\UnitTestCase;
|
||||||
class ContentEntityFormTest extends UnitTestCase {
|
class ContentEntityFormTest extends UnitTestCase {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Tests the constructor bc layer for injecting the entity manager.
|
||||||
|
*
|
||||||
* @group legacy
|
* @group legacy
|
||||||
* @expectedDeprecation Passing the entity.manager service to ContentEntityForm::__construct() is deprecated in Drupal 8.6.0 and will be removed before Drupal 9.0.0. Pass the entity.repository service instead. See https://www.drupal.org/node/2549139.
|
* @expectedDeprecation Passing the entity.manager service to ContentEntityForm::__construct() is deprecated in Drupal 8.6.0 and will be removed before Drupal 9.0.0. Pass the entity.repository service instead. See https://www.drupal.org/node/2549139.
|
||||||
|
* @expectedDeprecation EntityForm::entityManager is deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use EntityForm::entityTypeManager instead. See https://www.drupal.org/node/2549139
|
||||||
*/
|
*/
|
||||||
public function testEntityManagerDeprecation() {
|
public function testEntityManagerDeprecation() {
|
||||||
$entity_manager = $this->prophesize(EntityManagerInterface::class)->reveal();
|
$entity_manager = $this->prophesize(EntityManagerInterface::class)->reveal();
|
||||||
$entity_type_bundle_info = $this->prophesize(EntityTypeBundleInfoInterface::class)->reveal();
|
$entity_type_bundle_info = $this->prophesize(EntityTypeBundleInfoInterface::class)->reveal();
|
||||||
$time = $this->prophesize(TimeInterface::class)->reveal();
|
$time = $this->prophesize(TimeInterface::class)->reveal();
|
||||||
$form = new ContentEntityForm($entity_manager, $entity_type_bundle_info, $time);
|
$form = new ContentEntityForm($entity_manager, $entity_type_bundle_info, $time);
|
||||||
|
$this->assertSame($form->entityManager, $entity_manager);
|
||||||
$reflected_form = new \ReflectionClass($form);
|
|
||||||
$entity_manager_property = $reflected_form->getProperty('entityManager');
|
|
||||||
$entity_manager_property->setAccessible(TRUE);
|
|
||||||
$this->assertTrue($entity_manager_property->getValue($form) === $entity_manager);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,8 +2,10 @@
|
||||||
|
|
||||||
namespace Drupal\Tests\Core\Entity;
|
namespace Drupal\Tests\Core\Entity;
|
||||||
|
|
||||||
|
use Drupal\Core\DependencyInjection\ContainerBuilder;
|
||||||
use Drupal\Core\Entity\EntityForm;
|
use Drupal\Core\Entity\EntityForm;
|
||||||
use Drupal\Core\Entity\EntityInterface;
|
use Drupal\Core\Entity\EntityInterface;
|
||||||
|
use Drupal\Core\Entity\EntityManager;
|
||||||
use Drupal\Core\Entity\EntityStorageInterface;
|
use Drupal\Core\Entity\EntityStorageInterface;
|
||||||
use Drupal\Core\Entity\EntityType;
|
use Drupal\Core\Entity\EntityType;
|
||||||
use Drupal\Core\Entity\EntityTypeManagerInterface;
|
use Drupal\Core\Entity\EntityTypeManagerInterface;
|
||||||
|
@ -226,6 +228,48 @@ class EntityFormTest extends UnitTestCase {
|
||||||
$this->assertEquals($entity, $actual);
|
$this->assertEquals($entity, $actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests that setEntityManager triggers proper deprecation errors.
|
||||||
|
*
|
||||||
|
* @covers ::setEntityManager
|
||||||
|
*
|
||||||
|
* @group legacy
|
||||||
|
*
|
||||||
|
* @expectedDeprecation EntityForm::setEntityTypeManager() is deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use EntityFormInterface::setEntityTypeManager() instead. See https://www.drupal.org/node/2549139
|
||||||
|
*/
|
||||||
|
public function testSetEntityManager() {
|
||||||
|
$this->entityForm->setEntityManager($this->prophesize(EntityManager::class)->reveal());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests that __set triggers proper deprecation errors.
|
||||||
|
*
|
||||||
|
* @covers ::__set
|
||||||
|
*
|
||||||
|
* @group legacy
|
||||||
|
*
|
||||||
|
* @expectedDeprecation EntityForm::entityManager is deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use EntityForm::entityTypeManager instead. See https://www.drupal.org/node/2549139
|
||||||
|
*/
|
||||||
|
public function testSet() {
|
||||||
|
$this->entityForm->entityManager = $this->prophesize(EntityManager::class)->reveal();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests that __get triggers proper deprecation errors.
|
||||||
|
*
|
||||||
|
* @covers ::__get
|
||||||
|
* @group legacy
|
||||||
|
*
|
||||||
|
* @expectedDeprecation EntityForm::entityManager is deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use EntityForm::entityTypeManager instead. See https://www.drupal.org/node/2549139
|
||||||
|
*/
|
||||||
|
public function testGet() {
|
||||||
|
$container = new ContainerBuilder();
|
||||||
|
$entity_manager = $this->prophesize(EntityManager::class)->reveal();
|
||||||
|
$container->set('entity.manager', $entity_manager);
|
||||||
|
\Drupal::setContainer($container);
|
||||||
|
$this->assertSame($entity_manager, $this->entityForm->entityManager);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets up the storage accessed via the entity type manager in the form.
|
* Sets up the storage accessed via the entity type manager in the form.
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in New Issue