function InstallHelper::importUsers

Same name in other branches
  1. 11.x core/profiles/demo_umami/modules/demo_umami_content/src/InstallHelper.php \Drupal\demo_umami_content\InstallHelper::importUsers()

Imports users.

Users are created as their content is imported. However, some users might have non-default values (as preferred language), or editors don't have their own content so are created here instead.

Return value

$this

File

core/profiles/demo_umami/modules/demo_umami_content/src/InstallHelper.php, line 312

Class

InstallHelper
Defines a helper class for importing default content.

Namespace

Drupal\demo_umami_content

Code

protected function importUsers() {
    $user_storage = $this->entityTypeManager
        ->getStorage('user');
    $users = [
        'Gregorio Sánchez' => [
            'preferred_language' => 'es',
            'roles' => [
                'author',
            ],
        ],
        'Margaret Hopper' => [
            'preferred_language' => 'en',
            'roles' => [
                'editor',
            ],
        ],
        'Grace Hamilton' => [
            'preferred_language' => 'en',
            'roles' => [
                'editor',
            ],
        ],
        'María García' => [
            'preferred_language' => 'es',
            'roles' => [
                'editor',
            ],
        ],
    ];
    foreach ($users as $name => $user_data) {
        $user = $user_storage->create([
            'name' => $name,
            'status' => 1,
            'roles' => $user_data['roles'],
            'preferred_langcode' => $user_data['preferred_language'],
            'preferred_admin_langcode' => $user_data['preferred_language'],
            'mail' => \Drupal::transliteration()->transliterate(mb_strtolower(str_replace(' ', '.', $name))) . '@example.com',
        ]);
        $user->enforceIsNew();
        $user->save();
        $this->storeCreatedContentUuids([
            $user->uuid() => 'user',
        ]);
    }
    return $this;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.