> drupal 8的鲁棒插件系统,使后端开发人员具有可重复使用的功能。本文(两个部分中的第一部分)详细信息构建功能,启用具有节点实体的自定义表单,从而允许节点束的配置与节点显示旁边使用各种形式类型。 通过扩展提供的基类可以轻松定义新的形式类型。 (有关完整的代码示例,请参阅此存储库
)。>
本教程避免了深入的插件力学,假设对基本理论熟悉。我们将使用两个接口和六个类构建自定义插件类型(看似大的数字,但大多是直截了当的样板代码。 第二部分将演示将这些可重复使用的形式附加到节点上。
> 密钥概念:
form_builder
>
>插件管理器:>
插件管理器,对于发现和加载插件至关重要,它扩展了Drupal的。 在模块的DefaultPluginManager
目录中,/src
>包含:ReusableFormManager.php
>
<?php namespace Drupal\reusable_forms; use Drupal\Core\Plugin\DefaultPluginManager; use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Extension\ModuleHandlerInterface; class ReusableFormsManager extends DefaultPluginManager { public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) { parent::__construct('Plugin/ReusableForm', $namespaces, $module_handler, 'Drupal\reusable_forms\ReusableFormPluginInterface', 'Drupal\reusable_forms\Annotation\ReusableForm'); $this->alterInfo('reusable_forms_info'); $this->setCacheBackend($cache_backend, 'reusable_forms'); } }
,覆盖了构造函数。 关键参数定义:DefaultPluginManager
Plugin/ReusableForm
Drupalreusable_formsReusableFormPluginInterface
Drupalreusable_formsAnnotationReusableForm
>
)允许模块修改插件定义,并配置了缓存后端。
reusable_forms_info
接口(in
)定义了所有插件必须实现的方法:<?php namespace Drupal\reusable_forms; use Drupal\Core\Plugin\DefaultPluginManager; use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Extension\ModuleHandlerInterface; class ReusableFormsManager extends DefaultPluginManager { public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) { parent::__construct('Plugin/ReusableForm', $namespaces, $module_handler, 'Drupal\reusable_forms\ReusableFormPluginInterface', 'Drupal\reusable_forms\Annotation\ReusableForm'); $this->alterInfo('reusable_forms_info'); $this->setCacheBackend($cache_backend, 'reusable_forms'); } }
>getName()
>返回插件名称; buildForm()
接受实体,并返回实现Drupalreusable_formsFormReusableFormInterface
>和PluginInspectionInterface
,以添加功能和依赖注入。ContainerFactoryPluginInterface
>
>插件注释:
)定义了插件属性:ReusableForm.php
/src/Annotation
<?php namespace Drupal\reusable_forms; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Component\Plugin\PluginInspectionInterface; interface ReusableFormPluginInterface extends PluginInspectionInterface, ContainerFactoryPluginInterface { public function getName(); public function buildForm($entity); }
和id
(完全限定的表单名称)在此处定义。name
>
form
插件base类(
in)提供默认值:ReusableFormPluginBase.php
>
/src
<?php namespace Drupal\reusable_forms\Annotation; use Drupal\Component\Annotation\Plugin; /** * @Annotation */ class ReusableForm extends Plugin { public $id; public $name; public $form; }
>。 使用注释中指定的表单类实现PluginBase
和ReusableFormPluginInterface
。form_builder
>
getName()
buildForm()
形式接口和基类:
> 一个简单的表单接口(in
)和基类(inReusableFormInterface.php
>)是为了一致性而创建的:(这些在原始响应中显示,并且在此处未重复此处) 。/src/Form
ReusableFormBase.php
/src/Form
>结论(第1部分):
以上是Drupal 8自定义插件类型的详细内容。更多信息请关注PHP中文网其他相关文章!