3 namespace Drupal\user\Plugin\migrate\destination;
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\Core\Entity\ContentEntityInterface;
7 use Drupal\Core\Entity\EntityManagerInterface;
8 use Drupal\Core\Entity\EntityStorageInterface;
9 use Drupal\Core\Field\FieldTypePluginManagerInterface;
10 use Drupal\Core\Field\Plugin\Field\FieldType\EmailItem;
11 use Drupal\Core\Password\PasswordInterface;
12 use Drupal\migrate\Plugin\MigrationInterface;
13 use Drupal\migrate\Plugin\migrate\destination\EntityContentBase;
14 use Drupal\migrate\Row;
15 use Symfony\Component\DependencyInjection\ContainerInterface;
18 * @MigrateDestination(
22 class EntityUser extends EntityContentBase {
25 * The password service class.
27 * @var \Drupal\Core\Password\PasswordInterface
32 * Builds an user entity destination.
34 * @param array $configuration
35 * A configuration array containing information about the plugin instance.
36 * @param string $plugin_id
37 * The plugin_id for the plugin instance.
38 * @param mixed $plugin_definition
39 * The plugin implementation definition.
40 * @param \Drupal\migrate\Plugin\MigrationInterface $migration
42 * @param \Drupal\Core\Entity\EntityStorageInterface $storage
43 * The storage for this entity type.
44 * @param array $bundles
45 * The list of bundles this entity type has.
46 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
47 * The entity manager service.
48 * @param \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_manager
49 * The field type plugin manager service.
50 * @param \Drupal\Core\Password\PasswordInterface $password
51 * The password service.
53 public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, EntityStorageInterface $storage, array $bundles, EntityManagerInterface $entity_manager, FieldTypePluginManagerInterface $field_type_manager, PasswordInterface $password) {
54 parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $storage, $bundles, $entity_manager, $field_type_manager);
55 $this->password = $password;
61 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
62 $entity_type = static::getEntityTypeId($plugin_id);
68 $container->get('entity.manager')->getStorage($entity_type),
69 array_keys($container->get('entity.manager')->getBundleInfo($entity_type)),
70 $container->get('entity.manager'),
71 $container->get('plugin.manager.field.field_type'),
72 $container->get('password')
78 * @throws \Drupal\migrate\MigrateException
80 public function import(Row $row, array $old_destination_id_values = []) {
81 // Do not overwrite the root account password.
82 if ($row->getDestinationProperty('uid') == 1) {
83 $row->removeDestinationProperty('pass');
85 return parent::import($row, $old_destination_id_values);
91 protected function save(ContentEntityInterface $entity, array $old_destination_id_values = []) {
92 // Do not overwrite the root account password.
93 if ($entity->id() != 1) {
94 // Set the pre_hashed password so that the PasswordItem field does not hash
95 // already hashed passwords. If the md5_passwords configuration option is
96 // set we need to rehash the password and prefix with a U.
97 // @see \Drupal\Core\Field\Plugin\Field\FieldType\PasswordItem::preSave()
98 $entity->pass->pre_hashed = TRUE;
99 if (isset($this->configuration['md5_passwords'])) {
100 $entity->pass->value = 'U' . $this->password->hash($entity->pass->value);
103 return parent::save($entity, $old_destination_id_values);
109 protected function processStubRow(Row $row) {
110 parent::processStubRow($row);
111 // Email address is not defined as required in the base field definition but
112 // is effectively required by the UserMailRequired constraint. This means
113 // that Entity::processStubRow() did not populate it - we do it here.
114 $field_definitions = $this->entityManager
115 ->getFieldDefinitions($this->storage->getEntityTypeId(),
116 $this->getKey('bundle'));
117 $mail = EmailItem::generateSampleValue($field_definitions['mail']);
118 $row->setDestinationProperty('mail', reset($mail));
120 // @todo Work-around for https://www.drupal.org/node/2602066.
121 $name = $row->getDestinationProperty('name');
122 if (is_array($name)) {
123 $name = reset($name);
125 if (Unicode::strlen($name) > USERNAME_MAX_LENGTH) {
126 $row->setDestinationProperty('name', Unicode::substr($name, 0, USERNAME_MAX_LENGTH));