Plugin form element preview

Plugin form element provides two Form API elements — plugin and plugins — that let developers embed plugin configuration forms directly inside any Drupal form.

The plugin element renders the configuration form for a single pre-selected plugin (e.g. one condition, one action). The plugins element provides a full multi-item UI backed by vertical tabs, where users can add, configure, and remove multiple plugins from any plugin manager — with optional cardinality limits and allowlists.

Features

  • #type => 'plugin' — renders the configuration form for a single plugin instance
  • #type => 'plugins' — multi-item UI with vertical tabs, AJAX add/remove, and form submission handling
  • Works with any plugin manager implementing PluginManagerInterface
  • Supports FilteredPluginManagerInterface for context-aware plugin filtering
  • #cardinality property to limit how many plugins can be added
  • #allowed_plugins property to restrict which plugins appear in the Add menu
  • Full compatibility with PluginFormInterface and PluginWithFormsInterface

Usage

Configure a single plugin whose ID you already know:

$form['condition'] = [
  '#type' => 'plugin',
  '#title' => $this->t('Condition'),
  '#plugin_manager' => 'plugin.manager.condition',
  '#default_value' => [
    'id' => 'language',
    'langcodes' => ['en' => 'en'],
  ],
];

Let users pick and configure multiple plugins from any manager:

$form['conditions'] = [
  '#type' => 'plugins',
  '#title' => $this->t('Conditions'),
  '#plugin_manager' => 'plugin.manager.condition',
  '#cardinality' => -1,              // -1 = unlimited
  '#allowed_plugins' => ['language', 'request_path'], // optional allowlist
  '#default_value' => $saved_config, // keyed by UUID
];

On submit, $form_state->getValue('conditions') returns a UUID-keyed array of plugin configurations — ready to store and reinstantiate.

Supporting organizations: 
Development and maintanance

Project information

Releases