DateElementBase.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace Drupal\Core\Datetime\Element;
  3. use Drupal\Component\Utility\NestedArray;
  4. use Drupal\Core\Datetime\DrupalDateTime;
  5. use Drupal\Core\Render\Element\FormElement;
  6. /**
  7. * Provides a base class for date elements.
  8. */
  9. abstract class DateElementBase extends FormElement {
  10. /**
  11. * Specifies the start and end year to use as a date range.
  12. *
  13. * Handles a string like -3:+3 or 2001:2010 to describe a dynamic range of
  14. * minimum and maximum years to use in a date selector.
  15. *
  16. * Centers the range around the current year, if any, but expands it far enough
  17. * so it will pick up the year value in the field in case the value in the field
  18. * is outside the initial range.
  19. *
  20. * @param string $string
  21. * A min and max year string like '-3:+1' or '2000:2010' or '2000:+3'.
  22. * @param object $date
  23. * (optional) A date object to test as a default value. Defaults to NULL.
  24. *
  25. * @return array
  26. * A numerically indexed array, containing the minimum and maximum year
  27. * described by this pattern.
  28. */
  29. protected static function datetimeRangeYears($string, $date = NULL) {
  30. $datetime = new DrupalDateTime();
  31. $this_year = $datetime->format('Y');
  32. list($min_year, $max_year) = explode(':', $string);
  33. // Valid patterns would be -5:+5, 0:+1, 2008:2010.
  34. $plus_pattern = '@[\+|\-][0-9]{1,4}@';
  35. $year_pattern = '@^[0-9]{4}@';
  36. if (!preg_match($year_pattern, $min_year, $matches)) {
  37. if (preg_match($plus_pattern, $min_year, $matches)) {
  38. $min_year = $this_year + $matches[0];
  39. }
  40. else {
  41. $min_year = $this_year;
  42. }
  43. }
  44. if (!preg_match($year_pattern, $max_year, $matches)) {
  45. if (preg_match($plus_pattern, $max_year, $matches)) {
  46. $max_year = $this_year + $matches[0];
  47. }
  48. else {
  49. $max_year = $this_year;
  50. }
  51. }
  52. // We expect the $min year to be less than the $max year. Some custom values
  53. // for -99:+99 might not obey that.
  54. if ($min_year > $max_year) {
  55. $temp = $max_year;
  56. $max_year = $min_year;
  57. $min_year = $temp;
  58. }
  59. // If there is a current value, stretch the range to include it.
  60. $value_year = $date instanceof DrupalDateTime ? $date->format('Y') : '';
  61. if (!empty($value_year)) {
  62. $min_year = min($value_year, $min_year);
  63. $max_year = max($value_year, $max_year);
  64. }
  65. return [$min_year, $max_year];
  66. }
  67. /**
  68. * Returns the most relevant title of a datetime element.
  69. *
  70. * Since datetime form elements often consist of combined date and time fields
  71. * the element title might not be located on the element itself but on the
  72. * parent container element.
  73. *
  74. * @param array $element
  75. * The element being processed.
  76. * @param array $complete_form
  77. * The complete form structure.
  78. *
  79. * @return string
  80. * The title.
  81. */
  82. protected static function getElementTitle($element, $complete_form) {
  83. $title = '';
  84. if (!empty($element['#title'])) {
  85. $title = $element['#title'];
  86. }
  87. else {
  88. $parents = $element['#array_parents'];
  89. array_pop($parents);
  90. $parent_element = NestedArray::getValue($complete_form, $parents);
  91. if (!empty($parent_element['#title'])) {
  92. $title = $parent_element['#title'];
  93. }
  94. }
  95. return $title;
  96. }
  97. }