pino-8.x-1.2-no-core/src/Form/ConfigurationForm.php

src/Form/ConfigurationForm.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
 
namespace Drupal\pino\Form;
 
use Drupal\Core\Datetime\Entity\DateFormat;
use Drupal\user\Entity\User;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
 
/**
 * Provides CSV importer form.
 */
class ConfigurationForm extends FormBase {
 
  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'pino_configuration_form';
  }
 
  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
 
    $form['#title'] = t('Configuring officer account');
 
    $form['officer_account'] = [
      '#type' => 'fieldgroup',
      '#title' => $this->t('Officer account'),
    ];
 
    $form['officer_account']['name'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Username'),
      '#maxlength' => USERNAME_MAX_LENGTH,
      '#description' => $this->t("Several special characters are allowed, including space, period (.), hyphen (-), apostrophe ('), underscore (_), and the @ sign."),
      '#required' => TRUE,
      '#attributes' => ['class' => ['username']],
    ];
 
    $form['officer_account']['pass'] = [
      '#type' => 'password_confirm',
      '#required' => TRUE,
      '#size' => 25,
    ];
 
    $form['officer_account']['#tree'] = TRUE;
    $form['officer_account']['mail'] = [
      '#type' => 'email',
      '#title' => $this->t('Email address'),
      '#required' => TRUE,
    ];
 
    $form['preferred_date_format'] = [
      '#type' => 'select',
      '#title' => t('Preferred date format'),
      '#description' => t("To be used in member context, for example member's join date"),
      '#options' => [
        'd.m.Y' => date('d.m.Y'),
        'd/m/Y' => date('d/m/Y'),
        'd-m-Y' => date('d-m-Y'),
        'd F Y' => date('d F Y'),
        'm/d/Y' => date('m/d/Y'),
        'F j, Y' => date('F j, Y'),
        'Y-m-d' => date('Y-m-d'),
        'Y.m.d' => date('Y.m.d'),
        'Y/m/d' => date('Y/m/d'),
      ],
    ];
 
    $form['actions'] = ['#type' => 'actions'];
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => t('Save and continue'),
      '#button_type' => 'primary',
    ];
 
    return $form;
  }
 
  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
 
    // Username exists already.
    if (user_load_by_name($form_state->getValue(['officer_account', 'name']))) {
      $form_state->setErrorByName('officer_account][name', t('The username %value is already taken.', ['%value' => $form_state->getValue('name')]));
    }
 
    // Username allowed characters.
    if ($error = user_validate_name($form_state->getValue(['officer_account', 'name']))) {
      $form_state->setErrorByName('officer_account][name', $error);
    }
 
    // Passwords match.
    if ($form_state->getValue(['officer_account', 'pass', 'pass1']) !== $form_state->getValue([
      'officer_account',
      'pass',
      'pass2',
    ])) {
      $form_state->setErrorByName('officer_account][pass', t('The specified passwords do not match.'));
    }
 
    // Email exists already.
    if (user_load_by_mail($form_state->getValue(['officer_account', 'mail']))) {
      $form_state->setErrorByName('officer_account][mail', t('The email address %value is already taken.', ['%value' => $form_state->getValue('email')]));
    }
 
    // Email allowed characters.
    if (!\Drupal::service('email.validator')->isValid($form_state->getValue(['officer_account', 'mail']))) {
      $form_state->setErrorByName('officer_account][mail', t('The email address %mail is not valid.', ['%mail' => $form_state->getValue('email')]));
    }
 
  }
 
  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
 
    // Create officer user account.
    $language = \Drupal::languageManager()->getCurrentLanguage()->getId();
    $user = User::create();
 
    $user->setPassword($form_state->getValue(['officer_account', 'pass']));
    $user->enforceIsNew();
    $user->setEmail($form_state->getValue(['officer_account', 'mail']));
    $user->setUsername($form_state->getValue(['officer_account', 'name']));
 
    $user->set('init', 'email');
    $user->set('langcode', $language);
    $user->set('preferred_langcode', $language);
    $user->set('preferred_admin_langcode', $language);
    $user->activate();
 
    $user->addRole('officer');
 
    $user->save();
 
    // Create default  date format.
    $date_format = DateFormat::create([
      'label' => 'Membership date',
      'id' => 'membership_date',
      'pattern' => $form_state->getValue(['preferred_date_format']),
    ]);
 
    $date_format->save();
 
  }
 
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc