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.
| Comment | File | Size | Author |
|---|---|---|---|
| #4 | diff-countable-3329887-4.patch | 803 bytes | bala.addweb |
| #2 | countable-3329887-2.patch | 800 bytes | sah62 |
Issue fork diff-3329887
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:
- 3329887-typeerror-count-argument
compare
- 8.x-1.x
changes, plain diff MR !74
Comments
Comment #2
sah62 commentedProposed patch attached.
Comment #3
berdirVariable name should be lowercase. My guess is this happens when a revision page shows no revisions at all.
Comment #4
bala.addweb commenteddiff-countable-3329887.patch
Comment #5
sah62 commentedThe patches are working for me. Let's get this reviewed.
Comment #6
silvi.addweb commentedPatch is working fine for me
Comment #7
acbramley commentedNeeds tests and an MR. Patches are no longer accepted in this project.
Comment #9
kazuko.murata commentedI 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.
Comment #12
acbramley commentedAgreed 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.