3 namespace Drupal\user\Plugin\migrate\destination;
5 use Drupal\Core\Entity\ContentEntityInterface;
6 use Drupal\Core\Entity\EntityManagerInterface;
7 use Drupal\Core\Entity\EntityStorageInterface;
8 use Drupal\Core\Field\FieldTypePluginManagerInterface;
9 use Drupal\Core\Field\Plugin\Field\FieldType\EmailItem;
10 use Drupal\Core\Password\PasswordInterface;
11 use Drupal\migrate\Plugin\MigrationInterface;
12 use Drupal\migrate\Plugin\migrate\destination\EntityContentBase;
13 use Drupal\migrate\Row;
14 use Symfony\Component\DependencyInjection\ContainerInterface;
17 * Provides a destination plugin for migrating user entities.
21 * The example below migrates users and preserves original passwords from a
22 * source that has passwords as MD5 hashes without salt. The passwords will be
23 * salted and re-hashed before they are saved to the destination Drupal
24 * database. The MD5 hash used in the example is a hash of 'password'.
26 * The example uses the EmbeddedDataSource source plugin for the sake of
27 * simplicity. The mapping between old user_ids and new Drupal uids is saved in
28 * the migration map table.
30 * id: custom_user_migration
31 * label: Custom user migration
33 * plugin: embedded_data
38 * mail: johnsmith@example.com
39 * hash: '5f4dcc3b5aa765d61d8327deb882cf99'
48 * plugin: default_value
55 * For configuration options inherited from the parent class, refer to
56 * \Drupal\migrate\Plugin\migrate\destination\EntityContentBase.
58 * The example above is about migrating an MD5 password hash. For more examples
59 * on different password hash types and a list of other user properties, refer
60 * to the handbook documentation:
61 * @see https://www.drupal.org/docs/8/api/migrate-api/migrate-destination-plugins-examples/migrating-users
63 * @MigrateDestination(
67 class EntityUser extends EntityContentBase {
70 * The password service class.
72 * @var \Drupal\Core\Password\PasswordInterface
77 * Builds an user entity destination.
79 * @param array $configuration
80 * A configuration array containing information about the plugin instance.
81 * @param string $plugin_id
82 * The plugin_id for the plugin instance.
83 * @param mixed $plugin_definition
84 * The plugin implementation definition.
85 * @param \Drupal\migrate\Plugin\MigrationInterface $migration
87 * @param \Drupal\Core\Entity\EntityStorageInterface $storage
88 * The storage for this entity type.
89 * @param array $bundles
90 * The list of bundles this entity type has.
91 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
92 * The entity manager service.
93 * @param \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_manager
94 * The field type plugin manager service.
95 * @param \Drupal\Core\Password\PasswordInterface $password
96 * The password service.
98 public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, EntityStorageInterface $storage, array $bundles, EntityManagerInterface $entity_manager, FieldTypePluginManagerInterface $field_type_manager, PasswordInterface $password) {
99 parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $storage, $bundles, $entity_manager, $field_type_manager);
100 $this->password = $password;
106 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
107 $entity_type = static::getEntityTypeId($plugin_id);
113 $container->get('entity.manager')->getStorage($entity_type),
114 array_keys($container->get('entity.manager')->getBundleInfo($entity_type)),
115 $container->get('entity.manager'),
116 $container->get('plugin.manager.field.field_type'),
117 $container->get('password')
123 * @throws \Drupal\migrate\MigrateException
125 public function import(Row $row, array $old_destination_id_values = []) {
126 // Do not overwrite the root account password.
127 if ($row->getDestinationProperty('uid') == 1) {
128 $row->removeDestinationProperty('pass');
130 return parent::import($row, $old_destination_id_values);
136 protected function save(ContentEntityInterface $entity, array $old_destination_id_values = []) {
137 // Do not overwrite the root account password.
138 if ($entity->id() != 1) {
139 // Set the pre_hashed password so that the PasswordItem field does not hash
140 // already hashed passwords. If the md5_passwords configuration option is
141 // set we need to rehash the password and prefix with a U.
142 // @see \Drupal\Core\Field\Plugin\Field\FieldType\PasswordItem::preSave()
143 $entity->pass->pre_hashed = TRUE;
144 if (isset($this->configuration['md5_passwords'])) {
145 $entity->pass->value = 'U' . $this->password->hash($entity->pass->value);
148 return parent::save($entity, $old_destination_id_values);
154 protected function processStubRow(Row $row) {
155 parent::processStubRow($row);
156 // Email address is not defined as required in the base field definition but
157 // is effectively required by the UserMailRequired constraint. This means
158 // that Entity::processStubRow() did not populate it - we do it here.
159 $field_definitions = $this->entityManager
160 ->getFieldDefinitions($this->storage->getEntityTypeId(),
161 $this->getKey('bundle'));
162 $mail = EmailItem::generateSampleValue($field_definitions['mail']);
163 $row->setDestinationProperty('mail', reset($mail));
165 // @todo Work-around for https://www.drupal.org/node/2602066.
166 $name = $row->getDestinationProperty('name');
167 if (is_array($name)) {
168 $name = reset($name);
170 if (mb_strlen($name) > USERNAME_MAX_LENGTH) {
171 $row->setDestinationProperty('name', mb_substr($name, 0, USERNAME_MAX_LENGTH));
178 public function getHighestId() {
179 $highest_id = parent::getHighestId();
181 // Every Drupal site must have a user with UID of 1 and it's normal for
182 // migrations to overwrite this user.
183 if ($highest_id === 1) {