ImageAltText field widget action fails in media library add form
>>> [!note] Migrated issue <!-- Drupal.org comment --> <!-- Migrated from issue #3574811. --> Reported by: [petar_basic](https://www.drupal.org/user/3626336) Related to !1369 !1231 >>> <p>[Tracker]<br> <strong>Update Summary: </strong>[One-line status update for stakeholders]<br> <strong>Short Description: </strong>ImageAltText automator crashes with TypeError in media library add form due to hardcoded form structure assumptions<br> <strong>Check-in Date: </strong>02/20/2026<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> <p>The <code>automator_alt_text</code> field widget action works on standalone entity forms (e.g. <code>/media/add/image</code>) but throws two TypeErrors when used in the<br> media library add form (e.g. when adding an image through a media reference field on a node).</p> <p> <strong>Error 1: TypeError on <code>$key</code> parameter</strong></p> <p> <code>ImageAltText::aiAutomatorsAjax()</code> hardcodes <code>$array_parents[2]</code> as the delta and <code>$array_parents[0]</code> as the field name, assuming the<br> form structure is always <code>[field_name, 'widget', delta, ...]</code>. In the media library, the form nests fields under <code>media/0/fields/...</code>, so<br> <code>$array_parents[2]</code> is the string <code>'fields'</code> instead of an integer delta.</p> <pre> TypeError: AutomatorBaseAction::populateAutomatorValues(): Argument #4 ($key) must be of type ?int, string given </pre><p> <strong>Error 2: <code>buildEntity()</code> returns null</strong></p> <p> <code>FieldWidgetActionBase::buildEntity()</code> checks <code>$form_state-&gt;getFormObject() instanceof ContentEntityFormInterface</code>. The media library form (<code>AddFormBase</code>) extends <code>FormBase</code>, not <code>ContentEntityForm</code>, so <code>buildEntity()</code> returns null. The media entities are stored in <code>$form_state-&gt;get('media')</code> instead.</p> <pre> TypeError: AiAutomatorEntityModifier::saveEntity(): Argument #1 ($entity) must be of type EntityInterface, null given </pre><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.<br> 1. Enable <code>ai_automators</code> and <code>field_widget_actions</code><br> 2. Configure the <code>automator_alt_text</code> action on the <code>field_image</code> widget for media type <code>image</code> on the <code>media_library</code> form display<br> 3. Edit a node with a media reference field (e.g. an image paragraph)<br> 4. Open the media library modal and upload a new image<br> 5. Click "Generate Alt Text"<br> 6. Observe the TypeError</p> <p>Tested with Drupal core 11.3.x, AI module 2.0.x, Mistral provider (pixtral-large-latest).</p> <h3 id="summary-proposed-resolution">Proposed resolution</h3> <p>Two changes in <code>ImageAltText::aiAutomatorsAjax()</code>:</p> <p> 1. <strong>Fix form structure detection:</strong> Search for <code>'widget'</code> in <code>$array_parents</code> to derive the field name (element before <code>'widget'</code>) and delta (element after <code>'widget'</code>), regardless of form nesting depth.</p> <p> 2. <strong>Add media library support:</strong> New <code>populateMediaLibraryValues()</code> method that:<br> - Gets the media entity from <code>$form_state-&gt;get('media')[$delta]</code><br> - Extracts form values using <code>EntityFormDisplay::collectRenderDisplay()</code><br> - Runs the automator on the entity<br> - Updates the form element at the correct nested path<br> - Guards the <code>AddFormBase</code> check with <code>class_exists()</code> since <code>media_library</code> is an optional core module</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> <p>No UI, API, or data model changes. The fix is backwards-compatible &mdash; regular entity forms continue to work as before.</p> <h3 id="summary-ai-usage">AI usage (if applicable)</h3> <p>[x] AI Assisted Issue<br> This issue was generated with AI assistance, but was reviewed and refined by the creator.</p> <p>[x] AI Assisted Code<br> This code was mainly generated by a human, with AI autocompleting or parts AI generated, but under full human supervision.</p> <p>[ ] AI Generated Code<br> This code was mainly generated by an AI with human guidance, and reviewed, tested, and refined by a human.</p> <p>[ ] Vibe Coded<br> This code was generated by an AI and has only been functionally tested.</p>
issue