TypeError: count(): Argument #1 ($value) must be of type Countable|array, string given

Created on 27 December 2022, almost 2 years ago
Updated 20 August 2024, 3 months ago

Problem/Motivation

I recently noticed this error in my Watchdog log:

TypeError: count(): Argument #1 ($value) must be of type Countable|array, string given in count() (line 372 of /var/www/mysite/web/modules/contrib/diff/src/Form/RevisionOverviewForm.php) #0 /var/www/mysite/web/modules/contrib/diff/src/Form/RevisionOverviewForm.php(372): count()
#1 [internal function]: Drupal\diff\Form\RevisionOverviewForm->validateForm()
#2 /var/www/mysite/web/core/lib/Drupal/Core/Form/FormValidator.php(82): call_user_func_array()
#3 /var/www/mysite/web/core/lib/Drupal/Core/Form/FormValidator.php(275): Drupal\Core\Form\FormValidator->executeValidateHandlers()
#4 /var/www/mysite/web/core/lib/Drupal/Core/Form/FormValidator.php(118): Drupal\Core\Form\FormValidator->doValidateForm()
#5 /var/www/mysite/web/core/lib/Drupal/Core/Form/FormBuilder.php(591): Drupal\Core\Form\FormValidator->validateForm()
#6 /var/www/mysite/web/core/lib/Drupal/Core/Form/FormBuilder.php(323): Drupal\Core\Form\FormBuilder->processForm()
#7 /var/www/mysite/web/core/lib/Drupal/Core/Form/FormBuilder.php(222): Drupal\Core\Form\FormBuilder->buildForm()
#8 /var/www/mysite/web/modules/contrib/diff/src/Controller/NodeRevisionController.php(25): Drupal\Core\Form\FormBuilder->getForm()
#9 [internal function]: Drupal\diff\Controller\NodeRevisionController->revisionOverview()
#10 /var/www/mysite/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(123): call_user_func_array()
#11 /var/www/mysite/web/core/lib/Drupal/Core/Render/Renderer.php(580): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
#12 /var/www/mysite/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(124): Drupal\Core\Render\Renderer->executeInRenderContext()
#13 /var/www/mysite/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(97): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext()
#14 /var/www/mysite/vendor/symfony/http-kernel/HttpKernel.php(169): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
#15 /var/www/mysite/vendor/symfony/http-kernel/HttpKernel.php(81): Symfony\Component\HttpKernel\HttpKernel->handleRaw()
#16 /var/www/mysite/web/core/lib/Drupal/Core/StackMiddleware/Session.php(58): Symfony\Component\HttpKernel\HttpKernel->handle()
#17 /var/www/mysite/web/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(48): Drupal\Core\StackMiddleware\Session->handle()
#18 /var/www/mysite/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(106): Drupal\Core\StackMiddleware\KernelPreHandle->handle()
#19 /var/www/mysite/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(85): Drupal\page_cache\StackMiddleware\PageCache->pass()
#20 /var/www/mysite/web/core/modules/ban/src/BanMiddleware.php(50): Drupal\page_cache\StackMiddleware\PageCache->handle()
#21 /var/www/mysite/web/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(48): Drupal\ban\BanMiddleware->handle()
#22 /var/www/mysite/web/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(51): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle()
#23 /var/www/mysite/vendor/stack/builder/src/Stack/StackedHttpKernel.php(23): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle()
#24 /var/www/mysite/web/core/lib/Drupal/Core/DrupalKernel.php(707): Stack\StackedHttpKernel->handle()
#25 /var/www/mysite/web/index.php(19): Drupal\Core\DrupalKernel->handle()
#26 {main}.

I'm using PHP 8.1.2 with Drupal 9.5.0. The problem here is that $form_state->getValue('node_revisions_table') returns a value of type string, which isn't a valid input to the PHP count() function.

Steps to reproduce

I haven't been able to reproduce the error manually.

Proposed resolution

Change this line:

if (count($form_state->getValue('node_revisions_table')) <= 1) {

to this:

$nodeRevisions = $form_state->getValue('node_revisions_table');
if (is_countable($nodeRevisions) && count($nodeRevisions) <= 1) {

I'll attach a patch in a moment.

๐Ÿ› Bug report
Status

Fixed

Version

1.0

Component

Code

Created by

๐Ÿ‡บ๐Ÿ‡ธUnited States sah62 US

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024