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.
16 * $form['pass'] = array(
17 * '#type' => 'password',
18 * '#title' => $this->t('Password'),
23 * @see \Drupal\Core\Render\Element\PasswordConfirm
24 * @see \Drupal\Core\Render\Element\Textfield
26 * @FormElement("password")
28 class Password extends FormElement {
33 public function getInfo() {
34 $class = get_class($this);
40 [$class, 'processAjaxForm'],
41 [$class, 'processPattern'],
44 [$class, 'preRenderPassword'],
46 '#theme' => 'input__password',
47 '#theme_wrappers' => ['form_element'],
52 * Prepares a #type 'password' render element for input.html.twig.
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.
60 * The $element with prepared variables ready for input.html.twig.
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']);
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 : '';