Problem

Brief:
TypeError: count(): Argument #1 ($value) must be of type Countable|array, null given in count() (line 427 of /htdocs/drupal-project/modules/contrib/entity_embed/src/Form/EntityEmbedDialog.php) #0 /htdocs/drupal-project/modules/contrib/entity_embed/src/Form/EntityEmbedDialog.php(427): count(NULL)\n#1

Full Error:
[Wed, 07/13/2022 - 15:18 EDT] [Error] [php] [client: ::1, admin] TypeError: count(): Argument #1 ($value) must be of type Countable|array, null given in count() (line 427 of /htdocs/drupal-project/modules/contrib/entity_embed/src/Form/EntityEmbedDialog.php) #0 /htdocs/drupal-project/modules/contrib/entity_embed/src/Form/EntityEmbedDialog.php(427): count(NULL)\n#1 /htdocs/drupal-project/modules/contrib/entity_embed/src/Form/EntityEmbedDialog.php(224): Drupal\entity_embed\Form\EntityEmbedDialog->buildEmbedStep(Array, Object(Drupal\Core\Form\FormState))\n#2 [internal function]: Drupal\entity_embed\Form\EntityEmbedDialog->buildForm(Array, Object(Drupal\Core\Form\FormState), Object(Drupal\editor\Entity\Editor), Object(Drupal\embed\Entity\EmbedButton))\n#3 /htdocs/drupal-project/core/lib/Drupal/Core/Form/FormBuilder.php(531): call_user_func_array(Array, Array)\n#4 /htdocs/drupal-project/core/lib/Drupal/Core/Form/FormBuilder.php(370): Drupal\Core\Form\FormBuilder->retrieveForm('entity_embed_di...', Object(Drupal\Core\Form\FormState))\n#5 /htdocs/drupal-project/modules/contrib/entity_embed/src/Form/EntityEmbedDialog.php(757): Drupal\Core\Form\FormBuilder->rebuildForm('entity_embed_di...', Object(Drupal\Core\Form\FormState), Array)\n#6 [internal function]: Drupal\entity_embed\Form\EntityEmbedDialog->submitSelectStep(Array, Object(Drupal\Core\Form\FormState), Object(Symfony\Component\HttpFoundation\Request))\n#7 /htdocs/drupal-project/core/lib/Drupal/Core/Form/FormAjaxResponseBuilder.php(69): call_user_func_array(Array, Array)\n#8 /htdocs/drupal-project/core/lib/Drupal/Core/Form/EventSubscriber/FormAjaxSubscriber.php(109): Drupal\Core\Form\FormAjaxResponseBuilder->buildResponse(Object(Symfony\Component\HttpFoundation\Request), Array, Object(Drupal\Core\Form\FormState), Array)\n#9 [internal function]: Drupal\Core\Form\EventSubscriber\FormAjaxSubscriber->onException(Object(Symfony\Component\HttpKernel\Event\ExceptionEvent), 'kernel.exceptio...', Object(Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher))\n#10 /htdocs/drupal-project/core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php(142): call_user_func(Array, Object(Symfony\Component\HttpKernel\Event\ExceptionEvent), 'kernel.exceptio...', Object(Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher))\n#11 /htdocs/drupal-project/vendor/symfony/http-kernel/HttpKernel.php(219): Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch(Object(Symfony\Component\HttpKernel\Event\ExceptionEvent), 'kernel.exceptio...')\n#12 /htdocs/drupal-project/vendor/symfony/http-kernel/HttpKernel.php(91): Symfony\Component\HttpKernel\HttpKernel->handleThrowable(Object(Drupal\Core\Form\FormAjaxException), Object(Symfony\Component\HttpFoundation\Request), 1)\n#13 /htdocs/drupal-project/modules/contrib/force_password_change/src/Service/ForcePasswordChangeRedirectMiddleware.php(42): Symfony\Component\HttpKernel\HttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)\n#14 /htdocs/drupal-project/modules/contrib/simple_oauth/src/HttpMiddleware/BasicAuthSwap.php(67): Drupal\force_password_change\Service\ForcePasswordChangeRedirectMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)\n#15 /htdocs/drupal-project/core/lib/Drupal/Core/StackMiddleware/Session.php(58): Drupal\simple_oauth\HttpMiddleware\BasicAuthSwap->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)\n#16 /htdocs/drupal-project/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(48): Drupal\Core\StackMiddleware\Session->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)\n#17 /htdocs/drupal-project/core/modules/page_cache/src/StackMiddleware/PageCache.php(106): Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)\n#18 /htdocs/drupal-project/core/modules/page_cache/src/StackMiddleware/PageCache.php(85): Drupal\page_cache\StackMiddleware\PageCache->pass(Object(Symfony\Component\HttpFoundation\Request), 1, true)\n#19 /htdocs/drupal-project/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(48): Drupal\page_cache\StackMiddleware\PageCache->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)\n#20 /htdocs/drupal-project/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(51): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)\n#21 /htdocs/drupal-project/vendor/stack/builder/src/Stack/StackedHttpKernel.php(23): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)\n#22 /htdocs/drupal-project/core/lib/Drupal/Core/DrupalKernel.php(709): Stack\StackedHttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)\n#23 /htdocs/drupal-project/index.php(19): Drupal\Core\DrupalKernel->handle(Object(Symfony\Component\HttpFoundation\Request))\n#24 {main}.

PHP 8.1.0
entity_embed version "^1.2"
core version 9.4.1
db postgres

Triggered when selecting an item to embed

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

jakegibs617 created an issue. See original summary.

jakegibs617’s picture

seems like we just need to add count((array)

modules/contrib/entity_embed/src/Form/EntityEmbedDialog.php
- if (count( $entity_element['data-entity-ids']) > 0) {
+ if (count((array) $entity_element['data-entity-ids']) > 0) {

jakegibs617’s picture

wxman’s picture

I've been having the same problem. Had to switch PHP back to 8.0.x

dave reid’s picture

Version: 8.x-1.2 » 8.x-1.x-dev
Status: Active » Needs work

Can we please target the specific instance of count() that is causing the error? This just blindly adds array casting to things that definitely do not need them.

jakegibs617’s picture

Fair point @Dave Reid This isolates it to what fixed it for me

anybody’s picture

anybody’s picture

Wouldn't a simple !is_empty($entity_element['data-entity-ids']) check do better here?
It doesn't require the casting and is faster.

anybody’s picture

Status: Needs work » Needs review

I prepared a MR based on my suggestion in #8.

Balu Ertl made their first commit to this issue’s fork.

baluertl’s picture

Status: Needs review » Needs work

Cannot reproduce on Core 9.5.11 + PHP 8.1.28 + entity_embed 8.x-1.x-dev: inserting a node into another one through WYSIWYG works as expected. As the suggested fix is beneficial for better stability, I would mark this ticket RTBC but tests are failing on the MR, so let's wait them getting into HEAD, then get back to here again.

baluertl’s picture

Status: Needs work » Postponed
Related issues: +#3415080: Fix functional javascript tests for entity_embed / ckeditor4