Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

some changes #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
72 changes: 43 additions & 29 deletions src/Controller/UserImportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']),
];
}

/**
Expand Down
16 changes: 15 additions & 1 deletion src/Form/UserImportForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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)]));
Expand Down