Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / lib / Drupal / Core / Render / Element / Password.php
1 <?php
2
3 namespace Drupal\Core\Render\Element;
4
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\Core\Render\Element;
7
8 /**
9  * Provides a form element for entering a password, with hidden text.
10  *
11  * Properties:
12  * - #size: The size of the input element in characters.
13  * - #pattern: A string for the native HTML5 pattern attribute.
14  *
15  * Usage example:
16  * @code
17  * $form['pass'] = array(
18  *   '#type' => 'password',
19  *   '#title' => $this->t('Password'),
20  *   '#size' => 25,
21  *   '#pattern' => '[01]+',
22  * );
23  * @endcode
24  *
25  * @see \Drupal\Core\Render\Element\PasswordConfirm
26  * @see \Drupal\Core\Render\Element\Textfield
27  *
28  * @FormElement("password")
29  */
30 class Password extends FormElement {
31
32   /**
33    * {@inheritdoc}
34    */
35   public function getInfo() {
36     $class = get_class($this);
37     return [
38       '#input' => TRUE,
39       '#size' => 60,
40       '#maxlength' => 128,
41       '#process' => [
42         [$class, 'processAjaxForm'],
43         [$class, 'processPattern'],
44       ],
45       '#pre_render' => [
46         [$class, 'preRenderPassword'],
47       ],
48       '#theme' => 'input__password',
49       '#theme_wrappers' => ['form_element'],
50     ];
51   }
52
53   /**
54    * Prepares a #type 'password' render element for input.html.twig.
55    *
56    * @param array $element
57    *   An associative array containing the properties of the element.
58    *   Properties used: #title, #value, #description, #size, #maxlength,
59    *   #placeholder, #required, #attributes.
60    *
61    * @return array
62    *   The $element with prepared variables ready for input.html.twig.
63    */
64   public static function preRenderPassword($element) {
65     $element['#attributes']['type'] = 'password';
66     Element::setAttributes($element, ['id', 'name', 'size', 'maxlength', 'placeholder']);
67     static::setAttributes($element, ['form-text']);
68
69     return $element;
70   }
71
72   /**
73    * {@inheritdoc}
74    */
75   public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
76     if ($input !== FALSE && $input !== NULL) {
77       // This should be a string, but allow other scalars since they might be
78       // valid input in programmatic form submissions.
79       return is_scalar($input) ? (string) $input : '';
80     }
81     return NULL;
82   }
83
84 }