Commit 2588349a authored by Chris Green's avatar Chris Green Committed by Shelane French
Browse files

Issue #3167284 by joegraduate, trackleft2, mark_fullmer, shelane: Support...

Issue #3167284 by joegraduate, trackleft2, mark_fullmer, shelane: Support Smart Date field type in RangeFieldFormatter
parent b8758d30
Loading
Loading
Loading
Loading
+15 −5
Original line number Diff line number Diff line
@@ -108,11 +108,13 @@ class ApStyleDateFormatter {
   *   default system time zone. Defaults to NULL.
   * @param string $langcode
   *   The language code.
   * @param string $fieldtype
   *   Type of field. Example smartdate.
   *
   * @return string
   *   The formatted date string.
   */
  public function formatTimestamp($timestamp, array $options = [], $timezone = NULL, $langcode = NULL) {
  public function formatTimestamp($timestamp, array $options = [], $timezone = NULL, $langcode = NULL, $fieldtype = NULL) {
    if (empty($options)) {
      $options = $this->getOptions();
    }
@@ -221,11 +223,13 @@ class ApStyleDateFormatter {
   *   default system time zone. Defaults to NULL.
   * @param string $langcode
   *   The language code.
   * @param string $fieldtype
   *   Type of field. Example smartdate.
   *
   * @return string
   *   The formatted date string.
   */
  public function formatRange(array $timestamps, array $options = [], $timezone = NULL, $langcode = NULL) {
  public function formatRange(array $timestamps, array $options = [], $timezone = NULL, $langcode = NULL, $fieldtype = NULL) {
    if (empty($options)) {
      $options = $this->getOptions();
    }
@@ -233,7 +237,6 @@ class ApStyleDateFormatter {
    if (empty($langcode)) {
      $langcode = $this->languageManager->getCurrentLanguage()->getId();
    }

    // If no timezone is specified, use the user's if available, or the site
    // or system default.
    if (empty($timezone)) {
@@ -308,9 +311,16 @@ class ApStyleDateFormatter {
      }

      if (isset($options['display_time']) && $options['display_time']) {
        if (isset($options['use_all_day']) && $options['use_all_day'] && ($start_stamp->format('H:i') == '00:00' || $start_stamp->format('gia') == $end_stamp->format('gia'))) {
        if (isset($options['use_all_day']) && $options['use_all_day']) {
          if (isset($fieldtype) && $fieldtype === 'smartdate') {
            if ($start_stamp->format('H:i') === '00:00' && $end_stamp->format('H:i') === '23:59') {
              $time_output = $this->t('All Day');
            }
          }
          elseif ($start_stamp->format('H:i') == '00:00' || $start_stamp->format('gia') == $end_stamp->format('gia')) {
            $time_output = $this->t('All Day');
          }
        }
        else {
          $capital = (isset($options['capitalize_noon_and_midnight']) && $options['capitalize_noon_and_midnight']);
          // Don't display the minutes if it's the top of the hour.
+10 −15
Original line number Diff line number Diff line
@@ -56,7 +56,6 @@ class ApStyleDateFieldFormatter extends FormatterBase implements ContainerFactor
   */
  public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, ApStyleDateFormatter $date_formatter) {
    parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);

    $this->apStyleDateFormatter = $date_formatter;
  }

@@ -213,33 +212,30 @@ class ApStyleDateFieldFormatter extends FormatterBase implements ContainerFactor
    }

    if ($this->getSetting('use_today')) {
      $today = '';
      $suffix = '';
      if ($this->getSetting('cap_today')) {
        $today = ' (capitalized)';
        $suffix = $this->t('(capitalized)');
      }
      $summary[] = $this->t('Displaying today@today', ['@today' => $today]);
      $summary[] = $this->t('Displaying today @suffix', ['@suffix' => $suffix]);
    }

    if ($this->getSetting('display_time')) {
      $display_time = $this->t('Displaying time');
      if ($this->getSetting('time_before_date')) {
        $display_time .= ' (before date)';
        $suffix = $this->t('(before date)');
      }
      else {
        $display_time .= ' (after date)';
        $suffix = $this->t('(after date)');
      }
      $summary[] = $display_time;
      $summary[] = $this->t('Displaying time @suffix', ['@suffix' => $suffix]);
      if ($this->getSetting('use_all_day')) {
        $summary[] = 'Show "All Day" instead of midnight';
        $summary[] = $this->t('Show "All Day" instead of midnight');
      }
      elseif ($this->getSetting('display_noon_and_midnight')) {
        $noon_and_midnight = '';
        $suffix = '';
        if ($this->getSetting('capitalize_noon_and_midnight')) {
          $noon_and_midnight = ' (capitalized)';
          $suffix = $this->t('(capitalized)');
        }
        $summary[] = $this->t('Displaying noon and midnight@noon_and_midnight',
          ['@noon_and_midnight' => $noon_and_midnight]
        );
        $summary[] = $this->t('Displaying noon and midnight @suffix', ['@suffix' => $suffix]);
      }
    }

@@ -278,7 +274,6 @@ class ApStyleDateFieldFormatter extends FormatterBase implements ContainerFactor
    $timezone = $this->getSetting('timezone') ?: NULL;

    $field_type = $items->getFieldDefinition()->getType();

    foreach ($items as $delta => $item) {
      if ($field_type == 'datetime') {
        $timestamp = $item->date->getTimestamp();
+23 −18
Original line number Diff line number Diff line
@@ -18,7 +18,8 @@ use Drupal\datetime_range\DateTimeRangeTrait;
 *   id = "daterange_ap_style",
 *   label = @Translation("AP Style"),
 *   field_types = {
 *     "daterange"
 *     "daterange",
 *     "smartdate",
 *   }
 * )
 */
@@ -216,33 +217,30 @@ class ApStyleDateRangeFieldFormatter extends FormatterBase implements ContainerF
    }

    if ($this->getSetting('use_today')) {
      $today = '';
      $suffix = '';
      if ($this->getSetting('cap_today')) {
        $today = ' (capitalized)';
        $suffix = $this->t('(capitalized)');
      }
      $summary[] = $this->t('Displaying today@today', ['@today' => $today]);
      $summary[] = $this->t('Displaying today @suffix', ['@suffix' => $suffix]);
    }

    if ($this->getSetting('display_time')) {
      $display_time = $this->t('Displaying time');
      if ($this->getSetting('time_before_date')) {
        $display_time .= ' (before date)';
        $suffix = $this->t('(before date)');
      }
      else {
        $display_time .= ' (after date)';
        $suffix = $this->t('(after date)');
      }
      $summary[] = $display_time;
      $summary[] = $this->t('Displaying time @suffix', ['@suffix' => $suffix]);
      if ($this->getSetting('use_all_day')) {
        $summary[] = 'Show "All Day" instead of midnight';
        $summary[] = $this->t('Show "All Day" instead of midnight');
      }
      elseif ($this->getSetting('display_noon_and_midnight')) {
        $noon_and_midnight = '';
        $suffix = '';
        if ($this->getSetting('capitalize_noon_and_midnight')) {
          $noon_and_midnight = ' (capitalized)';
          $suffix = $this->t('(capitalized)');
        }
        $summary[] = $this->t('Displaying noon and midnight@noon_and_midnight',
          ['@noon_and_midnight' => $noon_and_midnight]
        );
        $summary[] = $this->t('Displaying noon and midnight @suffix', ['@suffix' => $suffix]);
      }
    }

@@ -286,24 +284,31 @@ class ApStyleDateRangeFieldFormatter extends FormatterBase implements ContainerF
    }

    $timezone = $this->getSetting('timezone') ?: NULL;

    $fieldtype = $items->getFieldDefinition()->getType();
    foreach ($items as $delta => $item) {
      if (!empty($item->start_date) && !empty($item->end_date)) {
        /** @var \Drupal\Core\Datetime\DrupalDateTime $start_date */
        $start_date = $item->start_date;
        /** @var \Drupal\Core\Datetime\DrupalDateTime $end_date */
        $end_date = $item->end_date;

        $dates['start'] = $start_date->getTimestamp();
        $dates['end'] = $end_date->getTimestamp();

      }
      if ($fieldtype === 'smartdate') {
        if (!empty($item->value) && !empty($item->end_value)) {
          // Check for data structure of smartdate field.
          $dates['start'] = $item->value;
          $dates['end'] = $item->end_value;
        }
      }
      if (isset($dates['start']) && isset($dates['end'])) {
        $elements[$delta] = [
          '#cache' => [
            'contexts' => [
              'timezone',
            ],
          ],
          '#markup' => $this->apStyleDateFormatter->formatRange($dates, $options, $timezone, $langcode),
          '#markup' => $this->apStyleDateFormatter->formatRange($dates, $options, $timezone, $langcode, $fieldtype),
        ];
      }
    }