ImageAltText: "Edit when changed" doesn't overwrite existing alt text
>>> [!note] Migrated issue
<!-- Drupal.org comment -->
<!-- Migrated from issue #3571573. -->
Reported by: [alexortega_98](https://www.drupal.org/user/3711404)
Related to !1196
>>>
<p> [Tracker]<br>
<strong>Update Summary: </strong>Reframed as bug fix — use existing edit_mode instead of new override_value config<br>
<strong>Short Description: </strong>ImageAltText "Edit when changed" doesn't overwrite existing alt text<br>
<strong>Check-in Date: </strong>03/05/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>When "Edit when changed" (<code>edit_mode</code>) is enabled for an image alt text automator and the base field changes, the automator runs and calls the LLM, but the generated alt text is silently discarded for images that already have alt text. Two bugs prevent <code>edit_mode</code> from working end-to-end in<br>
this plugin:</p>
<ol>
<li><strong><code>checkIfEmpty()</code> returns an incompatible format.</strong> It returns an associative array <code>['alt' =&gt; '...']</code>, but <code>baseShouldSave()</code> checks <code>$value[0]</code> (numeric index). Since <code>$value[0]</code> doesn't exist, the edit_mode gate is bypassed entirely — the<br>
automator runs unconditionally, wasting LLM calls even when it shouldn't.</li>
<li><strong><code>storeValues()</code> ignores <code>edit_mode</code>.</strong> It unconditionally skips any image that already has alt text (line 88), regardless of whether the automator was deliberately triggered by edit_mode. The LLM result is generated and then thrown away.</li>
</ol>
<p><strong>Note:</strong> Full end-to-end fix also depends on <a href="https://www.drupal.org/project/ai/issues/3508503">#3508503</a> which fixes bugs in <code>baseShouldSave()</code> itself (broken <code>$original</code> calculation and unconditional early return that makes <code>edit_mode</code> dead code for all<br>
plugins).</p>
<p><strong>Original request:</strong> This issue was originally filed as a feature request to add a separate <code>override_value</code> config checkbox to the ImageAltText plugin. After maintainer feedback (#8–#10), this was reframed as a bug — the existing "Edit when changed" setting already covers the use case but<br>
doesn't work for ImageAltText due to the bugs above.</p>
<h3 id="summary-proposed-resolution">Proposed resolution</h3>
<ul>
<li><code>checkIfEmpty()</code>: Return the original <code>$value</code> array (numerically indexed) instead of an associative marker, so <code>baseShouldSave()</code> can properly evaluate <code>$value[0]</code>.</li>
<li><code>storeValues()</code>: Only skip existing alt text when <code>edit_mode</code> is off. When on, allow overwriting.</li>
</ul>
<p>No new configuration is needed.</p>
<h3>Steps to reproduce</h3>
<ol>
<li>Create an image field with an AI automator using the ImageAltText rule.</li>
<li>Enable "Edit when changed".</li>
<li>Upload an image, save — alt text is generated.</li>
<li>Edit the entity, change the image — the old alt text is kept, the new LLM-generated alt text is discarded.</li>
</ol>
<h3>Remaining tasks</h3>
<ul>
<li>Review MR.</li>
<li>Ensure <a href="https://www.drupal.org/project/ai/issues/3508503">#3508503</a> lands (fixes <code>baseShouldSave()</code> logic).</li>
</ul>
<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>
> Related issue: [Issue #3508503](https://www.drupal.org/node/3508503)
issue