Currently when you have an activity from e.g. 2017-03-01 13:00 until 2017-03-01 15:00, it gets displayed with twice the actual day visible. In this case it would be nice if the end date could be formatted in a shorter manner, with only the hours. Meaning is not lost, and it's easier to read.

An example from my project (in Dutch however):

Current: za, 28/01/2017 14:00 - za, 28/01/2017 18:00
Could become: za, 28/01/2017 14:00 - 18:00

Comments

Cyberwolf created an issue. See original summary.

cyberwolf’s picture

I've created my own formatter for now, in a custom module.

<?php

namespace Drupal\mymodule\Plugin\Field\FieldFormatter;

use Drupal\Core\Annotation\Translation;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Field\Annotation\FieldFormatter;
use Drupal\Core\Form\FormStateInterface;
use Drupal\date_recur\Plugin\Field\FieldFormatter\DateRecurDefaultFormatter;

/**
 * @FieldFormatter(
 *   id = "mymodule_event_date_recur_formatter",
 *   label = @Translation("mymodule event date recur formatter"),
 *   field_types = {
 *     "date_recur"
 *   }
 * )
 */
class DateRecurFormatter extends DateRecurDefaultFormatter {

  /**
   * {@inheritdoc}
   */
  public static function defaultSettings() {
    return array(
        'end_date_format_type' => 'medium',
        'occurrence_end_date_format_type' => 'medium',
      ) + parent::defaultSettings();
  }

  /**
   * {@inheritdoc}
   */
  public function settingsForm(array $form, FormStateInterface $form_state) {
    $form = parent::settingsForm($form, $form_state);

    $form['end_date_format_type'] = $form['format_type'];
    $form['end_date_format_type']['#title'] .=  ' ' . t('(For end date when only its time differs)');
    $form['end_date_format_type']['#default_value'] = $this->getSetting('end_date_format_type');

    $form['occurrence_end_date_format_type'] = $form['format_type'];
    $form['occurrence_end_date_format_type']['#title'] .=  ' ' . t('(Occurences, for end date when only its time differs)');
    $form['occurrence_end_date_format_type']['#default_value'] = $this->getSetting('occurrence_end_date_format_type');

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function settingsSummary() {
    $summary = parent::settingsSummary();

    $date = new DrupalDateTime();
    $date->_dateRecurIsOccurrence = FALSE;
    $date->_useAlternativeEndDateFormat = TRUE;
    $summary[] = t('End date format: @display', array('@display' => $this->formatDate($date)));

    $date->_dateRecurIsOccurrence = TRUE;
    $summary[] = t('Occurrence end date format: @display', array('@display' => $this->formatDate($date)));
    return $summary;
  }

  /**
   * @param DrupalDateTime $start_date
   * @param DrupalDateTime $end_date
   * @param bool $isOccurrence
   * @return array
   */
  protected function buildDateRangeValue($start_date, $end_date, $isOccurrence = FALSE) {
    if ($isOccurrence) {
      $start_date->_dateRecurIsOccurrence = $end_date->_dateRecurIsOccurrence = TRUE;
    }

    // Use an alternative format for end when only its time component differs
    // from start.
    if ($end_date->format('Y-m-d') === $start_date->format('Y-m-d')) {
      $end_date->_useAlternativeEndDateFormat = TRUE;
    }

    if ($start_date->format('U') !== $end_date->format('U')) {
      $element = [
        'start_date' => $this->buildDateWithIsoAttribute($start_date),
        'separator' => ['#plain_text' => ' ' . $this->getSetting('separator') . ' '],
        'end_date' => $this->buildDateWithIsoAttribute($end_date),
      ];
    }
    else {
      $element = $this->buildDateWithIsoAttribute($start_date);
    }
    return $element;
  }

  protected function formatDate($date) {
    $format_type_setting_parts = [];
    if (!empty($date->_dateRecurIsOccurrence)) {
      $format_type_setting_parts[] = 'occurrence';
    }
    if (!empty($date->_useAlternativeEndDateFormat)) {
      $format_type_setting_parts[] = 'end_date';
    }

    $format_type_setting_parts[] = 'format_type';
    $format_type_setting_name = implode('_', $format_type_setting_parts);

    $format_type = $this->getSetting($format_type_setting_name);

    $timezone = $this->getSetting('timezone_override') ?: $date->getTimezone()->getName();
    return $this->dateFormatter->format($date->getTimestamp(), $format_type, '', $timezone != '' ? $timezone : NULL);
  }
}

If there is enough interest to incorporate this into the date_recur module and you can give me your opinion on how you would love to see this integrated (a new formatter? in the existing formatter but optional?) then I will make some time to provide a patch.

dpi’s picture

Component: Code » Formatter
dpi’s picture

Status: Active » Closed (outdated)

Same day alternative end date format was committed in 771e3ba1.