3 namespace Drupal\Core\Render\Element;
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\Core\Render\Element;
9 * Provides a one-line text field form element.
12 * - #maxlength: Maximum number of characters of input allowed.
13 * - #size: The size of the input element in characters.
14 * - #autocomplete_route_name: A route to be used as callback URL by the
15 * autocomplete JavaScript library.
16 * - #autocomplete_route_parameters: An array of parameters to be used in
17 * conjunction with the route name.
21 * $form['title'] = array(
22 * '#type' => 'textfield',
23 * '#title' => $this->t('Subject'),
24 * '#default_value' => $node->title,
26 * '#maxlength' => 128,
27 * '#required' => TRUE,
31 * @see \Drupal\Core\Render\Element\Color
32 * @see \Drupal\Core\Render\Element\Email
33 * @see \Drupal\Core\Render\Element\MachineName
34 * @see \Drupal\Core\Render\Element\Number
35 * @see \Drupal\Core\Render\Element\Password
36 * @see \Drupal\Core\Render\Element\PasswordConfirm
37 * @see \Drupal\Core\Render\Element\Range
38 * @see \Drupal\Core\Render\Element\Tel
39 * @see \Drupal\Core\Render\Element\Url
41 * @FormElement("textfield")
43 class Textfield extends FormElement {
48 public function getInfo() {
49 $class = get_class($this);
54 '#autocomplete_route_name' => FALSE,
56 [$class, 'processAutocomplete'],
57 [$class, 'processAjaxForm'],
58 [$class, 'processPattern'],
59 [$class, 'processGroup'],
62 [$class, 'preRenderTextfield'],
63 [$class, 'preRenderGroup'],
65 '#theme' => 'input__textfield',
66 '#theme_wrappers' => ['form_element'],
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 if (!is_scalar($input)) {
80 return str_replace(["\r", "\n"], '', $input);
86 * Prepares a #type 'textfield' render element for input.html.twig.
88 * @param array $element
89 * An associative array containing the properties of the element.
90 * Properties used: #title, #value, #description, #size, #maxlength,
91 * #placeholder, #required, #attributes.
94 * The $element with prepared variables ready for input.html.twig.
96 public static function preRenderTextfield($element) {
97 $element['#attributes']['type'] = 'text';
98 Element::setAttributes($element, ['id', 'name', 'value', 'size', 'maxlength', 'placeholder']);
99 static::setAttributes($element, ['form-text']);