Skip to main content

eca ai actions drupal

ECA AI Actions for Drupal

Status: Production Ready Drupal Version: 11.x+ Required Modules: drupal/eca, drupal/ai, drupal/ai_agents

Overview

Extends Drupal's Event-Condition-Action (ECA) module with AI-powered actions using GitLab AI Gateway and drupal/ai providers.

Quick Implementation

1. AI Action Plugins

Generate Text Action:

<?php namespace Drupal\eca_ai\Plugin\Action; use Drupal\eca\Plugin\Action\ActionBase; /** * @Action( * id = "eca_ai_generate_text", * label = @Translation("AI: Generate Text"), * description = @Translation("Generate text using AI"), * type = "node" * ) */ class GenerateText extends ActionBase { public function execute() { $provider = \Drupal::service('ai.provider') ->createInstance('gitlab_ai_gateway'); $prompt = $this->tokenService->replace($this->configuration['prompt']); $output = $provider->chat($prompt, 'claude-sonnet-4'); $this->setTokenData('ai_response', $output->getNormalized()); } }

2. Common ECA + AI Workflows

Auto Meta Description:

# On node save, generate meta description events: node_presave: plugin: 'node:presave' configuration: bundle: article actions: generate_meta: plugin: 'eca_ai:generate_text' configuration: provider: gitlab_ai_gateway model: claude-sonnet-4 prompt: | Write 150-char meta description: Title: [node:title] Body: [node:body:summary] target_field: field_meta_description

Content Moderation:

# Moderate comments with AI events: comment_insert: plugin: 'comment:insert' actions: check_spam: plugin: 'eca_ai:classify' configuration: text: '[comment:body:value]' categories: - spam - offensive - appropriate unpublish_if_bad: plugin: 'comment:unpublish' condition: '[ai_category] != "appropriate"'

Auto Tagging:

# Auto-tag articles events: node_insert: plugin: 'node:insert' configuration: bundle: article actions: extract_tags: plugin: 'eca_ai:extract_entities' configuration: text: '[node:body:value]' entity_types: - topics - keywords apply_tags: plugin: 'taxonomy:apply_terms' configuration: vocabulary: tags terms: '[ai_entities]'

Available AI Actions

ActionDescriptionUse Case
eca_ai:generate_textGenerate text from promptMeta descriptions, summaries
eca_ai:classifyClassify text into categoriesContent moderation, routing
eca_ai:extract_entitiesExtract entities from textAuto-tagging, metadata
eca_ai:summarizeSummarize long textArticle summaries
eca_ai:translateTranslate textMulti-language support
eca_ai:sentimentAnalyze sentimentComment moderation
eca_ai:embeddingsGenerate embeddingsSemantic search

Integration with drupal/flowdrop

# Visual workflow in FlowDrop UI nodes: - id: trigger type: drupal_event config: event: node_insert - id: ai_generate type: ai_action config: provider: gitlab_ai_gateway action: generate_text prompt: "Summarize: {node.body}" - id: save_field type: drupal_action config: action: field_set field: field_summary value: "{ai_generate.output}" edges: - from: trigger to: ai_generate - from: ai_generate to: save_field

Performance Optimization

Caching:

// Cache AI responses $cache_key = 'ai:' . md5($prompt); $cached = \Drupal::cache()->get($cache_key); if ($cached) { return $cached->data; } $output = $provider->chat($prompt, 'claude-sonnet-4'); \Drupal::cache()->set($cache_key, $output, CacheBackendInterface::CACHE_PERMANENT);

Queue Processing:

// Process AI tasks in queue $queue = \Drupal::queue('eca_ai_queue'); $queue->createItem([ 'node_id' => $node->id(), 'action' => 'generate_meta', ]);

Testing

// Test AI action $action = $this->container ->get('plugin.manager.action') ->createInstance('eca_ai:generate_text'); $action->setConfiguration([ 'prompt' => 'Test prompt', 'model' => 'claude-sonnet-4', ]); $action->execute(); $response = $action->getTokenData('ai_response'); $this->assertNotEmpty($response);

References