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 with placement, langcode, elements (question, answer, sources, rendered through ai_answers_element with their declarative shadow roots), strings (the JS's Drupal.t() strings, translated, keyed by source string) and urls (absolute question and feedback endpoints).
  • ElementBuilder builds 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_urls variable on ai_answers_element (default FALSE), passed through to a new optional $absolute argument on ElementStylesheets::getUrls().
  • Cacheability: a CacheableJsonResponse with the placement as a dependency, the bubbled render metadata, and the languages:language_interface and url.site contexts.
  • 404 for unknown, disabled and malformed placements, with the same {"error": "Unknown placement."} the question endpoint gives. It goes through PlacementResolver::resolve(), and the response is not cached.
  • Docs: a "Widget markup" section in placements.md, plus notes in architecture.md, theming.md (a theme appending to stylesheets should check absolute_urls) and the existing known-gaps entry.

Decisions to review

  • data-placement, not data-agent, on the answer element. The browser never names the agent of a placement. The runtime in #3615769 (closed) reads it. The current ai_answers.answer.js doesn't, which is fine since it never sees this markup.
  • ElementBuilder is static, not a service. It has no dependencies, and the block Unit tests build blocks with newInstanceWithoutConstructor(). A constructor-injected service would have meant changing 4 test fixtures for no gain.
  • Fixed element ids (ai-answers-{question,answer,sources}-<uuid>) via Html::getId(), not getUniqueId(). The markup is cached and injected into a page Drupal never sees, so a per-request suffix buys nothing.
  • sources is always in the payload. With show_references on, the answer lists references itself, so an embed places sources only 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.md say 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.
  • strings is pinned to the JS. PlacementWidgetEndpointTest parses js/*.js and fails if a Drupal.t() string is added or removed without updating the list.

Testing

  • Kernel: PlacementWidgetEndpointTest covers payload shape, shadow root and absolute stylesheets per element, the data-placement and data-target wiring, 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: ElementStylesheetsTest covers 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)

Merge request reports

Loading
Loading