TypeError in formatRange

Created on 15 September 2023, over 1 year ago

Problem/Motivation

After upgrade to 8.x-1.6 if I don't have the option selected to show the time this error is produced
TypeError: Drupal\date_ap_style\ApStyleDateFormatter::formatRange(): Return value must be of type string, null returned in Drupal\date_ap_style\ApStyleDateFormatter->formatRange() (line 293 of modules/contrib/date_ap_style/src/ApStyleDateFormatter.php).

Steps to reproduce

Have a date/time range field configured fill in data and have the formatter set to only show the date and not the time

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

πŸ› Bug report
Status

Fixed

Version

1.6

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States emerham

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @emerham
  • πŸ‡ΊπŸ‡ΈUnited States shelane

    Thanks for reporting this. I was able to replicate it with another case as all of these methods were given an updated signature with typing. In my case of a date that is not a date range date, this fix worked.

    Can you test this patch?

  • πŸ‡ΊπŸ‡ΈUnited States shelane
  • πŸ‡ΊπŸ‡ΈUnited States emerham

    Still getting the same error in the function formatRange.

    I think there's an issue in the formatRange, intellephese is saying that $output on line 293 is "probably undefined". Checking the logic it looks like if I don't have the option of "Show time" checked that $output will never be set and thus is null and being returned as such

    if (isset($options['display_time']) && $options['display_time']) {
            $date_output = $start_stamp->format($format_start_date);
            if (!empty($format_end_date)) {
              $date_output .= (isset($options['separator']) && $options['separator'] == 'endash' ? ' – ' : ' to ') . $end_stamp->format($format_end_date);
            }
            $time_output = $this->getTimeOutput($normalized_timestamps, $options, $fieldtype);
            if (!empty($time_output) && isset($options['time_before_date']) && $options['time_before_date']) {
              $output = $time_output . ', ' . $date_output;
            }
            elseif (!empty($time_output)) {
              $output = $date_output . ', ' . $time_output;
            }
            else {
              $output = $date_output;
            }
          }
          return $output;
    
  • πŸ‡ΊπŸ‡ΈUnited States emerham

    I moved the if (isset($options['display_time']) && $options['display_time']) down 4 lines and added a new else statement to ensure that $output will have a value.

    did some testing both showing and not showing the time date time range that is both same day and different days at different times.

  • πŸ‡ΊπŸ‡ΈUnited States kmonahan

    Was just in the process of making a patch with the same change. :) Can confirm that #5 fixes the issue for me.

  • Status changed to Fixed over 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States shelane

    Yep. That's it. Thank you very much!

  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024