View source
<?php
namespace Drupal\user_csv_import\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\user\RoleInterface;
use Drupal\user_csv_import\Controller\UserCsvImportController;
use Drupal\Core\Messenger\MessengerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Entity\EntityFieldManager;
class UserCsvImportForm extends FormBase {
protected $messenger;
protected $entityManager;
public function __construct(MessengerInterface $messenger, EntityFieldManager $entityManager) {
$this->messenger = $messenger;
$this->entityManager = $entityManager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('messenger'), $container
->get('entity_field.manager'));
}
public function getFormID() {
return 'user_csv_import_form';
}
protected function getEditableConfigNames() {
return [];
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['#tree'] = TRUE;
$form['config_options'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Options'),
];
$roles = user_role_names();
unset($roles['anonymous']);
$form['config_options']['roles'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Roles'),
'#options' => $roles,
];
$form['config_options']['roles'][RoleInterface::AUTHENTICATED_ID] = [
'#default_value' => TRUE,
'#disabled' => TRUE,
];
$form['config_options']['password'] = [
'#type' => 'textfield',
'#title' => $this
->t('Default password'),
'#required' => TRUE,
];
$form['config_options']['status'] = [
'#type' => 'select',
'#title' => $this
->t('Status'),
'#options' => [
'0' => $this
->t('Blocked'),
'1' => $this
->t('Active'),
],
];
$form['config_fields'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Fields'),
];
$user_fields = $this
->filterDefaultFields($this->entityManager
->getFieldStorageDefinitions('user'));
$selectable_fields = [];
foreach ($user_fields as $field) {
$selectable_fields[$field
->getName()] = $field
->getLabel();
}
$form['config_fields']['check_all'] = [
'#type' => 'checkbox',
'#title' => $this
->t('All'),
];
$form['config_fields']['fields'] = [
'#type' => 'checkboxes',
'#options' => $selectable_fields,
];
$form['file'] = [
'#type' => 'file',
'#title' => 'CSV file upload',
'#upload_validators' => [
'file_validate_extensions' => [
'csv',
],
],
];
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Import users'),
'#button_type' => 'primary',
];
$form['#theme'] = 'system_config_form';
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
$roles = $form_state
->getValue([
'config_options',
'roles',
]);
$fields = $form_state
->getValue([
'config_fields',
'fields',
]);
$roles_selected = array_filter($roles, function ($item) {
return $item;
});
$fields_selected = array_filter($fields, function ($item) {
return $item;
});
if (empty($roles_selected)) {
$form_state
->setErrorByName('roles', $this
->t('Please select at least one role to apply to the imported user(s).'));
}
elseif (empty($fields_selected)) {
$form_state
->setErrorByName('fields', $this
->t('Please select at least one field to apply to the imported user(s).'));
}
elseif (!array_key_exists('mail', $fields_selected) or !array_key_exists('name', $fields_selected)) {
$form_state
->setErrorByName('roles', $this
->t('The email and username fields is required'));
}
$this->file = file_save_upload('file', $form['file']['#upload_validators']);
if (!$this->file[0]) {
$form_state
->setErrorByName('file');
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$file = $this->file[0];
$roles = $form_state
->getValue([
'config_options',
'roles',
]);
$fields = $form_state
->getValue([
'config_fields',
'fields',
]);
$config = [
'roles' => array_filter($roles, function ($item) {
return $item;
}),
'fields' => array_filter($fields, function ($item) {
return $item;
}),
'password' => $form_state
->getValue([
'config_options',
'password',
]),
'status' => $form_state
->getValue([
'config_options',
'status',
]),
];
if ($created = UserCsvImportController::processUpload($file, $config)) {
$this->messenger
->addMessage($this
->t('Successfully imported @count users.', [
'@count' => count($created),
]));
}
else {
$this->messenger
->addMessage($this
->t('No users imported.'));
}
$form_state
->setRedirectUrl(new Url('entity.user.collection'));
}
private function filterDefaultFields($fields) {
unset($fields['uid']);
unset($fields['uuid']);
unset($fields['langcode']);
unset($fields['preferred_langcode']);
unset($fields['preferred_admin_langcode']);
unset($fields['pass']);
unset($fields['timezone']);
unset($fields['status']);
unset($fields['created']);
unset($fields['changed']);
unset($fields['access']);
unset($fields['login']);
unset($fields['init']);
unset($fields['roles']);
unset($fields['default_langcode']);
unset($fields['user_picture']);
return $fields;
}
}