DateRangeFormatterRangeFormatter.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace Drupal\date_range_formatter\Plugin\Field\FieldFormatter;
  3. use Drupal\Core\Field\FieldItemListInterface;
  4. use Drupal\Core\Form\FormStateInterface;
  5. use Drupal\datetime\Plugin\Field\FieldFormatter\DateTimeCustomFormatter;
  6. use Drupal\datetime_range\DateTimeRangeTrait;
  7. /**
  8. * Plugin implementation of the 'Custom' formatter for 'daterange' fields.
  9. *
  10. * This formatter renders the data range as plain text, with a fully
  11. * configurable date format using the PHP date syntax and separator.
  12. *
  13. * @FieldFormatter(
  14. * id = "date_range_without_time",
  15. * label = @Translation("Date range (without time)"),
  16. * field_types = {
  17. * "daterange"
  18. * }
  19. * )
  20. */
  21. class DateRangeFormatterRangeFormatter extends DateTimeCustomFormatter {
  22. use DateTimeRangeTrait;
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public static function defaultSettings() {
  27. return [
  28. 'separator' => '-',
  29. 'single' => 'd F Y',
  30. 'single_all_day' => 'd F Y',
  31. 'one_day' => 'd F Y',
  32. 'one_month' => 'd - {d} F Y',
  33. 'several_months' => 'd F - {d} {F} Y',
  34. 'several_years' => 'd F Y - {d} {F} {Y}',
  35. ] + parent::defaultSettings();
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function viewElements(FieldItemListInterface $items, $langcode) {
  41. $elements = [];
  42. foreach ($items as $delta => $item) {
  43. if (!empty($item->start_date) && !empty($item->end_date)) {
  44. /** @var \Drupal\Core\Datetime\DrupalDateTime $start_date */
  45. $start_date = $item->start_date->getTimestamp();
  46. /** @var \Drupal\Core\Datetime\DrupalDateTime $end_date */
  47. $end_date = $item->end_date->getTimestamp();
  48. if ($start_date !== $end_date) {
  49. $format = $this->getSetting('several_years');
  50. if (date('Y', $start_date) === date('Y', $end_date)) {
  51. $format = $this->getSetting('several_months');
  52. }
  53. if (date('m.Y', $start_date) === date('m.Y', $end_date)) {
  54. $format = $this->getSetting('one_month');
  55. }
  56. if (date('d.m.Y', $start_date) === date('d.m.Y', $end_date)) {
  57. $format = $this->getSetting('one_day');
  58. }
  59. $date_str = format_date($start_date, 'custom', preg_replace('/\{([a-zA-Z])\}/', '{\\\$1}', t($format)));
  60. $matches = array();
  61. if (preg_match_all('/\{([a-zA-Z])\}/', $date_str, $matches)) {
  62. foreach ($matches[1] as $match) {
  63. $date_str = preg_replace('/\{' . $match . '\}/', format_date($end_date, 'custom', $match), $date_str);
  64. }
  65. }
  66. $elements[$delta] = ['#markup' => '<span class="date-display-range">' . $date_str . '</span>',];
  67. }
  68. else {
  69. $format = $this->getSetting('one_day');
  70. $date_str = format_date($start_date, 'custom', $format);
  71. $elements[$delta] = ['#markup' => $date_str];
  72. }
  73. }
  74. }
  75. return $elements;
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. public function settingsForm(array $form, FormStateInterface $form_state) {
  81. $form = parent::settingsForm($form, $form_state);
  82. unset($form['date_format']);
  83. $form['single'] = [
  84. '#type' => 'textfield',
  85. '#title' => t('Date format for single date'),
  86. '#default_value' => $this->getSetting('single') ? : 'd F Y',
  87. ];
  88. $form['single_all_day'] = [
  89. '#type' => 'textfield',
  90. '#title' => t('Date format for the single date if the date is "all day"'),
  91. '#default_value' => $this->getSetting('single_all_day') ? : 'd F Y',
  92. ];
  93. $form['one_day'] = [
  94. '#type' => 'textfield',
  95. '#title' => t('Date format for the single day date range'),
  96. '#default_value' => $this->getSetting('one_day') ? : 'd F Y',
  97. ];
  98. $form['one_month'] = [
  99. '#type' => 'textfield',
  100. '#title' => t('Date format for the single month date range'),
  101. '#default_value' => $this->getSetting('one_month') ? : 'd - {d} F Y',
  102. ];
  103. $form['several_months'] = [
  104. '#type' => 'textfield',
  105. '#title' => t('Date format for the single year date range'),
  106. '#default_value' => $this->getSetting('several_months') ? : 'd F - {d} {F} Y',
  107. ];
  108. $form['several_years'] = [
  109. '#type' => 'textfield',
  110. '#title' => t('Date format for multiple years date range'),
  111. '#default_value' => $this->getSetting('several_years') ? : 'd F Y - {d} {F} {Y}',
  112. ];
  113. $form['help'] = [
  114. '#type' => 'markup',
  115. '#markup' => t('A user-defined date format. See the <a href="@url">PHP manual</a> for available options.', ['@url' => 'http://php.net/manual/function.date.php']) .
  116. '<br />' . t('Use letters in braces for end date elements, for example, {d} means the day of the end date.') .
  117. '<br />' . t('These format values are translated, for example, t("d F Y") instead of "d F Y" will be used as the actual date format.'),
  118. ];
  119. return $form;
  120. }
  121. /**
  122. * {@inheritdoc}
  123. */
  124. public function settingsSummary() {
  125. $summary = parent::settingsSummary();
  126. $summary[] = $this->t('Display date range using formats: @single, @single_all_day, @one_day, @one_month, @several_months, @several_years',
  127. array(
  128. '@single' => $this->getSetting('single') ? : 'd F Y',
  129. '@single_all_day' => $this->getSetting('single_all_day') ? : 'd F Y',
  130. '@one_day' => $this->getSetting('one_day') ? : 'd F Y',
  131. '@one_month' => $this->getSetting('one_month') ? : 'd - {d} F Y',
  132. '@several_months' => $this->getSetting('several_months') ? : 'd F - {d} {F} Y',
  133. '@several_years' => $this->getSetting('several_years') ? : 'd F Y - {d} {F} {Y}',
  134. )
  135. );
  136. return $summary;
  137. }
  138. }