Tel.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace Drupal\Core\Render\Element;
  3. use Drupal\Core\Render\Element;
  4. /**
  5. * Provides a form element for entering a telephone number.
  6. *
  7. * Provides an HTML5 input element with type of "tel". It provides no special
  8. * validation.
  9. *
  10. * Properties:
  11. * - #size: The size of the input element in characters.
  12. * - #pattern: A string for the native HTML5 pattern attribute.
  13. *
  14. * Usage example:
  15. * @code
  16. * $form['phone'] = array(
  17. * '#type' => 'tel',
  18. * '#title' => $this->t('Phone'),
  19. * '#pattern' => '[^\d]*',
  20. * );
  21. * @endcode
  22. *
  23. * @see \Drupal\Core\Render\Element
  24. *
  25. * @FormElement("tel")
  26. */
  27. class Tel extends FormElement {
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function getInfo() {
  32. $class = get_class($this);
  33. return [
  34. '#input' => TRUE,
  35. '#size' => 30,
  36. '#maxlength' => 128,
  37. '#autocomplete_route_name' => FALSE,
  38. '#process' => [
  39. [$class, 'processAutocomplete'],
  40. [$class, 'processAjaxForm'],
  41. [$class, 'processPattern'],
  42. ],
  43. '#pre_render' => [
  44. [$class, 'preRenderTel'],
  45. ],
  46. '#theme' => 'input__tel',
  47. '#theme_wrappers' => ['form_element'],
  48. ];
  49. }
  50. /**
  51. * Prepares a #type 'tel' render element for input.html.twig.
  52. *
  53. * @param array $element
  54. * An associative array containing the properties of the element.
  55. * Properties used: #title, #value, #description, #size, #maxlength,
  56. * #placeholder, #required, #attributes.
  57. *
  58. * @return array
  59. * The $element with prepared variables ready for input.html.twig.
  60. */
  61. public static function preRenderTel($element) {
  62. $element['#attributes']['type'] = 'tel';
  63. Element::setAttributes($element, ['id', 'name', 'value', 'size', 'maxlength', 'placeholder']);
  64. static::setAttributes($element, ['form-tel']);
  65. return $element;
  66. }
  67. }