You are here

public function PropertyEntityReference::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/PropertyEntityReference.php, line 69

Class

PropertyEntityReference
Class PropertyEntityReference.

Namespace

Drupal\usermerge\Plugin\UserMerge\Property

Code

public function process(UserInterface $retired, UserInterface $retained, array $settings = []) : void {
  try {
    $fields = $this
      ->getUserReferenceFields();
  } catch (InvalidPluginDefinitionException|PluginNotFoundException $e) {
    throw new UserMergeException('An error occurred during searching for user reference fields.');
  }
  foreach ($fields as $field) {
    try {

      /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
      $table_mapping = $this->entityTypeManager
        ->getStorage($field
        ->getTargetEntityTypeId())
        ->getTableMapping();
    } catch (InvalidPluginDefinitionException|PluginNotFoundException $e) {
      throw new UserMergeException('An error occurred during loading field storage.');
    }
    $tables = [];
    $tables[] = $table_mapping
      ->getDedicatedDataTableName($field);
    $tables[] = $table_mapping
      ->getDedicatedRevisionTableName($field);
    $field_name = $field
      ->getName() . '_target_id';
    foreach ($tables as $table_name) {

      // It is very likely that revision table will not exist.
      $exists = $this->connection
        ->schema()
        ->tableExists($table_name);
      if (!$exists) {
        continue;
      }
      $this->connection
        ->update($table_name)
        ->fields([
        $field_name => $retained
          ->id(),
      ])
        ->condition($field_name, $retired
        ->id())
        ->execute();
    }
  }
}