Commit 37d2299b authored by larowlan's avatar larowlan

Issue #2908271 by Jo Fitzgerald, swati.nuna, a.dmitriiev, TR, BramDriesen,...

Issue #2908271 by Jo Fitzgerald, swati.nuna, a.dmitriiev, TR, BramDriesen, Mile23: Replace deprecated \Drupal::entityManager() in EntityReferenceItem
parent bc73813e
......@@ -65,11 +65,11 @@ public static function defaultFieldSettings() {
*/
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$settings = $field_definition->getSettings();
$target_type_info = \Drupal::entityManager()->getDefinition($settings['target_type']);
$target_type_info = \Drupal::entityTypeManager()->getDefinition($settings['target_type']);
$target_id_data_type = 'string';
if ($target_type_info->entityClassImplements(FieldableEntityInterface::class)) {
$id_definition = \Drupal::entityManager()->getBaseFieldDefinitions($settings['target_type'])[$target_type_info->getKey('id')];
$id_definition = \Drupal::service('entity_field.manager')->getBaseFieldDefinitions($settings['target_type'])[$target_type_info->getKey('id')];
if ($id_definition->getType() === 'integer') {
$target_id_data_type = 'integer';
}
......@@ -114,7 +114,7 @@ public static function mainPropertyName() {
*/
public static function schema(FieldStorageDefinitionInterface $field_definition) {
$target_type = $field_definition->getSetting('target_type');
$target_type_info = \Drupal::entityManager()->getDefinition($target_type);
$target_type_info = \Drupal::entityTypeManager()->getDefinition($target_type);
$properties = static::propertyDefinitions($field_definition)['target_id'];
if ($target_type_info->entityClassImplements(FieldableEntityInterface::class) && $properties->getDataType() === 'integer') {
$columns = [
......@@ -287,7 +287,7 @@ public static function generateSampleValue(FieldDefinitionInterface $field_defin
'entity' => NULL,
];
$entity_type = \Drupal::entityManager()->getDefinition($options['target_type']);
$entity_type = \Drupal::entityTypeManager()->getDefinition($options['target_type']);
$options['handler_settings']['sort'] = [
'field' => $entity_type->getKey('id'),
'direction' => 'DESC',
......@@ -466,14 +466,15 @@ public function hasNewEntity() {
*/
public static function calculateDependencies(FieldDefinitionInterface $field_definition) {
$dependencies = parent::calculateDependencies($field_definition);
$manager = \Drupal::entityManager();
$target_entity_type = $manager->getDefinition($field_definition->getFieldStorageDefinition()->getSetting('target_type'));
$entity_type_manager = \Drupal::entityTypeManager();
$target_entity_type = $entity_type_manager->getDefinition($field_definition->getFieldStorageDefinition()->getSetting('target_type'));
// Depend on default values entity types configurations.
if ($default_value = $field_definition->getDefaultValueLiteral()) {
$entity_repository = \Drupal::service('entity.repository');
foreach ($default_value as $value) {
if (is_array($value) && isset($value['target_uuid'])) {
$entity = \Drupal::service('entity.repository')->loadEntityByUuid($target_entity_type->id(), $value['target_uuid']);
$entity = $entity_repository->loadEntityByUuid($target_entity_type->id(), $value['target_uuid']);
// If the entity does not exist do not create the dependency.
// @see \Drupal\Core\Field\EntityReferenceFieldItemList::processDefaultValue()
if ($entity) {
......@@ -489,7 +490,7 @@ public static function calculateDependencies(FieldDefinitionInterface $field_def
$handler = $field_definition->getSetting('handler_settings');
if (!empty($handler['target_bundles'])) {
if ($bundle_entity_type_id = $target_entity_type->getBundleEntityType()) {
if ($storage = $manager->getStorage($bundle_entity_type_id)) {
if ($storage = $entity_type_manager->getStorage($bundle_entity_type_id)) {
foreach ($storage->loadMultiple($handler['target_bundles']) as $bundle) {
$dependencies[$bundle->getConfigDependencyKey()][] = $bundle->getConfigDependencyName();
}
......@@ -505,7 +506,7 @@ public static function calculateDependencies(FieldDefinitionInterface $field_def
*/
public static function calculateStorageDependencies(FieldStorageDefinitionInterface $field_definition) {
$dependencies = parent::calculateStorageDependencies($field_definition);
$target_entity_type = \Drupal::entityManager()->getDefinition($field_definition->getSetting('target_type'));
$target_entity_type = \Drupal::entityTypeManager()->getDefinition($field_definition->getSetting('target_type'));
$dependencies['module'][] = $target_entity_type->getProvider();
return $dependencies;
}
......@@ -515,14 +516,15 @@ public static function calculateStorageDependencies(FieldStorageDefinitionInterf
*/
public static function onDependencyRemoval(FieldDefinitionInterface $field_definition, array $dependencies) {
$changed = parent::onDependencyRemoval($field_definition, $dependencies);
$entity_manager = \Drupal::entityManager();
$target_entity_type = $entity_manager->getDefinition($field_definition->getFieldStorageDefinition()->getSetting('target_type'));
$entity_type_manager = \Drupal::entityTypeManager();
$target_entity_type = $entity_type_manager->getDefinition($field_definition->getFieldStorageDefinition()->getSetting('target_type'));
// Try to update the default value config dependency, if possible.
if ($default_value = $field_definition->getDefaultValueLiteral()) {
$entity_repository = \Drupal::service('entity.repository');
foreach ($default_value as $key => $value) {
if (is_array($value) && isset($value['target_uuid'])) {
$entity = \Drupal::service('entity.repository')->loadEntityByUuid($target_entity_type->id(), $value['target_uuid']);
$entity = $entity_repository->loadEntityByUuid($target_entity_type->id(), $value['target_uuid']);
// @see \Drupal\Core\Field\EntityReferenceFieldItemList::processDefaultValue()
if ($entity && isset($dependencies[$entity->getConfigDependencyKey()][$entity->getConfigDependencyName()])) {
unset($default_value[$key]);
......@@ -541,7 +543,7 @@ public static function onDependencyRemoval(FieldDefinitionInterface $field_defin
$handler_settings = $field_definition->getSetting('handler_settings');
if (!empty($handler_settings['target_bundles'])) {
if ($bundle_entity_type_id = $target_entity_type->getBundleEntityType()) {
if ($storage = $entity_manager->getStorage($bundle_entity_type_id)) {
if ($storage = $entity_type_manager->getStorage($bundle_entity_type_id)) {
foreach ($storage->loadMultiple($handler_settings['target_bundles']) as $bundle) {
if (isset($dependencies[$bundle->getConfigDependencyKey()][$bundle->getConfigDependencyName()])) {
unset($handler_settings['target_bundles'][$bundle->id()]);
......@@ -687,7 +689,7 @@ public static function getPreconfiguredOptions() {
// Add all the commonly referenced entity types as distinct pre-configured
// options.
$entity_types = \Drupal::entityManager()->getDefinitions();
$entity_types = \Drupal::entityTypeManager()->getDefinitions();
$common_references = array_filter($entity_types, function (EntityTypeInterface $entity_type) {
return $entity_type->isCommonReferenceTarget();
});
......
......@@ -56,6 +56,21 @@ class EntityViewsDataTest extends UnitTestCase {
*/
protected $entityManager;
/**
* The mocked entity field manager.
*
* @var \Drupal\Core\Entity\EntityFieldManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $entityFieldManager;
/**
* The mocked entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $entityTypeManager;
/**
* The mocked module handler.
*
......@@ -85,6 +100,8 @@ protected function setUp() {
->disableOriginalConstructor()
->getMock();
$this->entityManager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
$this->entityFieldManager = $this->getMock('Drupal\Core\Entity\EntityFieldManagerInterface');
$this->entityTypeManager = $this->getMock('Drupal\Core\Entity\EntityTypeManagerInterface');
$typed_data_manager = $this->getMock(TypedDataManagerInterface::class);
$typed_data_manager->expects($this->any())
......@@ -135,6 +152,8 @@ protected function setUp() {
$container = new ContainerBuilder();
$container->set('plugin.manager.field.field_type', $field_type_manager);
$container->set('entity.manager', $this->entityManager);
$container->set('entity_field.manager', $this->entityFieldManager);
$container->set('entity_type.manager', $this->entityTypeManager);
$container->set('typed_data_manager', $typed_data_manager);
$container->set('state', $state->reveal());
\Drupal::setContainer($container);
......@@ -414,6 +433,12 @@ protected function setupFieldStorageDefinition() {
['user', TRUE, static::userEntityInfo()],
]
);
$this->entityTypeManager->expects($this->any())
->method('getDefinition')
->willReturnMap([
['user', TRUE, static::userEntityInfo()],
]
);
$user_id_field_storage_definition = $this->getMock('Drupal\Core\Field\FieldStorageDefinitionInterface');
$user_id_field_storage_definition->expects($this->any())
->method('getSetting')
......@@ -465,6 +490,12 @@ public function testBaseTableFields() {
['user', $user_base_field_definitions],
['entity_test', $base_field_definitions],
]));
$this->entityFieldManager->expects($this->any())
->method('getBaseFieldDefinitions')
->will($this->returnValueMap([
['user', $user_base_field_definitions],
['entity_test', $base_field_definitions],
]));
// Setup the table mapping.
$table_mapping = $this->getMockBuilder(DefaultTableMapping::class)
->disableOriginalConstructor()
......@@ -588,6 +619,12 @@ public function testDataTableFields() {
['user', $user_base_field_definitions],
['entity_test_mul', $base_field_definitions],
]));
$this->entityFieldManager->expects($this->any())
->method('getBaseFieldDefinitions')
->will($this->returnValueMap([
['user', $user_base_field_definitions],
['entity_test_mul', $base_field_definitions],
]));
$this->viewsData->setEntityType($entity_type);
......@@ -738,6 +775,12 @@ public function testRevisionTableFields() {
['user', $user_base_field_definitions],
['entity_test_mulrev', $base_field_definitions],
]));
$this->entityFieldManager->expects($this->any())
->method('getBaseFieldDefinitions')
->will($this->returnValueMap([
['user', $user_base_field_definitions],
['entity_test_mulrev', $base_field_definitions],
]));
$this->viewsData->setEntityType($entity_type);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment