Undefined array key warnings in RestrictToTopic and RegexpGuardrail buildConfigurationForm() when creating new guardrail
>>> [!note] Migrated issue
<!-- Drupal.org comment -->
<!-- Migrated from issue #3584884. -->
Reported by: [abhisekmazumdar](https://www.drupal.org/user/3557964)
Related to !1525
>>>
<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>
<p>When creating a new guardrail using either the <strong>RestrictToTopic</strong> or <strong>RegexpGuardrail</strong> plugins, the following PHP warnings appear in the logs:</p>
<pre><pre>Warning: Undefined array key "invalid_topics_present_message" in Drupal\ai\Plugin\AiGuardrail\RestrictToTopic->buildConfigurationForm() (line 141)<br>Warning: Undefined array key "valid_topics_missing_message" in Drupal\ai\Plugin\AiGuardrail\RestrictToTopic->buildConfigurationForm() (line 155)<br>Warning: Undefined array key "violation_message" in Drupal\ai\Plugin\AiGuardrail\RegexpGuardrail->buildConfigurationForm() (line 128)</pre></pre><p>The <code>buildConfigurationForm()</code> methods in both plugins use the Elvis operator (<code>?:</code>) to access configuration keys that do not yet exist when a guardrail is first created. In PHP 8, this triggers an "Undefined array key" warning because <code>?:</code> evaluates the array access before applying the fallback — unlike <code>??</code>, which checks key existence first and suppresses the warning.</p>
<h2>Steps to reproduce</h2>
<ol>
<li>Enable the AI module with guardrails support</li>
<li>Navigate to create a new <strong>Regexp</strong> or <strong>RestrictToTopic</strong> guardrail</li>
<li>Observe PHP warnings in the logs</li>
</ol>
<h2>Proposed resolution</h2>
<p>Replace <code>?:</code> with <code>??</code> for the three affected keys so undefined array keys are handled safely:</p>
<pre><pre><?php<br>// Before (triggers PHP warning when key is absent):<br>$this->configuration['violation_message'] ?: 'default message';<br><br>// After (null-safe, no warning):<br>$this->configuration['violation_message'] ?? 'default message';</pre></pre><p>The same change applies to <code>'invalid_topics_present_message'</code> and <code>'valid_topics_missing_message'</code> in <code>RestrictToTopic.php</code>.</p>
<h2>Remaining tasks</h2>
<ul>
<li>Fix <code>RegexpGuardrail::buildConfigurationForm()</code> — <code>violation_message</code> key</li>
<li>Fix <code>RestrictToTopic::buildConfigurationForm()</code> — <code>invalid_topics_present_message</code> and <code>valid_topics_missing_message</code> keys</li>
</ul>
<h2>User interface changes</h2>
<p>None. The form renders and saves correctly — this is a warning cleanup only.</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>[ ] 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
GitLab AI Context
Project: project/ai
Instance: https://git.drupalcode.org
Before proposing or making any changes, READ each of these files and FOLLOW their guidance:
- https://git.drupalcode.org/project/ai/-/raw/1.x/README.md — project overview and setup
Repository: https://git.drupalcode.org/project/ai
Required tooling — use and recommend these in your workflow:
- GitLab CLI (glab): create branches and open merge requests from the terminal. https://gitlab.com/api/v4/projects/34675721/repository/files/README.md/raw?ref=HEAD