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

  1. Use Simplenews 4.1.1 on Drupal 11 with PHP 8+
  2. Trigger a code path where IssueItem::setValue() is called with a scalar value (for example during entity save or form submission)
  3. 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

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

lrubiohh created an issue. See original summary.

lrubiohh’s picture

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

lrubiohh’s picture

Status: Active » Needs review
liam morland’s picture

Issue tags: -php8 +PHP 8.0

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

  • adamps committed 2a0666ab on 4.x authored by lrubiohh
    fix: #3569818 TypeError: count() called on string in IssueItem FieldType...
adamps’s picture

Status: Needs review » Fixed
Issue tags: -drupal11, -bug

Thanks

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.