You are here

class PropertyEntityReference in User Merge 2.x

Class PropertyEntityReference.

@UserMergeProperty( id = "property_entity_reference", name = @Translation("Entity reference"), description = @Translation("Reassign user-reference fields associated with the retired user."), review = "", ) @package Drupal\usermerge\Plugin\UserMerge\Property

Hierarchy

Expanded class hierarchy of PropertyEntityReference

File

src/Plugin/UserMerge/Property/PropertyEntityReference.php, line 24

Namespace

Drupal\usermerge\Plugin\UserMerge\Property
View source
class PropertyEntityReference extends UserMergePropertyBase {

  /**
   * The database connection.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $connection;

  /**
   * PropertyComment constructor.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\Database\Connection $connection
   *   The database 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;
  }

  /**
   * {@inheritdoc}
   */
  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'));
  }

  /**
   * {@inheritDoc}
   */
  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();
      }
    }
  }

  /**
   * Get user reference fields.
   *
   * @return \Drupal\Core\Field\FieldStorageDefinitionInterface[]
   *   Referenced fields.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  private function getUserReferenceFields() {
    $references = [];

    /** @var \Drupal\Core\Field\FieldStorageDefinitionInterface[] $fields */
    $fields = $this->entityTypeManager
      ->getStorage('field_storage_config')
      ->loadMultiple();
    foreach ($fields as $field) {
      if ($field
        ->getType() != 'entity_reference') {
        continue;
      }
      if ($field
        ->getSetting('target_type') != 'user') {
        continue;
      }
      $references[] = $field;
    }
    return $references;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 2
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PropertyEntityReference::$connection protected property The database connection.
PropertyEntityReference::create public static function Creates an instance of the plugin. Overrides UserMergePropertyBase::create
PropertyEntityReference::getUserReferenceFields private function Get user reference fields.
PropertyEntityReference::process public function Process merge on selected property. Overrides UserMergePropertyPluginInterface::process
PropertyEntityReference::__construct public function PropertyComment constructor. Overrides UserMergePropertyBase::__construct
UserMergePropertyBase::$entityTypeManager protected property Entity type manager.
UserMergePropertyBase::getName public function Return the name of the property plugin. Overrides UserMergePropertyPluginInterface::getName
UserMergePropertyBase::getReviewForm public function Get the review form if it is provided by the plugin. Overrides UserMergePropertyPluginInterface::getReviewForm