Textfield.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace Drupal\Core\Render\Element;
  3. use Drupal\Core\Form\FormStateInterface;
  4. use Drupal\Core\Render\Element;
  5. /**
  6. * Provides a one-line text field form element.
  7. *
  8. * Properties:
  9. * - #maxlength: Maximum number of characters of input allowed.
  10. * - #size: The size of the input element in characters.
  11. * - #autocomplete_route_name: A route to be used as callback URL by the
  12. * autocomplete JavaScript library.
  13. * - #autocomplete_route_parameters: An array of parameters to be used in
  14. * conjunction with the route name.
  15. * - #pattern: A string for the native HTML5 pattern attribute.
  16. *
  17. * Usage example:
  18. * @code
  19. * $form['title'] = array(
  20. * '#type' => 'textfield',
  21. * '#title' => $this->t('Subject'),
  22. * '#default_value' => $node->title,
  23. * '#size' => 60,
  24. * '#maxlength' => 128,
  25. * '#pattern' => 'some-prefix-[a-z]+',
  26. * '#required' => TRUE,
  27. * );
  28. * @endcode
  29. *
  30. * @see \Drupal\Core\Render\Element\Color
  31. * @see \Drupal\Core\Render\Element\Email
  32. * @see \Drupal\Core\Render\Element\MachineName
  33. * @see \Drupal\Core\Render\Element\Number
  34. * @see \Drupal\Core\Render\Element\Password
  35. * @see \Drupal\Core\Render\Element\PasswordConfirm
  36. * @see \Drupal\Core\Render\Element\Range
  37. * @see \Drupal\Core\Render\Element\Tel
  38. * @see \Drupal\Core\Render\Element\Url
  39. *
  40. * @FormElement("textfield")
  41. */
  42. class Textfield extends FormElement {
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function getInfo() {
  47. $class = get_class($this);
  48. return [
  49. '#input' => TRUE,
  50. '#size' => 60,
  51. '#maxlength' => 128,
  52. '#autocomplete_route_name' => FALSE,
  53. '#process' => [
  54. [$class, 'processAutocomplete'],
  55. [$class, 'processAjaxForm'],
  56. [$class, 'processPattern'],
  57. [$class, 'processGroup'],
  58. ],
  59. '#pre_render' => [
  60. [$class, 'preRenderTextfield'],
  61. [$class, 'preRenderGroup'],
  62. ],
  63. '#theme' => 'input__textfield',
  64. '#theme_wrappers' => ['form_element'],
  65. ];
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
  71. if ($input !== FALSE && $input !== NULL) {
  72. // This should be a string, but allow other scalars since they might be
  73. // valid input in programmatic form submissions.
  74. if (!is_scalar($input)) {
  75. $input = '';
  76. }
  77. return str_replace(["\r", "\n"], '', $input);
  78. }
  79. return NULL;
  80. }
  81. /**
  82. * Prepares a #type 'textfield' render element for input.html.twig.
  83. *
  84. * @param array $element
  85. * An associative array containing the properties of the element.
  86. * Properties used: #title, #value, #description, #size, #maxlength,
  87. * #placeholder, #required, #attributes.
  88. *
  89. * @return array
  90. * The $element with prepared variables ready for input.html.twig.
  91. */
  92. public static function preRenderTextfield($element) {
  93. $element['#attributes']['type'] = 'text';
  94. Element::setAttributes($element, ['id', 'name', 'value', 'size', 'maxlength', 'placeholder']);
  95. static::setAttributes($element, ['form-text']);
  96. return $element;
  97. }
  98. }