Problem/Motivation

In version 2.1.0, declare(strict_types=1); was added to multiple files which has caused TypeErrors when using Date AP Style formatters in Views and field displays.
With strict type declarations enabled, PHP no longer performs implicit type conversions. This causes problems:

  1. Timestamp values as strings: When datetime field values (particularly created, changed, timestamp field types) are loaded from the database or passed through Views, they come through as strings. However, ApStyleDateFormatter::formatTimestamp() requires an int parameter, resulting in:
    TypeError: Drupal\date_ap_style\ApStyleDateFormatter::formatTimestamp(): 
       Argument #1 ($timestamp) must be of type int, string given
  2. Boolean options as integers: When setting an Formatter option in the view to always display as year this is saves as a 0/1 instead of an implicate Boolean True/False.`ApStyleDateFormatter::formatYear()` expect parameters, resulting in: `always_display_year``?bool`
    TypeError: Drupal\date_ap_style\ApStyleDateFormatter::formatYear(): 
       Argument #3 ($always_display_year) must be of type ?bool, int given
    

Steps to reproduce

  1. Create a View of Nodes
  2. Add the Node "Created" field (or any timestamp/created/changed field)
  3. Set the field formatter to "AP Style"
  4. Configure the formatter with any boolean options (e.g., "Always display year")
  5. View the page - a TypeError will be thrown

Proposed resolution

Remove `declare(strict_types=1);` or go though the code and add implicit types when retrieving data.

Remaining tasks

User interface changes

API changes

Data model changes

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

emerham created an issue. See original summary.

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

trackleft2’s picture

Status: Active » Needs review
trackleft2’s picture

Hi there, thank you for creating this issue. I've added a merge request that resolves the TypeError issues caused by strict type declarations.

Changes made:

1. Type casting for timestamps: Added explicit (int) casts in ApStyleDateFieldFormatter and ApStyleDateRangeFieldFormatter when passing timestamp values to formatTimestamp(), since these values come from the database as strings but the method requires integers.

2. Type casting for boolean options: Added explicit (bool) casts in ApSelectFormatterBase when processing form settings, since checkbox values are stored as 0/1 integers but the formatter methods expect boolean types.

Testing:

I've added PHPUnit kernel tests (FieldFormatterStrictTypesTest) that cover:
- Timestamp fields with string values
- Datetime fields with integer boolean settings
- Date range fields with string timestamps
- Views integration with integer boolean settings (includes a fixture that reproduces the original TypeError)

All tests fail without the fix and pass with it, confirming the issue is resolved. The changes are minimal, backward-compatible, and don't alter any public APIs.

  • trackleft2 committed 9c0cd09c on 3.0.x
    bugfix: #3556754 Declaration of Script Types causes type error in...

  • trackleft2 authored 160c435a on 2.1.x
    bugfix: #3556754 Declaration of Script Types causes type error in...

  • 64b6a17f committed on 2.1.x
    bugfix: #3556754 Remove return type from viewElements() for...
trackleft2’s picture

Status: Needs review » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

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