diff --git a/README.md b/README.md index 708ece2..522405a 100644 --- a/README.md +++ b/README.md @@ -5,11 +5,17 @@ Import users into Drupal 8 from CSV file upload. This module currently assumes the following: +- Field: uid (int) - Field: field_name_first (string) - Field: field_name_last (string) +- Field: email (string) +- Field: username (string) +- Field: password (string) - CSV: In the following format: ``` - fname,lname,email + uid,fname,lname,email,username,password ``` - -Usernames are generated from first+last+any digits to make name unique. +CSV may not contain someone of the next fields: uid, username and password. +-Usernames are generated from first+last+any digits to make name unique. +-Password null +-Uid automatic diff --git a/src/Controller/UserImportController.php b/src/Controller/UserImportController.php index dfec611..360356d 100644 --- a/src/Controller/UserImportController.php +++ b/src/Controller/UserImportController.php @@ -44,37 +44,51 @@ public static function processUpload(File $file, array $config) { return $created; } - /** - * Prepares a new user from an upload row and current config. - * - * @param $row - * A row from the currently uploaded file. - * - * @param $config - * An array of configuration containing: - * - roles: an array of role ids to assign to the user - * - * @return array - * New user values suitable for User::create(). - */ + /** + * Prepares a new user from an upload row and current config. + * + * @param array $row + * A row from the currently uploaded file. + * + * @param array $config + * An array of configuration containing: + * - roles: an array of role ids to assign to the user + * @return array New user values suitable for User::create(). + * New user values suitable for User::create(). + */ public static function prepareRow(array $row, array $config) { - $preferred_username = (strtolower($row[0] . $row[1])); - $i = 0; - while (self::usernameExists($i ? $preferred_username . $i : $preferred_username)) { - $i++; + if(empty(array_filter($config['opt']))){ + $preferred_username = (strtolower($row[1] . $row[2])); + $i = 0; + while (self::usernameExists($i ? $preferred_username . $i : $preferred_username)) { + $i++; + } + $username = $i ? $preferred_username . $i : $preferred_username; + return [ + 'uid' => NULL, + 'name' => $username, + 'field_name_first' => $row[1], + 'field_name_last' => $row[2], + 'pass' => NULL, + 'mail' => $row[3], + 'status' => 1, + 'created' => REQUEST_TIME, + 'roles' => array_values($config['roles']), + ]; + }else{ + $username = (strtolower($row[4])); + return [ + 'uid' => $row[0], + 'name' => $username, + 'field_name_first' => $row[1], + 'field_name_last' => $row[2], + 'pass' => $row[5], + 'mail' => $row[3], + 'status' => 1, + 'created' => REQUEST_TIME, + 'roles' => array_values($config['roles']), + ]; } - $username = $i ? $preferred_username . $i : $preferred_username; - return [ - 'uid' => NULL, - 'name' => $username, - 'field_name_first' => $row[0], - 'field_name_last' => $row[1], - 'pass' => NULL, - 'mail' => $row[2], - 'status' => 1, - 'created' => REQUEST_TIME, - 'roles' => array_values($config['roles']), - ]; } /** diff --git a/src/Form/UserImportForm.php b/src/Form/UserImportForm.php index 39f6ee5..af53630 100644 --- a/src/Form/UserImportForm.php +++ b/src/Form/UserImportForm.php @@ -65,6 +65,19 @@ public function buildForm(array $form, FormStateInterface $form_state) { 'file_validate_extensions' => ['csv'] ] ]; + // Includes uid, username and password? + $form['#tree'] = TRUE; + $form['OPT'] = [ + '#type' => 'fieldset', + '#title' => $this->t('Extra Fields Included into CSV File') + ]; + $fields = array("uid","username","password"); + $form['OPT']['fields'] = [ + '#type' => 'checkboxes', + '#title' => $this->t('fields'), + '#options' => $fields + ]; + //end includes uid, username and password? $form['actions']['#type'] = 'actions'; $form['actions']['submit'] = array( '#type' => 'submit', @@ -105,7 +118,8 @@ public function submitForm(array &$form, FormStateInterface $form_state) { $config = [ 'roles' => array_filter($roles, function($item) { return ($item); - }) + }), + 'opt' => $form_state->getValue(['OPT', 'fields']) ]; if ($created = UserImportController::processUpload($file, $config)) { drupal_set_message(t('Successfully imported @count users.', ['@count' => count($created)]));