Date.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace Drupal\Core\Render\Element;
  3. use Drupal\Core\Form\FormStateInterface;
  4. use Drupal\Core\Render\Element;
  5. /**
  6. * Provides a form element for date selection.
  7. *
  8. * Properties:
  9. * - #default_value: An array with the keys: 'year', 'month', and 'day'.
  10. * Defaults to the current date if no value is supplied.
  11. * - #size: The size of the input element in characters.
  12. *
  13. * @code
  14. * $form['expiration'] = array(
  15. * '#type' => 'date',
  16. * '#title' => $this->t('Content expiration'),
  17. * '#default_value' => array('year' => 2020, 'month' => 2, 'day' => 15,)
  18. * );
  19. * @endcode
  20. *
  21. * @FormElement("date")
  22. */
  23. class Date extends FormElement {
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public function getInfo() {
  28. $class = get_class($this);
  29. return [
  30. '#input' => TRUE,
  31. '#theme' => 'input__date',
  32. '#process' => [
  33. [$class, 'processAjaxForm'],
  34. [$class, 'processDate'],
  35. ],
  36. '#pre_render' => [[$class, 'preRenderDate']],
  37. '#theme_wrappers' => ['form_element'],
  38. '#attributes' => ['type' => 'date'],
  39. '#date_date_format' => 'Y-m-d',
  40. ];
  41. }
  42. /**
  43. * Processes a date form element.
  44. *
  45. * @param array $element
  46. * The form element to process. Properties used:
  47. * - #attributes: An associative array containing:
  48. * - type: The type of date field rendered.
  49. * - #date_date_format: The date format used in PHP formats.
  50. * @param \Drupal\Core\Form\FormStateInterface $form_state
  51. * The current state of the form.
  52. * @param array $complete_form
  53. * The complete form structure.
  54. *
  55. * @return array
  56. * The processed element.
  57. */
  58. public static function processDate(&$element, FormStateInterface $form_state, &$complete_form) {
  59. // Attach JS support for the date field, if we can determine which date
  60. // format should be used.
  61. if ($element['#attributes']['type'] == 'date' && !empty($element['#date_date_format'])) {
  62. $element['#attached']['library'][] = 'core/drupal.date';
  63. $element['#attributes']['data-drupal-date-format'] = [$element['#date_date_format']];
  64. }
  65. return $element;
  66. }
  67. /**
  68. * Adds form-specific attributes to a 'date' #type element.
  69. *
  70. * Supports HTML5 types of 'date', 'datetime', 'datetime-local', and 'time'.
  71. * Falls back to a plain textfield with JS datepicker support. Used as a
  72. * sub-element by the datetime element type.
  73. *
  74. * @param array $element
  75. * An associative array containing the properties of the element.
  76. * Properties used: #title, #value, #options, #description, #required,
  77. * #attributes, #id, #name, #type, #min, #max, #step, #value, #size. The
  78. * #name property will be sanitized before output. This is currently done by
  79. * initializing Drupal\Core\Template\Attribute with all the attributes.
  80. *
  81. * @return array
  82. * The $element with prepared variables ready for #theme 'input__date'.
  83. */
  84. public static function preRenderDate($element) {
  85. if (empty($element['#attributes']['type'])) {
  86. $element['#attributes']['type'] = 'date';
  87. }
  88. Element::setAttributes($element, ['id', 'name', 'type', 'min', 'max', 'step', 'value', 'size']);
  89. static::setAttributes($element, ['form-' . $element['#attributes']['type']]);
  90. return $element;
  91. }
  92. }