PropertyFlag.php in User Merge 2.x
File
src/Plugin/UserMerge/Property/PropertyFlag.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\Entity\EntityStorageException;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\flag\FlagServiceInterface;
use Drupal\user\UserInterface;
use Drupal\usermerge\Exception\UserMergeException;
use Symfony\Component\DependencyInjection\ContainerInterface;
class PropertyFlag extends UserMergePropertyBase {
protected $flag;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, FlagServiceInterface $flag) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager);
$this->flag = $flag;
}
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('flag'));
}
public function process(UserInterface $retired, UserInterface $retained, array $settings = []) : void {
try {
$flagging_storage = $this->entityTypeManager
->getStorage('flagging');
} catch (PluginNotFoundException|InvalidPluginDefinitionException $e) {
throw new UserMergeException('Storage for flagging entity has not been found.');
}
$flaggings_ids = $flagging_storage
->getQuery()
->condition('uid', $retired
->id())
->sort('flag_id')
->execute();
$flaggings = $flagging_storage
->loadMultiple($flaggings_ids);
try {
foreach ($flaggings as $flagging) {
$flag = $flagging
->getFlag();
$entity = $flagging
->getFlaggable();
if (!$flag
->isFlagged($entity, $retained)) {
$flagging
->setOwner($retained);
$flagging
->save();
}
}
} catch (EntityStorageException $e) {
throw new UserMergeException('An error occurred during flag reassignment.');
}
}
}