mcpField
D
drupal-plugins

drupal-plugins

github.com/edutrul开发7351 次安装21 周前更新
代码仓库

关于 drupal-plugins

面向 Drupal 11 开发的插件编写技能,涵盖基于属性的发现机制、插件管理器、依赖注入、可配置区块和 Drush 生成器。

主要功能

  1. 01基于 PHP 属性发现插件
  2. 02插件管理器与 ID 指引
  3. 03依赖注入模式
  4. 04可配置区块示例
  5. 05Drush 插件生成器

适用场景

  1. 01构建 Drupal 11 区块插件
  2. 02实现字段控件或格式化器
  3. 03添加条件或过滤器插件

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/edutrul/drupal-ai --skill drupal-plugins

安装后在 Claude Code 中输入/drupal-plugins确认已加载。

附带文件README.md

详细介绍

Drupal 插件(Drupal 11)

示例(最佳实践)

// src/Plugin/Block/MyBlock.php
namespace Drupal\my_module\Plugin\Block;

use Drupal\Core\Block\Attribute\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

#[Block(
  id: 'my_block',
  admin_label: new TranslatableMarkup('My Block'),
  category: new TranslatableMarkup('Custom'),
)]
final class MyBlock extends BlockBase implements ContainerFactoryPluginInterface {

  use StringTranslationTrait;

  public function __construct(
    array $configuration,
    string $plugin_id,
    mixed $plugin_definition,
    private readonly EntityTypeManagerInterface $entityTypeManager,
  ) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
  }

  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): static {
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->get('entity_type.manager'),
    );
  }

  public function build(): array {
    return [
      '#markup' => $this->t('Hello from my block.'),
      '#cache' => [
        'tags' => ['node_list'],
        'contexts' => ['user.permissions'],
      ],
    ];
  }

}

注意:插件通过属性(或旧版代码中的注解)发现,而不是通过 services.yml 发现。

PHP 属性与注解

使用 PHP 属性(Drupal 10.2+;Drupal 11 要求使用此风格):

#[Block(id: 'my_block', admin_label: new TranslatableMarkup('My Block'))]

旧版(不推荐):

// @Block(id = "my_block", admin_label = @Translation("My Block"))

可配置区块(blockForm / blockSubmit)

public function defaultConfiguration(): array {
  return ['limit' => 5];
}

public function blockForm(array $form, FormStateInterface $form_state): array {
  $form['limit'] = [
    '#type' => 'number',
    '#title' => $this->t('Limit'),
    '#default_value' => $this->configuration['limit'],
  ];
  return $form;
}

public function blockSubmit(array $form, FormStateInterface $form_state): void {
  $this->configuration['limit'] = $form_state->getValue('limit');
}

插件生成器

drush generate plugin:block
drush generate plugin:field:formatter
drush generate plugin:field:widget
drush generate plugin:field:type
drush generate plugin:condition
drush generate plugin:filter

思维模型

发现通过 PHP 属性(或旧版注解)——而不是 services.yml
实例化由插件管理器执行,而不是由服务容器执行
依赖注入需要 ContainerFactoryPluginInterface
管理器每种插件类型都有自己的管理器(例如 BlockManager)
插件 ID必须唯一——供 Drupal 在内部识别插件

评论

支持 Markdown