Search.php 1.5 KB

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