public function UserCsvImportForm::buildForm in User CSV import 8
Same name and namespace in other branches
- 2.0.x src/Form/UserCsvImportForm.php \Drupal\user_csv_import\Form\UserCsvImportForm::buildForm()
- 1.0.x src/Form/UserCsvImportForm.php \Drupal\user_csv_import\Form\UserCsvImportForm::buildForm()
Implements \Drupal\Core\Form\FormInterface::buildForm().
Parameters
array $form: The form array.
\Drupal\Core\Form\FormStateInterface $form_state: The form state.
Return value
array Return the form object.
Overrides FormInterface::buildForm
File
- src/
Form/ UserCsvImportForm.php, line 83
Class
- UserCsvImportForm
- Provides methods to define and build the user import form.
Namespace
Drupal\user_csv_import\FormCode
public function buildForm(array $form, FormStateInterface $form_state) {
$form['#tree'] = TRUE;
// Options field set.
$form['config_options'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Options'),
];
// Roles field.
$roles = user_role_names();
unset($roles['anonymous']);
$form['config_options']['roles'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Roles'),
'#options' => $roles,
];
// Special handling for the inevitable "Authenticated user" role.
$form['config_options']['roles'][RoleInterface::AUTHENTICATED_ID] = [
'#default_value' => TRUE,
'#disabled' => TRUE,
];
// Default password.
$form['config_options']['password'] = [
'#type' => 'textfield',
'#title' => $this
->t('Default password'),
'#required' => TRUE,
];
// Status.
$form['config_options']['status'] = [
'#type' => 'select',
'#title' => $this
->t('Status'),
'#options' => [
'0' => $this
->t('Blocked'),
'1' => $this
->t('Active'),
],
];
// Fields field set.
$form['config_fields'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Fields'),
];
// Get user entity fields.
$user_fields = $this
->filterDefaultFields($this->entityManager
->getFieldStorageDefinitions('user'));
// Construct values for checkboxes.
$selectable_fields = [];
foreach ($user_fields as $field) {
$selectable_fields[$field
->getName()] = $field
->getLabel();
}
// Select all fields.
$form['config_fields']['check_all'] = [
'#type' => 'checkbox',
'#title' => $this
->t('All'),
];
// User form fields.
$form['config_fields']['fields'] = [
'#type' => 'checkboxes',
'#options' => $selectable_fields,
];
// File to upload.
$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',
];
// By default, render the form using theme_system_config_form().
$form['#theme'] = 'system_config_form';
return $form;
}