Add entity type and bundle context scope plugin
>>> [!note] Migrated issue
<!-- Drupal.org comment -->
<!-- Migrated from issue #3582920. -->
Reported by: [jessehs](https://www.drupal.org/user/620440)
Related to !115
>>>
<p>[Tracker]<br>
<strong>Update Summary: </strong>Merge request ready for review — adds Entity Bundle scope plugin for bundle-level context matching<br>
<strong>Check-in Date: </strong>04/02/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>ai_context</code> module provides a Target Entity scope that restricts context items to <strong>specific entity instances</strong> (e.g., node 123). However, there is no built-in scope for targeting <strong>entire entity bundles</strong> — for example, "all Blog nodes" or "all Event nodes."</p>
<p>This is a common need for site builders who want to:</p>
<ul>
<li>Apply writing guidelines to all content of a given type (e.g., brand voice rules for blog posts, legal disclaimers for press releases)</li>
<li>Provide content-type-specific editorial instructions to AI tools without manually targeting every individual entity</li>
<li>Scope context items to taxonomy vocabularies (e.g., different instructions for "Categories" terms vs. "Tags" terms)</li>
</ul>
<p>Currently, the closest alternative is the Site Section scope using URL path patterns (e.g., <code>/blogs/*</code>), but this is fragile — it breaks when path aliases change, doesn't cover admin/edit routes, and requires manual pattern maintenance.</p>
<h3 id="summary-proposed-resolution">Proposed resolution</h3>
<p>Add a new <strong>Entity Bundle</strong> scope plugin (<code>entity_bundle</code>) that allows context items to be associated with specific entity type and bundle combinations.</p>
<h4>New file: <code>src/Plugin/AiContextScope/AiContextScopeEntityBundle.php</code></h4>
<ul>
<li>Extends <code>AiContextScopeBase</code> with the <code>#[AiContextScope]</code> attribute (plugin ID: <code>entity_bundle</code>, weight: 15).</li>
<li>Stores values as <code>{entity_type}:{bundle}</code> strings (e.g., <code>node:blog</code>, <code>taxonomy_term:tags</code>).</li>
<li>Uses the existing <code>AiContextEntityTargetResolver</code> service to detect the current entity from route parameters or request attributes — works on both frontend canonical pages (e.g., <code>/blogs/my-post</code>) and backend routes (e.g., <code>/node/123/edit</code>, <code>/node/123/revisions</code>).</li>
<li>Loads the detected entity and checks its bundle against the context item's configured bundle values via <code>matchesCurrentContext()</code>.</li>
<li>Provides an admin settings form at <code>/admin/ai/context/settings/scope/entity-bundle</code> where administrators select which entity types should have their bundles exposed as scope options. The plugin auto-discovers all bundles for the selected types using <code>EntityTypeBundleInfoInterface</code>.</li>
<li>On context item forms, displays checkboxes for each available bundle, labeled as "Entity Type: Bundle" (e.g., "Content: Blog", "Taxonomy term: Categories").</li>
<li>Supports agent subscriptions, multiple value selection, and the standard manage link.</li>
</ul>
<h4>New file: <code>config/install/ai_context.scope_settings.entity_bundle.yml</code></h4>
<ul>
<li>Default configuration enables the scope with <code>node</code> and <code>taxonomy_term</code> entity types.</li>
</ul>
<h4>Modified file: <code>config/schema/ai_context.schema.yml</code></h4>
<ul>
<li>Adds <code>ai_context.scope_settings.entity_bundle</code> schema with <code>enabled</code> (boolean) and <code>entity_types</code> (sequence of strings).</li>
</ul>
<h4>How it differs from existing scopes:</h4>
<table>
<tr>
<th>Scope</th>
<th>Granularity</th>
<th>Use case</th>
</tr>
<tr>
<td><strong>Target Entity</strong></td>
<td>Specific entity instance</td>
<td>"This context applies to node 123 only"</td>
</tr>
<tr>
<td><strong>Entity Bundle</strong> (new)</td>
<td>All entities of a bundle</td>
<td>"This context applies to all Blog nodes"</td>
</tr>
<tr>
<td><strong>Site Section</strong></td>
<td>URL path patterns</td>
<td>"This context applies to pages under /blogs/*"</td>
</tr>
</table>
<h3 id="summary-remaining-tasks">Target date or deadline</h3>
<p>No specific deadline.</p>
<h3 id="summary-remaining-tasks">Remaining tasks</h3>
<ol>
<li>Review and feedback on the approach.</li>
<li>Consider whether the settings form should also support per-bundle granularity (currently all bundles for a selected entity type are exposed — administrators could instead pick specific bundles to expose).</li>
<li>Consider adding kernel/functional tests for bundle detection and context matching.</li>
</ol>
<h3 id="summary-ai-usage">AI usage (if applicable)</h3>
<p>[x] AI Generated Code<br>
<br>This code was mainly generated by an AI with human guidance, and reviewed, tested, and refined by a human.</p>
> Related issue: [Issue #3570934](https://www.drupal.org/node/3570934)
issue