This project is not covered by Drupal’s security advisory policy.

AI Metatag Generator

A Drupal module that leverages AI to automatically generate metatags based on the content
(title, description, keywords, abstract) for content nodes.

Features

  • AI-powered metatag generation (title, description, keywords, abstract) from node view
  • Per-language prompt configuration
  • Default prompt template with language placeholder
  • Configurable content types and metatag field
  • Selectable AI provider and model
  • HTML content stripping and cleanup
  • One-click AJAX generation from node edit forms
  • Success/error feedback via modal dialog popups
  • Comprehensive error handling with try-catch and logging

Requirements

  • Drupal 10+
  • AI module (`drupal/ai`)
  • Metatag module (`drupal/metatag`)
  • PHP 8+

Installation

  1. Enable the module via Drupal admin or Drush: drush en ai_metatag_generator
  2. For site-wide installation, add to core.extension.yml

Configuration

Navigate to Configuration > AI > AI Metatag Generator
(`/admin/config/ai/ai-metatag-generator`).

Settings

  • Content Types: Select which content types allow AI metatag generation.
  • Meta tag field machine name: The metatag field on the content type (e.g. `field_meta_tag`).
  • AI Provider and Model: Select the AI provider/model for chat operations. Falls back to the default provider if not set.
  • Content Processing:
    • Strip HTML Tags: Remove HTML before sending to AI.
    • Display Mode: View mode used to render node content. (default: `full`)
  • Prompts
    • Default Prompt: A read-only reference prompt showing the template used when no language-specific prompt is set.
    • Per-language prompts: Customize the prompt for each enabled language.

Default Configuration

content_types: {}
metatag_field: 'field_meta_tag'
provider_model: ''
strip_html: true
display_mode: 'full'
prompts: {}
black_list: ''

Usage

From Node Edit Forms

  1. Edit a node of an enabled content type that has the configured metatag field.
  2. Click Generate Metatags with AI in the form actions area.
  3. The module will:
    • Render the node content using the configured display mode.
    • Strip HTML if configured.
    • Send content to the AI provider with the language-specific prompt (or the default prompt).
    • Parse the JSON response.
    • Populate the metatag form fields (title, description, keywords, abstract) via AJAX.
  4. A popup dialog confirms success or displays errors.
  5. Review and save the node to apply changes.

Programmatic Usage

$ai_service = \Drupal::service(
  'ai_metatag_generator.ai_service'
);

// Generate metatags for a node.
$metatags = $ai_service->generateMetatags($node);
// Returns: ['title' => ..., 'description' => ...,
//   'keywords' => ..., 'abstract' => ...] or NULL.

// Update node metatag fields.
$ai_service->updateNodeMetatags($node, $metatags);

Permissions

  • Administer AI Metatag Generator: Configure module settings (`administer ai metatag generator`).
  • Use AI Metatag Generator: Generate metatags for content (`use ai metatag generator`)

Error Handling

  • All service methods and form handlers use try-catch blocks with logging to the `ai_metatag_generator` channel.
  • AJAX errors are displayed in modal dialog popups.
  • Configuration save failures show messenger errors.
  • AI provider unavailability is handled gracefully with a user-facing message on the config form.

Development

Running Tests

All tests

./vendor/bin/phpunit <module_path>/ai_metatag_generator/tests/

Specific test

./vendor/bin/phpunit <module_path>/ai_metatag_generator/tests/src/Unit/Services/AiMetatagServiceTest.php

Code Standards

./vendor/bin/phpcs <module_path>/ai_metatag_generator/

Troubleshooting

  • Generate button not showing: Check that the node's content type is selected in config, the metatag field name matches, the node is not new, and the user has the use ai metatag generator permission.
  • AI calls failing: Verify the AI module is configured with valid API keys and a chat provider is available.
  • Empty responses: Check the prompt template.
  • Config form errors: If AI providers fail to load, the form will display an error message instead of settings.
  • AJAX requests prevent page reloads
  • Asynchronous processing for better user experience

Support

For issues related to this module, check:

  1. Module configuration at /admin/config/ai/ai-metatag-generator
  2. Drupal logs at /admin/reports/dblog
  3. AI module configuration
  4. Network connectivity to AI services

Project information

Releases