Skip to content
Snippets Groups Projects
Commit c9cb639c authored by Lucas Hedding's avatar Lucas Hedding Committed by Lucas Hedding
Browse files

Issue #3052510 by heddn, mikelutz, NickWilde: Entity manager deprecation fixes

parent d452202e
No related branches found
No related tags found
No related merge requests found
......@@ -3,10 +3,7 @@
namespace Drupal\migrate_plus\Plugin\migrate\process;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManagerInterface;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\Plugin\MigratePluginManager;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\Row;
use Symfony\Component\DependencyInjection\ContainerInterface;
......@@ -53,6 +50,13 @@ class EntityGenerate extends EntityLookup {
*/
protected $row;
/**
* The migrate executable.
*
* @var \Drupal\migrate\MigrateExecutableInterface
*/
protected $migrateExecutable;
/**
* The MigratePluginManager instance.
*
......@@ -67,42 +71,13 @@ class EntityGenerate extends EntityLookup {
*/
protected $getProcessPlugin;
/**
* EntityGenerate constructor.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $pluginId
* The plugin_id for the plugin instance.
* @param mixed $pluginDefinition
* The plugin implementation definition.
* @param \Drupal\migrate\Plugin\MigrationInterface $migration
* The migration.
* @param \Drupal\Core\Entity\EntityManagerInterface $entityManager
* The $entityManager instance.
* @param \Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManagerInterface $selectionPluginManager
* The $selectionPluginManager instance.
* @param \Drupal\migrate\Plugin\MigratePluginManager $processPluginManager
* The MigratePluginManager instance.
*/
public function __construct(array $configuration, $pluginId, $pluginDefinition, MigrationInterface $migration, EntityManagerInterface $entityManager, SelectionPluginManagerInterface $selectionPluginManager, MigratePluginManager $processPluginManager) {
parent::__construct($configuration, $pluginId, $pluginDefinition, $migration, $entityManager, $selectionPluginManager);
$this->processPluginManager = $processPluginManager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition, MigrationInterface $migration = NULL) {
return new static(
$configuration,
$pluginId,
$pluginDefinition,
$migration,
$container->get('entity.manager'),
$container->get('plugin.manager.entity_reference_selection'),
$container->get('plugin.manager.migrate.process')
);
$instance = parent::create($container, $configuration, $pluginId, $pluginDefinition, $migration);
$instance->processPluginManager = $container->get('plugin.manager.migrate.process');
return $instance;
}
/**
......@@ -130,7 +105,7 @@ class EntityGenerate extends EntityLookup {
*/
protected function generateEntity($value) {
if (!empty($value)) {
$entity = $this->entityManager
$entity = $this->entityTypeManager
->getStorage($this->lookupEntityType)
->create($this->entity($value));
$entity->save();
......
......@@ -3,8 +3,6 @@
namespace Drupal\migrate_plus\Plugin\migrate\process;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\MigrateException;
......@@ -63,11 +61,18 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
class EntityLookup extends ProcessPluginBase implements ContainerFactoryPluginInterface {
/**
* The entity manager.
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityManagerInterface
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityManager;
protected $entityTypeManager;
/**
* The field manager.
*
* @var \Drupal\Core\Entity\EntityFieldManagerInterface
*/
protected $entityFieldManager;
/**
* The migration.
......@@ -139,31 +144,23 @@ class EntityLookup extends ProcessPluginBase implements ContainerFactoryPluginIn
*/
protected $accessCheck = TRUE;
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $pluginId, $pluginDefinition, MigrationInterface $migration, EntityManagerInterface $entityManager, SelectionPluginManagerInterface $selectionPluginManager) {
parent::__construct($configuration, $pluginId, $pluginDefinition);
$this->migration = $migration;
$this->entityManager = $entityManager;
$this->selectionPluginManager = $selectionPluginManager;
$pluginIdParts = explode(':', $this->migration->getDestinationPlugin()->getPluginId());
$this->destinationEntityType = empty($pluginIdParts[1]) ? NULL : $pluginIdParts[1];
$this->destinationBundleKey = $this->destinationEntityType ? $this->entityManager->getDefinition($this->destinationEntityType)->getKey('bundle') : NULL;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition, MigrationInterface $migration = NULL) {
return new static(
$instance = new static(
$configuration,
$pluginId,
$pluginDefinition,
$migration,
$container->get('entity.manager'),
$container->get('plugin.manager.entity_reference_selection')
$pluginDefinition
);
$instance->migration = $migration;
$instance->entityTypeManager = $container->get('entity_type.manager');
$instance->entityFieldManager = $container->get('entity_field.manager');
$instance->selectionPluginManager = $container->get('plugin.manager.entity_reference_selection');
$pluginIdParts = explode(':', $instance->migration->getDestinationPlugin()->getPluginId());
$instance->destinationEntityType = empty($pluginIdParts[1]) ? NULL : $pluginIdParts[1];
$instance->destinationBundleKey = $instance->destinationEntityType ? $instance->entityTypeManager->getDefinition($instance->destinationEntityType)->getKey('bundle') : NULL;
return $instance;
}
/**
......@@ -214,7 +211,7 @@ class EntityLookup extends ProcessPluginBase implements ContainerFactoryPluginIn
// See if we can introspect the lookup properties from destination field.
if (!empty($this->migration->getProcess()[$this->destinationBundleKey][0]['default_value'])) {
$destinationEntityBundle = $this->migration->getProcess()[$this->destinationBundleKey][0]['default_value'];
$fieldConfig = $this->entityManager->getFieldDefinitions($this->destinationEntityType, $destinationEntityBundle)[$destinationProperty]->getConfig($destinationEntityBundle);
$fieldConfig = $this->entityFieldManager->getFieldDefinitions($this->destinationEntityType, $destinationEntityBundle)[$destinationProperty]->getConfig($destinationEntityBundle);
switch ($fieldConfig->getType()) {
case 'entity_reference':
if (empty($this->lookupBundle)) {
......@@ -231,9 +228,11 @@ class EntityLookup extends ProcessPluginBase implements ContainerFactoryPluginIn
// Make an assumption that if the selection handler can target more
// than one type of entity that we will use the first entity type.
$this->lookupEntityType = $this->lookupEntityType ?: reset($this->selectionPluginManager->createInstance($fieldConfig->getSetting('handler'))->getPluginDefinition()['entity_types']);
$this->lookupValueKey = $this->lookupValueKey ?: $this->entityManager->getDefinition($this->lookupEntityType)->getKey('label');
$this->lookupBundleKey = $this->lookupBundleKey ?: $this->entityManager->getDefinition($this->lookupEntityType)->getKey('bundle');
$fieldHandler = $fieldConfig->getSetting('handler');
$selection = $this->selectionPluginManager->createInstance($fieldHandler);
$this->lookupEntityType = $this->lookupEntityType ?: reset($selection->getPluginDefinition()['entity_types']);
$this->lookupValueKey = $this->lookupValueKey ?: $this->entityTypeManager->getDefinition($this->lookupEntityType)->getKey('label');
$this->lookupBundleKey = $this->lookupBundleKey ?: $this->entityTypeManager->getDefinition($this->lookupEntityType)->getKey('bundle');
break;
case 'file':
......@@ -278,7 +277,7 @@ class EntityLookup extends ProcessPluginBase implements ContainerFactoryPluginIn
$multiple = is_array($value);
$query = $this->entityManager->getStorage($this->lookupEntityType)
$query = $this->entityTypeManager->getStorage($this->lookupEntityType)
->getQuery()
->accessCheck($this->accessCheck)
->condition($this->lookupValueKey, $value, $multiple ? 'IN' : NULL);
......@@ -301,7 +300,7 @@ class EntityLookup extends ProcessPluginBase implements ContainerFactoryPluginIn
if (!$ignoreCase) {
// Returns the entity's identifier.
foreach ($results as $k => $identifier) {
$entity = $this->entityManager->getStorage($this->lookupEntityType)->load($identifier);
$entity = $this->entityTypeManager->getStorage($this->lookupEntityType)->load($identifier);
$result_value = $entity instanceof ConfigEntityInterface ? $entity->get($this->lookupValueKey) : $entity->get($this->lookupValueKey)->value;
if (($multiple && !in_array($result_value, $value, TRUE)) || (!$multiple && $result_value !== $value)) {
unset($results[$k]);
......
......@@ -149,7 +149,7 @@ class EntityGenerateTest extends KernelTestBase implements MigrateMessageInterfa
/** @var \Drupal\migrate\Plugin\Migration $migration */
$migration = $this->migrationPluginManager->createStubMigration($definition);
/** @var EntityStorageBase $storage */
/** @var \Drupal\Core\Entity\EntityStorageBase $storage */
$storage = $this->readAttribute($migration->getDestinationPlugin(), 'storage');
$migrationExecutable = (new MigrateExecutable($migration, $this));
$migrationExecutable->import();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment