1. 8.5.x core/modules/user/src/Plugin/migrate/destination/EntityUser.php
  2. 8.0.x core/modules/user/src/Plugin/migrate/destination/EntityUser.php
  3. 8.1.x core/modules/user/src/Plugin/migrate/destination/EntityUser.php
  4. 8.2.x core/modules/user/src/Plugin/migrate/destination/EntityUser.php
  5. 8.3.x core/modules/user/src/Plugin/migrate/destination/EntityUser.php
  6. 8.4.x core/modules/user/src/Plugin/migrate/destination/EntityUser.php
  7. 8.6.x core/modules/user/src/Plugin/migrate/destination/EntityUser.php

Namespace

Drupal\user\Plugin\migrate\destination

File

core/modules/user/src/Plugin/migrate/destination/EntityUser.php
View source
<?php

namespace Drupal\user\Plugin\migrate\destination;

use Drupal\Component\Utility\Unicode;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\FieldTypePluginManagerInterface;
use Drupal\Core\Field\Plugin\Field\FieldType\EmailItem;
use Drupal\Core\Password\PasswordInterface;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\Plugin\migrate\destination\EntityContentBase;
use Drupal\migrate\Row;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * @MigrateDestination(
 *   id = "entity:user"
 * )
 */
class EntityUser extends EntityContentBase {

  /**
   * The password service class.
   *
   * @var \Drupal\Core\Password\PasswordInterface
   */
  protected $password;

  /**
   * Builds an user entity destination.
   *
   * @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\migrate\Plugin\MigrationInterface $migration
   *   The migration.
   * @param \Drupal\Core\Entity\EntityStorageInterface $storage
   *   The storage for this entity type.
   * @param array $bundles
   *   The list of bundles this entity type has.
   * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
   *   The entity manager service.
   * @param \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_manager
   *   The field type plugin manager service.
   * @param \Drupal\Core\Password\PasswordInterface $password
   *   The password service.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, EntityStorageInterface $storage, array $bundles, EntityManagerInterface $entity_manager, FieldTypePluginManagerInterface $field_type_manager, PasswordInterface $password) {
    parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $storage, $bundles, $entity_manager, $field_type_manager);
    $this->password = $password;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
    $entity_type = static::getEntityTypeId($plugin_id);
    return new static($configuration, $plugin_id, $plugin_definition, $migration, $container
      ->get('entity.manager')
      ->getStorage($entity_type), array_keys($container
      ->get('entity.manager')
      ->getBundleInfo($entity_type)), $container
      ->get('entity.manager'), $container
      ->get('plugin.manager.field.field_type'), $container
      ->get('password'));
  }

  /**
   * {@inheritdoc}
   * @throws \Drupal\migrate\MigrateException
   */
  public function import(Row $row, array $old_destination_id_values = []) {

    // Do not overwrite the root account password.
    if ($row
      ->getDestinationProperty('uid') == 1) {
      $row
        ->removeDestinationProperty('pass');
    }
    return parent::import($row, $old_destination_id_values);
  }

  /**
   * {@inheritdoc}
   */
  protected function save(ContentEntityInterface $entity, array $old_destination_id_values = []) {

    // Do not overwrite the root account password.
    if ($entity
      ->id() != 1) {

      // Set the pre_hashed password so that the PasswordItem field does not hash
      // already hashed passwords. If the md5_passwords configuration option is
      // set we need to rehash the password and prefix with a U.
      // @see \Drupal\Core\Field\Plugin\Field\FieldType\PasswordItem::preSave()
      $entity->pass->pre_hashed = TRUE;
      if (isset($this->configuration['md5_passwords'])) {
        $entity->pass->value = 'U' . $this->password
          ->hash($entity->pass->value);
      }
    }
    return parent::save($entity, $old_destination_id_values);
  }

  /**
   * {@inheritdoc}
   */
  protected function processStubRow(Row $row) {
    parent::processStubRow($row);

    // Email address is not defined as required in the base field definition but
    // is effectively required by the UserMailRequired constraint. This means
    // that Entity::processStubRow() did not populate it - we do it here.
    $field_definitions = $this->entityManager
      ->getFieldDefinitions($this->storage
      ->getEntityTypeId(), $this
      ->getKey('bundle'));
    $mail = EmailItem::generateSampleValue($field_definitions['mail']);
    $row
      ->setDestinationProperty('mail', reset($mail));

    // @todo Work-around for https://www.drupal.org/node/2602066.
    $name = $row
      ->getDestinationProperty('name');
    if (is_array($name)) {
      $name = reset($name);
    }
    if (Unicode::strlen($name) > USERNAME_MAX_LENGTH) {
      $row
        ->setDestinationProperty('name', Unicode::substr($name, 0, USERNAME_MAX_LENGTH));
    }
  }

}

Classes

Namesort descending Description
EntityUser Plugin annotation @MigrateDestination( id = "entity:user" )