3 namespace Drupal\Core\Render\Element;
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\Core\Render\Element;
9 * Provides a form element for entering a password, with hidden text.
12 * - #size: The size of the input element in characters.
13 * - #pattern: A string for the native HTML5 pattern attribute.
17 * $form['pass'] = array(
18 * '#type' => 'password',
19 * '#title' => $this->t('Password'),
21 * '#pattern' => '[01]+',
25 * @see \Drupal\Core\Render\Element\PasswordConfirm
26 * @see \Drupal\Core\Render\Element\Textfield
28 * @FormElement("password")
30 class Password extends FormElement {
35 public function getInfo() {
36 $class = get_class($this);
42 [$class, 'processAjaxForm'],
43 [$class, 'processPattern'],
46 [$class, 'preRenderPassword'],
48 '#theme' => 'input__password',
49 '#theme_wrappers' => ['form_element'],
54 * Prepares a #type 'password' render element for input.html.twig.
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.
62 * The $element with prepared variables ready for input.html.twig.
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']);
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 : '';