Passing empty value to t() when same day + time for start and end date

Created on 13 September 2022, over 2 years ago
Updated 21 December 2022, about 2 years ago

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?

🐛 Bug report
Status

RTBC

Version

4.0

Component

Code

Created by

🇺🇸United States MikeyR

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇮🇳India bapi_22

    @here the patch is applying correctly with 4.0.2 version.

    Uploading a clean patch

  • Status changed to Needs review about 1 year ago
  • 🇩🇪Germany tobiasb Berlin

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

  • First commit to issue fork.
  • Status changed to RTBC 12 months ago
  • 🇺🇦Ukraine taraskorpach Lutsk 🇺🇦

    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.

  • 🇺🇸United States NicholasS

    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.

  • 🇺🇸United States NicholasS

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

  • 🇺🇸United States NicholasS

    Here is another screenshot higher up the call stack

  • @nicholass opened merge request.
  • 🇺🇸United States NicholasS

    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.

  • 🇦🇺Australia nigelcunningham Geelong

    Static version of MR for use in composer patching.

  • 🇦🇹Austria paper boy

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

  • 🇩🇪Germany demonde

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

  • 🇩🇪Germany demonde

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

  • 🇨🇦Canada teknocat

    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.

  • First commit to issue fork.
  • 🇺🇸United States jeffam

    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.

  • 🇺🇸United States majorrobot

    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!

  • 🇫🇷France bachbach Le Havre

    here is a patch against 4.0.2

  • 🇳🇱Netherlands pefferen

    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 🐛 Single date and time rendering incorrect Active

  • 🇺🇸United States sea2709 Texas

    This patch is an improvement from the patch #30 🐛 Passing empty value to t() when same day + time for start and end date RTBC 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.

  • leymannx Berlin

    #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.

  • leymannx Berlin

    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.

  • leymannx Berlin

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

Production build 0.71.5 2024