PHP8 + MAX aggregation in Views on Date fields throw error

Created on 9 August 2022, almost 3 years ago
Updated 27 January 2023, over 2 years ago

watchdog error:
TypeError: round(): Argument #1 ($num) must be of type int|float, string given in round() (line 173 of /xxx_proj_folder/docroot/core/modules/views/src/Plugin/views/field/NumericField.php)
#0 /xxx_proj_folder/docroot/core/modules/views/src/Plugin/views/field/NumericField.php(173): round('2021-06-29', 0)

code:
$value = round($value, $precision);
value is string '2021-06-29' (just example)

if php version is 7, it will return '2021' and no error,
php 8 will throw this error.

related issue:
https://www.drupal.org/project/drupal/issues/3151654 πŸ› Date field is not displaying correct value on a views with aggregation max/min Needs work

πŸ› Bug report
Status

Needs work

Version

10.1 ✨

Component
ViewsΒ  β†’

Last updated 4 minutes ago

Created by

πŸ‡¨πŸ‡³China caohan@ciandt.com

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

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.

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

    This still needs a test case

    Also would be helpful to have steps to reproduce this issue.

  • πŸ‡ΊπŸ‡ΈUnited States mortona2k Seattle

    I added steps to create a view that triggers the error and is fixed with this patch.

  • πŸ‡ΊπŸ‡ΈUnited States mortona2k Seattle

    There are two very different approaches in the patches here.

    #11 has:

    +    if (is_string($value)) {
    +      if (strtotime($value) !== FALSE) {
    +        $value = date("Y", strtotime($value));
    +      }
    +    }
    

    This will convert date strings to year.

    #18 has:

    +    if (is_string($value)) {
    +      $value = (float) $value;
    +    }
    

    This will convert a date string to to a number like 2025.0.

    A patch in #3338895 has:

    +    $value = round((float) $value, $precision);
    

    Basically the same as #18, without the check for a string.

    I bundled them all together and came up with this:

        if (is_string($value)) {
          if (strtotime($value) !== FALSE) {
            $value = date("U", strtotime($value));
          }
        }
    
        $value = round((float) $value, $precision);
    

    Now a date string is converted to a timestamp. However, I'm not sure if this does anything for the max comparison, or if this just rendering the value afterwards.

Production build 0.71.5 2024