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-&gt;calculateTextFormat() (Line: 255)<br> Drupal\ai_automators\PluginBaseClasses\EntityReference-&gt;storeValues() (Line: 86)<br> Drupal\ai_automators\AiAutomatorRuleRunner-&gt;generateResponse() (Line: 70)<br> Drupal\ai_automators\Plugin\AiAutomatorProcess\DirectSaveProcessing-&gt;modify() (Line: 229)<br> Drupal\ai_automators\AiAutomatorEntityModifier-&gt;markFieldForProcessing() (Line: 129)<br> Drupal\ai_automators\AiAutomatorEntityModifier-&gt;saveEntity() (Line: 31)<br> ai_automators_entity_presave()<br> call_user_func_array() (Line: 355)<br> Drupal\Core\Extension\ModuleHandler-&gt;Drupal\Core\Extension\{closure}() (Line: 307)<br> Drupal\Core\Extension\ModuleHandler-&gt;invokeAllWith() (Line: 354)<br> Drupal\Core\Extension\ModuleHandler-&gt;invokeAll() (Line: 217)<br> Drupal\Core\Entity\EntityStorageBase-&gt;invokeHook() (Line: 900)<br> Drupal\Core\Entity\ContentEntityStorageBase-&gt;invokeHook() (Line: 529)<br> Drupal\Core\Entity\EntityStorageBase-&gt;doPreSave() (Line: 753)<br> Drupal\Core\Entity\ContentEntityStorageBase-&gt;doPreSave() (Line: 483)<br> Drupal\Core\Entity\EntityStorageBase-&gt;save() (Line: 805)<br> Drupal\Core\Entity\Sql\SqlContentEntityStorage-&gt;save() (Line: 354)<br> Drupal\Core\Entity\EntityBase-&gt;save() (Line: 281)<br> Drupal\node\NodeForm-&gt;save()<br> call_user_func_array() (Line: 105)<br> Drupal\Core\Form\FormSubmitter-&gt;executeSubmitHandlers() (Line: 43)<br> Drupal\Core\Form\FormSubmitter-&gt;doSubmitForm() (Line: 589)<br> Drupal\Core\Form\FormBuilder-&gt;processForm() (Line: 144)<br> Drupal\autosave_form\Form\AutosaveFormBuilder-&gt;processForm() (Line: 321)<br> Drupal\Core\Form\FormBuilder-&gt;buildForm() (Line: 97)<br> Drupal\autosave_form\Form\AutosaveFormBuilder-&gt;buildForm() (Line: 73)<br> Drupal\Core\Controller\FormController-&gt;getContentResult() (Line: 39)<br> Drupal\layout_builder\Controller\LayoutBuilderHtmlEntityFormController-&gt;getContentResult()<br> call_user_func_array() (Line: 123)<br> Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber-&gt;Drupal\Core\EventSubscriber\{closure}() (Line: 593)<br> Drupal\Core\Render\Renderer-&gt;executeInRenderContext() (Line: 121)<br> Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber-&gt;wrapControllerExecutionInRenderContext() (Line: 97)<br> Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber-&gt;Drupal\Core\EventSubscriber\{closure}() (Line: 183)<br> Symfony\Component\HttpKernel\HttpKernel-&gt;handleRaw() (Line: 76)<br> Symfony\Component\HttpKernel\HttpKernel-&gt;handle() (Line: 53)<br> Drupal\Core\StackMiddleware\Session-&gt;handle() (Line: 48)<br> Drupal\Core\StackMiddleware\KernelPreHandle-&gt;handle() (Line: 28)<br> Drupal\Core\StackMiddleware\ContentLength-&gt;handle() (Line: 32)<br> Drupal\big_pipe\StackMiddleware\ContentLength-&gt;handle() (Line: 116)<br> Drupal\page_cache\StackMiddleware\PageCache-&gt;pass() (Line: 90)<br> Drupal\page_cache\StackMiddleware\PageCache-&gt;handle() (Line: 48)<br> Drupal\Core\StackMiddleware\ReverseProxyMiddleware-&gt;handle() (Line: 51)<br> Drupal\Core\StackMiddleware\NegotiationMiddleware-&gt;handle() (Line: 36)<br> Drupal\Core\StackMiddleware\AjaxPageState-&gt;handle() (Line: 51)<br> Drupal\Core\StackMiddleware\StackedHttpKernel-&gt;handle() (Line: 709)<br> Drupal\Core\DrupalKernel-&gt;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