You are here

public function PropertyNode::process in User Merge 2.x

Process merge on selected property.

Parameters

\Drupal\user\UserInterface $retired: Retired account.

\Drupal\user\UserInterface $retained: Retained account.

array $settings: Review settings.

Throws

\Drupal\usermerge\Exception\UserMergeException

Overrides UserMergePropertyPluginInterface::process

File

src/Plugin/UserMerge/Property/PropertyNode.php, line 70

Class

PropertyNode
Class PropertyNode.

Namespace

Drupal\usermerge\Plugin\UserMerge\Property

Code

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.');
  }

  // Anonymize nodes (current revisions).
  $node_ids = $node_storage
    ->getQuery()
    ->condition('uid', $retired
    ->id())
    ->execute();
  $nodes = $node_storage
    ->loadMultiple($node_ids);
  try {

    /** @var \Drupal\node\NodeInterface $node */
    foreach ($nodes as $node) {
      $node
        ->setOwnerId($retained
        ->id());
      $node
        ->save();
    }
  } catch (EntityStorageException $e) {
    throw new UserMergeException('An error occurred during nodes reassignment.');
  }

  // Anonymize old revisions.
  $this->connection
    ->update('node_field_revision')
    ->fields([
    'uid' => $retained
      ->id(),
  ])
    ->condition('uid', $retired
    ->id())
    ->execute();
}