Editorial teams in universities, media groups, and public administrations spend up to 60% of their time on repetitive tasks: translating content into English, writing SEO meta descriptions, summarizing long documents, and assigning taxonomy tags. By integrating Anthropic Claude or OpenAI directly into the Drupal 11 node edit form, we turn a 20-minute task into a 5-second review.
1. Architecture of a Native Drupal 11 AI Module
In my open-source module Drupal AI Assistant, the integration follows strict Drupal enterprise best practices:
- Key Management: Never store API keys in plain configuration. Use the
keymodule or environment variables outside the webroot. - Service Layer: Create an injectable service (
drupal_ai_assistant.client) wrapping Drupal'shttp_client(Guzzle) with timeouts and structured system prompts. - Form Alter & AJAX Callbacks: Attach non-intrusive action buttons directly to node edit forms via
hook_form_BASE_FORM_ID_alter().
2. Structured Prompts for Reliable JSON Output
When auto-generating SEO metadata or taxonomy suggestions, instruct Claude to return strict JSON so your Drupal AJAX callback can populate the form fields deterministically:
$response = $this->httpClient->post("https://api.anthropic.com/v1/messages", [
"headers" => [
"x-api-key" => $apiKey,
"anthropic-version" => "2023-06-01",
"content-type" => "application/json",
],
"json" => [
"model" => "claude-3-5-sonnet-latest",
"max_tokens" => 1024,
"system" => "You are a Drupal editorial assistant. Return valid JSON with keys: summary, seo_description, suggested_tags.",
"messages" => [
["role" => "user", "content" => $nodeBodyText],
],
],
"timeout" => 30,
]);
3. Human-in-the-Loop Validation
AI should assist editors, never publish blindly. Combined with Drupal Content Validator, every AI-generated translation or summary passes through business-rule validators before a node can transition to Published.