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:
- 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 -
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
- Create a View of Nodes
- Add the Node "Created" field (or any timestamp/created/changed field)
- Set the field formatter to "AP Style"
- Configure the formatter with any boolean options (e.g., "Always display year")
- 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
Issue fork date_ap_style-3556754
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
Comment #4
trackleft2Comment #5
trackleft2Hi 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 inApStyleDateFieldFormatterandApStyleDateRangeFieldFormatterwhen passing timestamp values toformatTimestamp(), since these values come from the database as strings but the method requires integers.2. Type casting for boolean options: Added explicit
(bool)casts inApSelectFormatterBasewhen 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.
Comment #9
trackleft2