I have a site where there is only Month/Day/Year being captured in a field and no Time.

The module breaks when the same Month, Day, Year (no time) is used for the start and end date.

I believe this is because the 'single' setting was taken out, but is still being called in DateRangeFormatterRangeFormatter.php on line 80 to set the $single_format variable. This variable which is now empty is passed to the t() in line 81. Results in any page that displays the formatter to break.

It works if you change the getSetting to "one_day"

Old

$single_format = $this->getSetting('single');
$elements[$delta] = ['#markup' => \Drupal::service('date.formatter')->format($start_date, 'custom', t($single_format))];

New

$single_format = $this->getSetting('one_day');
$elements[$delta] = ['#markup' => \Drupal::service('date.formatter')->format($start_date, 'custom', t($single_format))];

I would submit a patch, but for some reason I'm having a hard time publishing it to Drupal.org. Maybe someone wants to test this out and submit a patch?

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

MikeyR created an issue. See original summary.

nicholass’s picture

I am also getting this error after updating the module from

Upgrading drupal/date_range_formatter (dev-9.0.x a1bbc67 => dev-9.0.x 8808a9c)

My Error is similar

InvalidArgumentException: $string ("") must be a string. in Drupal\Core\StringTranslation\TranslatableMarkup->__construct() (line 132 of core/lib/Drupal/Core/StringTranslation/TranslatableMarkup.php).
t(NULL) (Line: 81)
Drupal\date_range_formatter\Plugin\Field\FieldFormatter\DateRangeFormatterRangeFormatter->viewElements(Object, 'en') (Line: 89)
Drupal\Core\Field\FormatterBase->view(Object, 'en') (Line: 263)
Drupal\Core\Entity\Entity\EntityViewDisplay->buildMultiple(Array) (Line: 256)
nicholass’s picture

nicholass’s picture

So even after running database updates the error persists, so I will go ahead can create a patch as you suggested. Since changing line 80 $this->getSetting('one_day'); seems to work.

nicholass’s picture

Status: Active » Needs review

Changing this to needs review as it works now on my local and all my views using date ranges appear to be working normally.

nicholass’s picture

davidiio’s picture

We ha the same problem described in #2 and we've changed
$single_format = $this->getSetting('single');
to
$single_format = $this->getSetting('one_day');
And it seems to solve the probleme. Date range is displayed properly without error in dblog.
Thanks :)

nickdickinsonwilde’s picture

Status: Needs review » Reviewed & tested by the community
vlyalko’s picture

StatusFileSize
new843 bytes
kenrbnsn’s picture

This doesn't seem to fix the formatting problem.

I patched v4.0.1 with the patch.

Here is my test record -- see the file "Date and time - no end date.jpg"

Here's how it's displayed -- see the file "Date and time - no end date display.jpg"

bapi_22’s picture

StatusFileSize
new850 bytes

@here the patch is applying correctly with 4.0.2 version.

Uploading a clean patch

bapi_22’s picture

Status: Reviewed & tested by the community » Needs review
tobiasb’s picture

fyi: The format single, single_all_day was removed via #2947426: Rename "Date range (without time)" formatter, the settings form for single, single_all_day was added again via #3151725: t() calls should be avoided in classes.

taraskorpach made their first commit to this issue’s fork.

taraskorpach’s picture

Status: Needs review » Reviewed & tested by the community

I've rebased the commits from the latest release.

The patch from #12 seems good to me; it has been applied, and the issue is resolved. Therefore, I'm marking this as RTBC.

I hope that the fix will be included in the next release, as it is quite critical.

nicholass’s picture

StatusFileSize
new64.35 KB

So I just removed my patch for https://git.drupalcode.org/project/date_range_formatter/-/merge_requests... and then upgraded drupal/date_range_formatter (dev-9.0.x 8808a9c => dev-9.0.x d1f07a7).

And I am back to seeing a white screen on my site

