首页 > 后端开发 > php教程 > Drupal 8自定义插件类型

Drupal 8自定义插件类型

Joseph Gordon-Levitt
发布: 2025-02-16 10:15:10
原创
202 人浏览过

> drupal 8的鲁棒插件系统,使后端开发人员具有可重复使用的功能。本文(两个部分中的第一部分)详细信息构建功能,启用具有节点实体的自定义表单,从而允许节点束的配置与节点显示旁边使用各种形式类型。 通过扩展提供的基类可以轻松定义新的形式类型。 (有关完整的代码示例,请参阅此存储库

)。

> Drupal 8 Custom Plugin Types

本教程避免了深入的插件力学,假设对基本理论熟悉。我们将使用两个接口和六个类构建自定义插件类型(看似大的数字,但大多是直截了当的样板代码。 第二部分将演示将这些可重复使用的形式附加到节点上。

> 密钥概念:

  • > Drupal 8的插件系统促进可重复使用的功能,为节点实体启用自定义表单。 可以将节点捆绑包配置为在节点显示内使用多个表单类型。
  • >插件管理器,对于插件发现和加载必不可少的,利用Drupal的默认基类以易于扩展。 所有插件都必须实现定义的接口。
  • >插件定义使用包含关键信息的注释:插件子目录,所需界面和定义插件属性的注释类。
  • >自定义插件类型需要所有插件可扩展的基类。该类实现界面,并使用依赖注入
  • 服务,对于形式结构必不可少。 该插件与表单类交互;下一步是将这些表单与节点显示。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>
一个Alter Hook(

)允许模块修改插件定义,并配置了缓存后端。 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>

>插件注释: 注释类(

in

)定义了插件属性: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;
}
登录后复制
,实现了,并使用依赖性注入

>。 使用注释中指定的表单类实现PluginBaseReusableFormPluginInterfaceform_builder> getName() buildForm()形式接口和基类:

> 一个简单的表单接口(in

)和基类(

inReusableFormInterface.php>)是为了一致性而创建的:(这些在原始响应中显示,并且在此处未重复此处) 。/src/Form ReusableFormBase.php/src/Form>结论(第1部分):>

>这第一部分设置了自定义插件类型,并准备将其与表单类集成。 第二部分将涵盖用节点显示这些表格,涉及节点类型的配置并在内容视图模式中呈现形式。

以上是Drupal 8自定义插件类型的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板