feat: #3615770 Show a copyable embed snippet on the placement edit form
Stacked on !33 (merged), !35 (merged), !36 (merged), !37 (merged) and !39 (merged); review only the last commit(s) until those merge.
Closes #3615770 (closed).
What changes
Snippet builder. New EmbedSnippet service (ai_answers.embed_snippet) builds the snippet from a saved placement, so the form, the tests and later code share 1 source.
v1.cssandv1.jsURLs are absolute, on the site's base URL, built with the "not applicable" language, so no language prefix or domain is added. The script reads the site from its ownsrc.<ai-answers-sources>only whenshow_referencesis off. The endpoint still always returnssources.<noscript><a href="…">Ask a question</a></noscript>inside the container whenfallback_urlis set. A path on this site goes on the base URL as typed: no alias, no language prefix, no re-encoding.- Every value is escaped; the fallback href also goes through
UrlHelper::filterBadProtocol().
Placement edit form. New "Embed snippet" section after the placement id: read-only field with the snippet, "Copy snippet" button, a role="status" line and links to the docs. Not on the add form, since there is no UUID yet.
Warnings. In the section, when the placement is disabled or has no allowed origins. Both break the embed.
Copy button. ai_answers/snippet library, js/ai_answers.snippet.js: a behavior with once, no inline JS. Clipboard API in a secure context, execCommand('copy') on plain HTTP. If both fail it leaves the text selected and says so. The button is hidden until the behavior runs.
Docs. docs/placements.md gets "Copy the snippet"; the work in progress note no longer says the snippet is to come.
Snippet
<link rel="stylesheet" href="https://example.com/ai-answers/widget/v1.css">
<div data-ai-answers="PLACEMENT-UUID">
<ai-answers-question></ai-answers-question>
<ai-answers-answer></ai-answers-answer>
<noscript><a href="https://example.com/ask">Ask a question</a></noscript>
</div>
<script src="https://example.com/ai-answers/widget/v1.js" defer></script>Decisions to look at
<noscript>sits inside the container, so the fallback shows where the widget would. The loader leaves other tags alone.- "Ask a question" is in the admin's interface language. The admin can edit it after pasting.
- A root-relative fallback is not routed:
/ask-ailinks to<base>/ask-aiexactly, even if the site has a language prefix or alias for it. - The snippet and warnings show the saved placement, not unsaved form values. The help text says to save first.
- Snippet on the edit form only, not in the list. The list already shows origins and status.
- Help links go to the published docs (
placements/#copy-the-snippet,theming/). The issue namesdocs/embedding.md, which doesn't exist.
Tests
- New
EmbedSnippetTest(Kernel, 5 tests): full default snippet, sources only with references off, absolute and relative fallback, escaping of UUID and fallback, no language prefix with URL negotiation on. - New
PlacementSnippetTest(Functional, 2 tests): not on the add form, field value equals the service output and is escaped in the page, button hidden and library attached, saving doesn't change the placement, each warning on and off. - New
PlacementSnippetCopyTest(FJS): the button shows and a click reports the outcome. Not run locally (no WebDriver here); CI is its first run. - Local: Kernel 181 passed, Functional 11, Unit 52,
ai_answers_logKernel 33. PHPStan clean, PHPCS clean on the new files, cspell and ESLint clean (4 JSDoc warnings of the kind core's behavior files raise).
AI disclosure
Used AI to help with research, writing and code (writing the snippet service, form section and tests, drafting this description). The reasoning and positions are my own, and I reviewed everything before posting. (Drupal.org AI policy)