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.

Issue fork diff-3329887

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

sah62 created an issue. See original summary.

sah62’s picture

StatusFileSize
new800 bytes

Proposed patch attached.

berdir’s picture

Status: Active » Needs work

Variable name should be lowercase. My guess is this happens when a revision page shows no revisions at all.

bala.addweb’s picture

StatusFileSize
new803 bytes

diff-countable-3329887.patch

sah62’s picture

Status: Needs work » Needs review

The patches are working for me. Let's get this reviewed.

silvi.addweb’s picture

Status: Needs review » Reviewed & tested by the community

Patch is working fine for me

acbramley’s picture

Version: 8.x-1.1 » 8.x-1.x-dev
Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests

Needs tests and an MR. Patches are no longer accepted in this project.

kazuko.murata’s picture

Status: Needs work » Needs review

I tried writing steps to reproduce the issue and adding code for automated testing, but I realized that this fix might not be necessary.

Two conditions are required on the revision page to reproduce this error:

・No revisions are displayed.
・The "Compare selected revisions" button is displayed, and it can be clicked.

However, the revision page only displays the "Compare selected revisions" button when there is one or more revisions. You can reproduce the error by creating a submit button using the browser's development tools and executing the submit, but I think this modification is unnecessary because the display and hiding of the "Compare selected revisions" button are controlled correctly.

It would be better to leave it up to the maintainers to decide whether this modification is necessary.

Thank you.

  • acbramley committed 7b283095 on 8.x-1.x
    Issue #3329887: TypeError: count(): Argument #1 ($value) must be of type...

  • acbramley committed 6fb0c9e1 on 2.x
    Issue #3329887: TypeError: count(): Argument #1 ($value) must be of type...
acbramley’s picture

Status: Needs review » Fixed
Issue tags: -Needs tests

Agreed with #9 that this is realistically not a bug but I've committed something to stop the error. The solutions in the patch were not correct.

Status: Fixed » Closed (fixed)

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