TypeError: count(): Argument #1 ($value) must be of type Countable|array, null given in count() (line 519 of modules/contrib/ai/modules/ai_automators/src/Rulehelpers/GeneralHelper.php).
>>> [!note] Migrated issue
<!-- Drupal.org comment -->
<!-- Migrated from issue #3504568. -->
Reported by: [itamair](https://www.drupal.org/user/1179076)
Related to !458
>>>
<h3 id="summary-problem-motivation">Problem/Motivation</h3>
<p>I have setup a "Geoplace Type" Entity/Taxonomy Reference type field, with AI Automator enabled, and some settings on that ...<br>
I am trying to make that field automatically populated with a Taxonomy term (existing or being created at runtime as new) on there basis of the analysis of an image referenced / uploaded into a Media Image field type on the same Content entity (being edited).</p>
<p>May be my AI Automator settings on this "Geoplace Type" Entity/Taxonomy Reference field are not correct (not the most appropriate) as I am still learning and testing with all this AI Drupal module (BTW super hyper mega thanks for all this ... ).<br>
Whatever could be, when I try to Save the Content the Drupal CMS instance breaks with the following error ... </p>
<blockquote><p>The website encountered an unexpected error. Try again later.</p>
<p>TypeError: count(): Argument #1 ($value) must be of type Countable|array, null given in count() (line 519 of modules/contrib/ai/modules/ai_automators/src/Rulehelpers/GeneralHelper.php).<br>
Drupal\ai_automators\Rulehelpers\GeneralHelper->calculateTextFormat() (Line: 255)<br>
Drupal\ai_automators\PluginBaseClasses\EntityReference->storeValues() (Line: 86)<br>
Drupal\ai_automators\AiAutomatorRuleRunner->generateResponse() (Line: 70)<br>
Drupal\ai_automators\Plugin\AiAutomatorProcess\DirectSaveProcessing->modify() (Line: 229)<br>
Drupal\ai_automators\AiAutomatorEntityModifier->markFieldForProcessing() (Line: 129)<br>
Drupal\ai_automators\AiAutomatorEntityModifier->saveEntity() (Line: 31)<br>
ai_automators_entity_presave()<br>
call_user_func_array() (Line: 355)<br>
Drupal\Core\Extension\ModuleHandler->Drupal\Core\Extension\{closure}() (Line: 307)<br>
Drupal\Core\Extension\ModuleHandler->invokeAllWith() (Line: 354)<br>
Drupal\Core\Extension\ModuleHandler->invokeAll() (Line: 217)<br>
Drupal\Core\Entity\EntityStorageBase->invokeHook() (Line: 900)<br>
Drupal\Core\Entity\ContentEntityStorageBase->invokeHook() (Line: 529)<br>
Drupal\Core\Entity\EntityStorageBase->doPreSave() (Line: 753)<br>
Drupal\Core\Entity\ContentEntityStorageBase->doPreSave() (Line: 483)<br>
Drupal\Core\Entity\EntityStorageBase->save() (Line: 805)<br>
Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (Line: 354)<br>
Drupal\Core\Entity\EntityBase->save() (Line: 281)<br>
Drupal\node\NodeForm->save()<br>
call_user_func_array() (Line: 105)<br>
Drupal\Core\Form\FormSubmitter->executeSubmitHandlers() (Line: 43)<br>
Drupal\Core\Form\FormSubmitter->doSubmitForm() (Line: 589)<br>
Drupal\Core\Form\FormBuilder->processForm() (Line: 144)<br>
Drupal\autosave_form\Form\AutosaveFormBuilder->processForm() (Line: 321)<br>
Drupal\Core\Form\FormBuilder->buildForm() (Line: 97)<br>
Drupal\autosave_form\Form\AutosaveFormBuilder->buildForm() (Line: 73)<br>
Drupal\Core\Controller\FormController->getContentResult() (Line: 39)<br>
Drupal\layout_builder\Controller\LayoutBuilderHtmlEntityFormController->getContentResult()<br>
call_user_func_array() (Line: 123)<br>
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 593)<br>
Drupal\Core\Render\Renderer->executeInRenderContext() (Line: 121)<br>
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext() (Line: 97)<br>
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 183)<br>
Symfony\Component\HttpKernel\HttpKernel->handleRaw() (Line: 76)<br>
Symfony\Component\HttpKernel\HttpKernel->handle() (Line: 53)<br>
Drupal\Core\StackMiddleware\Session->handle() (Line: 48)<br>
Drupal\Core\StackMiddleware\KernelPreHandle->handle() (Line: 28)<br>
Drupal\Core\StackMiddleware\ContentLength->handle() (Line: 32)<br>
Drupal\big_pipe\StackMiddleware\ContentLength->handle() (Line: 116)<br>
Drupal\page_cache\StackMiddleware\PageCache->pass() (Line: 90)<br>
Drupal\page_cache\StackMiddleware\PageCache->handle() (Line: 48)<br>
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle() (Line: 51)<br>
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle() (Line: 36)<br>
Drupal\Core\StackMiddleware\AjaxPageState->handle() (Line: 51)<br>
Drupal\Core\StackMiddleware\StackedHttpKernel->handle() (Line: 709)<br>
Drupal\Core\DrupalKernel->handle() (Line: 19)</p></blockquote>
<p>May be some weird use case should be taken onto account ($formatsAllowed not defined?) and more defensive check might/should be added here (?):<br>
<a href="https://git.drupalcode.org/project/ai/-/blob/1.0.x/modules/ai_automators/src/Rulehelpers/GeneralHelper.php?ref_type=heads#L519">https://git.drupalcode.org/project/ai/-/blob/1.0.x/modules/ai_automators/src/Rulehelpers/GeneralHelper.php?ref_type=heads#L519</a></p>
issue