The website encountered an unexpected error. Please try again later.

InvalidArgumentException: $string ("") must be a string. in Drupal\Core\StringTranslation\TranslatableMarkup->__construct() (line 132 of core/lib/Drupal/Core/StringTranslation/TranslatableMarkup.php).
Drupal\date_range_formatter\Plugin\Field\FieldFormatter\DateRangeFormatterRangeFormatter->t(NULL) (Line: 91)
Drupal\date_range_formatter\Plugin\Field\FieldFormatter\DateRangeFormatterRangeFormatter->viewElements(Object, 'en') (Line: 89)
Drupal\Core\Field\FormatterBase->view(Object, 'en') (Line: 265)
Drupal\Core\Entity\Entity\EntityViewDisplay->buildMultiple(Array) (Line: 268)
Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay->buildMultiple(Array) (Line: 257)
Drupal\views\Entity\Render\EntityFieldRenderer->buildFields(Array) (Line: 143)
Drupal\views\Entity\Render\EntityFieldRenderer->render(Object, Object) (Line: 838)
Drupal\views\Plugin\views\field\EntityField->getItems(Object) (Line: 1189)
Drupal\views\Plugin\views\field\FieldPluginBase->advancedRender(Object) (Line: 238)
template_preprocess_views_view_field(Array, 'views_view_field', Array)
call_user_func_array('template_preprocess_views_view_field', Array) (Line: 285)
Drupal\Core\Theme\ThemeManager->render('views_view_field', Array) (Line: 445)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 204)
Drupal\Core\Render\Renderer->render(Array) (Line: 1784)
Drupal\views\Plugin\views\field\FieldPluginBase->theme(Object) (Line: 765)
Drupal\views\Plugin\views\style\StylePluginBase->elementPreRenderRow(Array)
call_user_func_array(Array, Array) (Line: 111)
Drupal\Core\Render\Renderer->doTrustedCallback(Array, Array, 'Render #pre_render callbacks must be methods of a class that implements \Drupal\Core\Security\TrustedCallbackInterface or be an anonymous function. The callback was %s. See https://www.drupal.org/node/2966725', 'exception', 'Drupal\Core\Render\Element\RenderCallbackInterface') (Line: 797)
Drupal\Core\Render\Renderer->doCallback('#pre_render', Array, Array) (Line: 386)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 204)
Drupal\Core\Render\Renderer->render(Array) (Line: 704)
Drupal\views\Plugin\views\style\StylePluginBase->renderFields(Array) (Line: 570)
Drupal\views\Plugin\views\style\StylePluginBase->renderGrouping(Array, Array, 1) (Line: 461)
Drupal\views\Plugin\views\style\StylePluginBase->render(Array) (Line: 2181)
Drupal\views\Plugin\views\display\DisplayPluginBase->render() (Line: 1548)
Drupal\views\ViewExecutable->render() (Line: 131)
Drupal\views\Plugin\views\display\Block->execute() (Line: 1645)
Drupal\views\ViewExecutable->executeDisplay('block_3', Array) (Line: 81)

So is there some kind of missing database update or something? Do I need to change my views? I tried to resave the date fields but that didn't seem to work either.

nicholass’s picture

StatusFileSize
new278.57 KB

Here is an xdebug screenshot if that helps debug, im not sure what needs fixed....

nicholass’s picture

StatusFileSize
new431.39 KB

Here is another screenshot higher up the call stack

nicholass’s picture

Ok so I updated this forks branch with commits from the source, and re-applied the original change. So the new diff is https://git.drupalcode.org/project/date_range_formatter/-/merge_requests... and seems to work for me.

nigelcunningham’s picture

StatusFileSize
new981 bytes

Static version of MR for use in composer patching.

lowfidelity’s picture

Patch from #12 resolves the issue for us on Drupal 10.2.2

demonde changed the visibility of the branch 3309324-POST-RTBC-Fix-single-day to hidden.

