InvalidArgumentException in AutoCompleteTagsTaxonomy when form has empty entity reference select fields
>>> [!note] Migrated issue <!-- Drupal.org comment --> <!-- Migrated from issue #3560145. --> Reported by: [nicholass](https://www.drupal.org/user/3487851) Related to !1028 !1027 >>> <p>[Tracker]<br> <strong>Update Summary: </strong>[One-line status update for stakeholders]<br> <strong>Short Description: </strong>[One-line issue summary for stakeholders]<br> <strong>Check-in Date: </strong>MM/DD/YYYY<br> <em>Metadata is used by the <a href="https://www.drupalstarforge.ai/" title="AI Tracker">AI Tracker.</a> Docs and additional fields <a href="https://www.drupalstarforge.ai/ai-dashboard/docs" title="AI Issue Tracker Documentation">here</a>.</em><br> [/Tracker]</p> <h3 id="summary-problem-motivation">Problem/Motivation</h3> <pre>InvalidArgumentException: Value is not a valid entity. in Drupal\Core\Entity\Plugin\DataType\EntityReference-&gt;setValue() (line 108 of /var/www/html/docroot/core/lib/Drupal/Core/Entity/Plugin/DataType/EntityReference.php).</pre><p>On a content type with multiple entity ref fields, some of which using various formatters (autocomplete, choose, select list). When using the AI Automators module with an entity reference autocomplete tags field (e.g., taxonomy tags), clicking the automator button to generate tags causes an InvalidArgumentException: Value is not a valid entity error due to other entity fields existing in the node edit form.</p> <p>The root cause is that when buildEntity() is called during the ajax callback, other entity reference fields on the same form (such as select lists or Chosen widgets) may contain TranslatableMarkup objects as their value. This happens when those fields have the "- None -" (_none) option selected. Drupal's entity reference field type cannot process these TranslatableMarkup objects and throws an exception.</p> <p>Enable the ai_automators module along with field_widget_actions<br> Configure an AI provider (any provider should reproduce this)<br> Create a content type with:<br> An entity reference autocomplete tags field (e.g., taxonomy terms)<br> Another entity reference field using a select widget or Chosen widget<br> Configure an AI Automator for the autocomplete tags field<br> Enable the automator widget action on the field widget settings<br> Create/edit content of that type<br> Leave the select/Chosen entity reference field empty (showing "- None -")<br> Click the AI Automator button on the autocomplete tags field<br> Result: InvalidArgumentException: Value is not a valid entity in EntityReference-&gt;setValue()</p> <h4 id="summary-steps-reproduce">Steps to reproduce (required for bugs, but not feature requests)</h4> <p>Please provide information like AI modules enabled, which AI provider, browser, etc.</p> <h3 id="summary-proposed-resolution">Proposed resolution</h3> <p>Override the buildEntity() method in AutoCompleteTagsTaxonomy to sanitize all entity reference field values in the form state before calling the parent's buildEntity().</p> <p>The sanitization:</p> <p>Detects TranslatableMarkup objects (from empty "_none" select options) and replaces them with empty arrays<br> Filters out invalid values from multi-value entity reference fields<br> Only processes fields that actually contain invalid values to minimize overhead</p> <h3 id="summary-remaining-tasks">Remaining tasks</h3> <h3>Optional: Other details as applicable (e.g., User interface changes, API changes, Data model changes)</h3>
issue