public static function UserCsvImportController::prepareRow in User CSV import 8
Same name and namespace in other branches
- 2.0.x src/Controller/UserCsvImportController.php \Drupal\user_csv_import\Controller\UserCsvImportController::prepareRow()
- 1.0.x src/Controller/UserCsvImportController.php \Drupal\user_csv_import\Controller\UserCsvImportController::prepareRow()
Prepares a new user from an upload row and current config.
Parameters
array $row: A row from the currently uploaded file.
array $config: An array of configuration containing:
- roles: an array of role ids to assign to the user
- password: a password for the imported users extracted from settings
- status: the status to be assigned to the new users extracted from settings
array $fields_position: An array with the position of the selected fields.
Return value
array New user values suitable for User::create().
1 call to UserCsvImportController::prepareRow()
- UserCsvImportController::processUpload in src/
Controller/ UserCsvImportController.php - Processes an uploaded CSV file, creating a new user for each row of values.
File
- src/
Controller/ UserCsvImportController.php, line 102
Class
- UserCsvImportController
- Provides methods to import CSV files and convert to users.
Namespace
Drupal\user_csv_import\ControllerCode
public static function prepareRow(array $row, array $config, array $fields_position) {
// Prepare username.
$preferred_username = strtolower($row[0]);
// Check if the username exists.
$i = 0;
while (self::usernameExists($i ? $preferred_username . $i : $preferred_username)) {
$i++;
}
// If exists, assign a number to the name.
$username = $i ? $preferred_username . $i : $preferred_username;
$user_data = [
'uid' => NULL,
'name' => $username,
'pass' => $config['password'],
'status' => $config['status'],
'created' => \Drupal::time()
->getRequestTime(),
'roles' => array_values($config['roles']),
];
// Add selected fields with their values to user data for store un database.
foreach ($fields_position as $index => $position) {
$user_data[$index] = $row[$position];
}
return $user_data;
}