demonde changed the visibility of the branch 3309324-POST-RTBC-Fix-single-day to active.

teknocat’s picture

These patches are not the correct solution to the problem. The issue is that "single" and "single_all_day" options were added to the settings form, but not defined in the schema YML or provided with the default options. They also weren't displayed in the summary.

I'll create another issue fork that makes those corrections.

jeffam made their first commit to this issue’s fork.

jeffam’s picture

I added the 3309324-single-day branch to create a patch against the dev version of this module.

Here's the URL for the patch from the one commit on that branch:

https://git.drupalcode.org/issue/date_range_formatter-3309324/-/commit/4...

This patch only works against the the 9.0.x dev version.

majorrobot’s picture

Version: 4.0.1 » 4.0.2

Can confirm this is also broken in 4.0.2.

However, the most recent fix (3309324-single-day) does the trick (at least for the dev version of the module). Would be great to get this merged in and released so we don't have to depend on the dev version!

bachbach’s picture

StatusFileSize
new850 bytes

here is a patch against 4.0.2

pefferen’s picture

The patch in #30 works for us, we use version 4.0.2

It introduced another issue, if the start and end date and time are the same, the date is rendered incorrectly if the one_day time format is configured to include the start and end time as well. The module assumes that no end date is needed to render the date if the start and end timestamps are the same.
For this I created a new issue #3492241

sea2709’s picture

StatusFileSize
new1.63 KB

This patch is an improvement from the patch #30 I see in the formatter configuration, we have 'single' and 'single_all_day' settings but we've never used it. So this patch removes these settings from the settings form.

norman.lol’s picture

#3309324-30: Passing empty value to t() when same day + time for start and end date is working around the issue so far.

But I'm really confused why we pass a variable to t() in the first place. You should never use t() to translate variables.

norman.lol’s picture

Status: Reviewed & tested by the community » Needs work

This one needs work. We can't simply drop the single config. Because now dates without an end date will have no way to be configured.

norman.lol’s picture

Status: Needs work » Needs review
StatusFileSize
new6.09 KB

I think this is how it is supposed to be. Added single back and removed t() around variables.

pefferen’s picture

Status: Needs review » Reviewed & tested by the community

The patch in comment #35 looks good and works fine in my tests +1 for removing the translation of the variable.

rahul_’s picture

We identified similar issue with the latest code and we fixed using the below Patch

diff --git a/src/Plugin/Field/FieldFormatter/DateRangeFormatterRangeFormatter.php b/src/Plugin/Field/FieldFormatter/DateRangeFormatterRangeFormatter.php
index bbc5a9c..ea858ad 100644
--- a/src/Plugin/Field/FieldFormatter/DateRangeFormatterRangeFormatter.php
+++ b/src/Plugin/Field/FieldFormatter/DateRangeFormatterRangeFormatter.php
@@ -72,6 +72,7 @@ class DateRangeFormatterRangeFormatter extends DateTimeCustomFormatter {
             if (date('d.m.Y', $start_date) === date('d.m.Y', $end_date)) {
               $format = $this->getSetting('one_day');
             }
+            $format = is_string($format) && $format !== '' ? $format : 'd.m.Y';
             $date_str = $this->dateFormatter
               // Since Drupal does not support the translation of formatter
               // settings, we use 'phpcs:ignore' here.
@@ -92,6 +93,7 @@ class DateRangeFormatterRangeFormatter extends DateTimeCustomFormatter {
           // No end date provided or end date equals start date use single
           // formatting.
           $single_format = $this->getSetting('single');
+          $single_format = is_string($single_format) && $single_format !== '' ? $single_format : 'd.m.Y';
           $elements[$delta] = $this->buildDateRenderArray(
             $this->formatDateToRfc3339($start_date),
             // phpcs:ignore Drupal.Semantics.FunctionT.NotLiteralString
rahul_’s picture

StatusFileSize
new1.34 KB