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