Problem/Motivation
With PHP 8.0+ (tested on PHP 8.3) and Drupal 11.2.8, Simplenews 4.1.1 throws a fatal TypeError in the IssueItem FieldType.
The error occurs in IssueItem::setValue() when calling count() on the $values argument, which in some cases is passed as a scalar (string) instead of an array.
PHP 8 no longer allows calling count() on non-countable values, so this results in a fatal error.
ERROR
TypeError: count(): Argument #1 ($value) must be of type Countable|array, string given
in Drupal\simplenews\Plugin\Field\FieldType\IssueItem::setValue()
(src/Plugin/Field/FieldType/IssueItem.php:119)
Steps to reproduce
- Use Simplenews 4.1.1 on Drupal 11 with PHP 8+
- Trigger a code path where IssueItem::setValue() is called with a scalar value (for example during entity save or form submission)
- A fatal TypeError is thrown
Proposed resolution
Ensure that count() is only called when $values is an array.
The attached patch adds a simple type check before calling count(), preserving the existing behavior while preventing the PHP 8 TypeError.
Remaining tasks
Review and commit the attached patch.
Issue fork simplenews-3569818
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 #2
lrubiohh commentedThis issue occurs consistently on Drupal 11 with PHP 8.3.
The problem is that IssueItem::setValue() calls count() on $values without
checking its type. In some code paths Drupal passes a scalar value, which
causes a TypeError in PHP 8+.
A minimal fix is to ensure count() is only called when $values is an array.
Comment #4
lrubiohh commentedComment #5
liam morlandComment #8
adamps commentedThanks