Loading
feat: #3615768 Add an endpoint that returns a placement's rendered widget markup
Closes #3615768 (closed). Step 1.2 of #3615752.
What this adds
GET /ai-answers/widget/placement/{uuid}returns JSON withplacement,langcode,elements(question,answer,sources, rendered throughai_answers_elementwith their declarative shadow roots),strings(the JS'sDrupal.t()strings, translated, keyed by source string) andurls(absolute question and feedback endpoints).ElementBuilderbuilds the 3 element render arrays. The blocks and the endpoint both use it, so an embed gets the same markup as a block. Block output is unchanged. The existing Unit, Kernel and Functional tests pass without edits.- Absolute stylesheet URLs: a new
absolute_urlsvariable onai_answers_element(defaultFALSE), passed through to a new optional$absoluteargument onElementStylesheets::getUrls(). - Cacheability: a
CacheableJsonResponsewith the placement as a dependency, the bubbled render metadata, and thelanguages:language_interfaceandurl.sitecontexts. - 404 for unknown, disabled and malformed placements, with the same
{"error": "Unknown placement."}the question endpoint gives. It goes throughPlacementResolver::resolve(), and the response is not cached. - Docs: a "Widget markup" section in
placements.md, plus notes inarchitecture.md,theming.md(a theme appending tostylesheetsshould checkabsolute_urls) and the existing known-gaps entry.
Decisions to review
data-placement, notdata-agent, on the answer element. The browser never names the agent of a placement. The runtime in #3615769 (closed) reads it. The currentai_answers.answer.jsdoesn't, which is fine since it never sees this markup.ElementBuilderis static, not a service. It has no dependencies, and the block Unit tests build blocks withnewInstanceWithoutConstructor(). A constructor-injected service would have meant changing 4 test fixtures for no gain.- Fixed element ids (
ai-answers-{question,answer,sources}-<uuid>) viaHtml::getId(), notgetUniqueId(). The markup is cached and injected into a page Drupal never sees, so a per-request suffix buys nothing. sourcesis always in the payload. Withshow_referenceson, the answer lists references itself, so an embed placessourcesonly when it is off, as with the blocks. The snippet (#3615770 (closed)) decides that.- No embed token, not even a stub. The controller docblock and
placements.mdsay the token goes in a separate, uncached response, never in this cached payload. - Open access, GET only, no CORS. The payload is only what an embedding page shows anyway. CORS and the origin check belong to #3615766 (closed), so a browser on another origin can't read it yet. That is noted in known-gaps.
stringsis pinned to the JS.PlacementWidgetEndpointTestparsesjs/*.jsand fails if aDrupal.t()string is added or removed without updating the list.
Testing
- Kernel:
PlacementWidgetEndpointTestcovers payload shape, shadow root and absolute stylesheets per element, thedata-placementanddata-targetwiring, absolute endpoint URLs, strings matching the JS, cache tags and contexts, re-render after a placement save, 404 for unknown, disabled and malformed placements, and 405 for POST. It goes through the HTTP kernel, so the route and the early-rendering guard run too. - Unit:
ElementStylesheetsTestcovers the absolute flag. - Locally on 11.4 / PHP 8.4: Unit 50, Kernel 140 and Functional 6 pass. PHPCS (Drupal, DrupalPractice) and PHPStan are clean. CSpell, ESLint and Stylelint were not run locally, so the pipeline is the first check for those.
- Manual, on a local site: unknown and malformed UUIDs return a JSON 404, and POST returns 405. The site has no answers-enabled agent, so the 200 path is covered by the Kernel test only.
AI disclosure
Used AI to help with research, writing and code (reading the block and element render path, writing the builder, endpoint and tests, drafting this description). The reasoning and positions are my own, and I reviewed everything before posting. (Drupal.org AI policy)