$npx -y skills add edutrul/drupal-ai --skill drupal-pluginsDrupal plugin system — Block, Field, Condition, Filter plugins with PHP attributes and ContainerFactoryPluginInterface.
| 1 | # Drupal Plugins (Drupal 11) |
| 2 | |
| 3 | ## Example (Best Practice) |
| 4 | |
| 5 | ```php |
| 6 | // src/Plugin/Block/MyBlock.php |
| 7 | namespace Drupal\my_module\Plugin\Block; |
| 8 | |
| 9 | use Drupal\Core\Block\Attribute\Block; |
| 10 | use Drupal\Core\Block\BlockBase; |
| 11 | use Drupal\Core\Plugin\ContainerFactoryPluginInterface; |
| 12 | use Drupal\Core\StringTranslation\StringTranslationTrait; |
| 13 | use Drupal\Core\StringTranslation\TranslatableMarkup; |
| 14 | use Drupal\Core\Entity\EntityTypeManagerInterface; |
| 15 | use Symfony\Component\DependencyInjection\ContainerInterface; |
| 16 | |
| 17 | #[Block( |
| 18 | id: 'my_block', |
| 19 | admin_label: new TranslatableMarkup('My Block'), |
| 20 | category: new TranslatableMarkup('Custom'), |
| 21 | )] |
| 22 | final class MyBlock extends BlockBase implements ContainerFactoryPluginInterface { |
| 23 | |
| 24 | use StringTranslationTrait; |
| 25 | |
| 26 | public function __construct( |
| 27 | array $configuration, |
| 28 | string $plugin_id, |
| 29 | mixed $plugin_definition, |
| 30 | private readonly EntityTypeManagerInterface $entityTypeManager, |
| 31 | ) { |
| 32 | parent::__construct($configuration, $plugin_id, $plugin_definition); |
| 33 | } |
| 34 | |
| 35 | public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): static { |
| 36 | return new static( |
| 37 | $configuration, |
| 38 | $plugin_id, |
| 39 | $plugin_definition, |
| 40 | $container->get('entity_type.manager'), |
| 41 | ); |
| 42 | } |
| 43 | |
| 44 | public function build(): array { |
| 45 | return [ |
| 46 | '#markup' => $this->t('Hello from my block.'), |
| 47 | '#cache' => [ |
| 48 | 'tags' => ['node_list'], |
| 49 | 'contexts' => ['user.permissions'], |
| 50 | ], |
| 51 | ]; |
| 52 | } |
| 53 | |
| 54 | } |
| 55 | ``` |
| 56 | |
| 57 | Note: Plugins are discovered via attributes (or annotations in legacy code), not services.yml. |
| 58 | |
| 59 | ## PHP Attributes vs Annotations |
| 60 | |
| 61 | Use PHP attributes (Drupal 10.2+, required style for Drupal 11): |
| 62 | |
| 63 | ```php |
| 64 | #[Block(id: 'my_block', admin_label: new TranslatableMarkup('My Block'))] |
| 65 | ``` |
| 66 | Legacy (discouraged): |
| 67 | ```php |
| 68 | // @Block(id = "my_block", admin_label = @Translation("My Block")) |
| 69 | ``` |
| 70 | |
| 71 | ## Configurable Block (blockForm / blockSubmit) |
| 72 | |
| 73 | ```php |
| 74 | public function defaultConfiguration(): array { |
| 75 | return ['limit' => 5]; |
| 76 | } |
| 77 | |
| 78 | public function blockForm(array $form, FormStateInterface $form_state): array { |
| 79 | $form['limit'] = [ |
| 80 | '#type' => 'number', |
| 81 | '#title' => $this->t('Limit'), |
| 82 | '#default_value' => $this->configuration['limit'], |
| 83 | ]; |
| 84 | return $form; |
| 85 | } |
| 86 | |
| 87 | public function blockSubmit(array $form, FormStateInterface $form_state): void { |
| 88 | $this->configuration['limit'] = $form_state->getValue('limit'); |
| 89 | } |
| 90 | ``` |
| 91 | |
| 92 | ## Plugin Generators |
| 93 | |
| 94 | ```bash |
| 95 | drush generate plugin:block |
| 96 | drush generate plugin:field:formatter |
| 97 | drush generate plugin:field:widget |
| 98 | drush generate plugin:field:type |
| 99 | drush generate plugin:condition |
| 100 | drush generate plugin:filter |
| 101 | ``` |
| 102 | |
| 103 | ## Mental Model |
| 104 | |
| 105 | | | | |
| 106 | |---|---| |
| 107 | | **Discovery** | Via PHP attributes (or legacy annotations) — not services.yml | |
| 108 | | **Instantiation** | By plugin managers, not the service container | |
| 109 | | **DI** | Requires `ContainerFactoryPluginInterface` | |
| 110 | | **Manager** | Each plugin type has its own manager (e.g., `BlockManager`) | |
| 111 | | **Plugin ID** | Must be unique — used internally by Drupal to identify the plugin | |