PropertyNode.php in User Merge 2.x
File
src/Plugin/UserMerge/Property/PropertyNode.php
View source
<?php
namespace Drupal\usermerge\Plugin\UserMerge\Property;
use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\user\UserInterface;
use Drupal\usermerge\Exception\UserMergeException;
use Symfony\Component\DependencyInjection\ContainerInterface;
class PropertyNode extends UserMergePropertyBase {
protected $connection;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, Connection $connection) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager);
$this->connection = $connection;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager'), $container
->get('database'));
}
public function process(UserInterface $retired, UserInterface $retained, array $settings = []) : void {
try {
$node_storage = $this->entityTypeManager
->getStorage('node');
} catch (PluginNotFoundException|InvalidPluginDefinitionException $e) {
throw new UserMergeException('Storage for node entity has not been found.');
}
$node_ids = $node_storage
->getQuery()
->condition('uid', $retired
->id())
->execute();
$nodes = $node_storage
->loadMultiple($node_ids);
try {
foreach ($nodes as $node) {
$node
->setOwnerId($retained
->id());
$node
->save();
}
} catch (EntityStorageException $e) {
throw new UserMergeException('An error occurred during nodes reassignment.');
}
$this->connection
->update('node_field_revision')
->fields([
'uid' => $retained
->id(),
])
->condition('uid', $retired
->id())
->execute();
}
}