1<?php 2 3namespace Drupal\Core\Render\Element; 4 5use Drupal\Core\Form\FormStateInterface; 6use Drupal\Core\Render\Element; 7 8/** 9 * Provides a one-line text field form element. 10 * 11 * Properties: 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. 18 * - #pattern: A string for the native HTML5 pattern attribute. 19 * 20 * Usage example: 21 * @code 22 * $form['title'] = array( 23 * '#type' => 'textfield', 24 * '#title' => $this->t('Subject'), 25 * '#default_value' => $node->title, 26 * '#size' => 60, 27 * '#maxlength' => 128, 28 * '#pattern' => 'some-prefix-[a-z]+', 29 * '#required' => TRUE, 30 * ); 31 * @endcode 32 * 33 * @see \Drupal\Core\Render\Element\Color 34 * @see \Drupal\Core\Render\Element\Email 35 * @see \Drupal\Core\Render\Element\MachineName 36 * @see \Drupal\Core\Render\Element\Number 37 * @see \Drupal\Core\Render\Element\Password 38 * @see \Drupal\Core\Render\Element\PasswordConfirm 39 * @see \Drupal\Core\Render\Element\Range 40 * @see \Drupal\Core\Render\Element\Tel 41 * @see \Drupal\Core\Render\Element\Url 42 * 43 * @FormElement("textfield") 44 */ 45class Textfield extends FormElement { 46 47 /** 48 * {@inheritdoc} 49 */ 50 public function getInfo() { 51 $class = static::class; 52 return [ 53 '#input' => TRUE, 54 '#size' => 60, 55 '#maxlength' => 128, 56 '#autocomplete_route_name' => FALSE, 57 '#process' => [ 58 [$class, 'processAutocomplete'], 59 [$class, 'processAjaxForm'], 60 [$class, 'processPattern'], 61 [$class, 'processGroup'], 62 ], 63 '#pre_render' => [ 64 [$class, 'preRenderTextfield'], 65 [$class, 'preRenderGroup'], 66 ], 67 '#theme' => 'input__textfield', 68 '#theme_wrappers' => ['form_element'], 69 ]; 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 if (!is_scalar($input)) { 80 $input = ''; 81 } 82 return str_replace(["\r", "\n"], '', $input); 83 } 84 return NULL; 85 } 86 87 /** 88 * Prepares a #type 'textfield' render element for input.html.twig. 89 * 90 * @param array $element 91 * An associative array containing the properties of the element. 92 * Properties used: #title, #value, #description, #size, #maxlength, 93 * #placeholder, #required, #attributes. 94 * 95 * @return array 96 * The $element with prepared variables ready for input.html.twig. 97 */ 98 public static function preRenderTextfield($element) { 99 $element['#attributes']['type'] = 'text'; 100 Element::setAttributes($element, ['id', 'name', 'value', 'size', 'maxlength', 'placeholder']); 101 static::setAttributes($element, ['form-text']); 102 103 return $element; 104 } 105 106} 107