> drupal 8的鲁棒插件系统,使后端开发人员具有可重复使用的功能。本文(两个部分中的第一部分)详细信息构建功能,启用具有节点实体的自定义表单,从而允许节点束的配置与节点显示旁边使用各种形式类型。 通过扩展提供的基类可以轻松定义新的形式类型。 (有关完整的代码示例,请参阅此存储库
)。>
本教程避免了深入的插件力学,假设对基本理论熟悉。我们将使用两个接口和六个类构建自定义插件类型(看似大的数字,但大多是直截了当的样板代码。 第二部分将演示将这些可重复使用的形式附加到节点上。
> 密钥概念:
- > 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
>
)允许模块修改插件定义,并配置了缓存后端。
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中文网其他相关文章!

TheSecretTokeEpingAphp-PowerEdwebSiterUnningSmoothlyShyunderHeavyLoadInVolvOLVOLVOLDEVERSALKEYSTRATICES:1)emplactopCodeCachingWithOpcachingWithOpCacheToreCescriptexecution Time,2)使用atabasequercachingCachingCachingWithRedataBasEndataBaseLeSendataBaseLoad,3)

你应该关心DependencyInjection(DI),因为它能让你的代码更清晰、更易维护。1)DI通过解耦类,使其更模块化,2)提高了测试的便捷性和代码的灵活性,3)使用DI容器可以管理复杂的依赖关系,但要注意性能影响和循环依赖问题,4)最佳实践是依赖于抽象接口,实现松散耦合。

是的,优化papplicationispossibleandessential.1)empartcachingingcachingusedapcutorediucedsatabaseload.2)优化的atabaseswithexing,高效Quereteries,and ConconnectionPooling.3)EnhanceCodeWithBuilt-unctions,避免使用,避免使用ingglobalalairaiables,并避免使用

theKeyStrategiestosiminificallyBoostphpapplicationPermenCeare:1)useOpCodeCachingLikeLikeLikeLikeLikeCacheToreDuceExecutiontime,2)优化AtabaseInteractionswithPreparedStateTemtStatementStatementSandProperIndexing,3)配置

aphpdepentioncontiveContainerIsatoolThatManagesClassDeptions,增强codemodocultion,可验证性和Maintainability.itactsasaceCentralHubForeatingingIndections,因此reducingTightCightTightCoupOulplingIndeSingantInting。

选择DependencyInjection(DI)用于大型应用,ServiceLocator适合小型项目或原型。1)DI通过构造函数注入依赖,提高代码的测试性和模块化。2)ServiceLocator通过中心注册获取服务,方便但可能导致代码耦合度增加。

phpapplicationscanbeoptimizedForsPeedAndeffificeby:1)启用cacheInphp.ini,2)使用preparedStatatementSwithPdoforDatabasequesies,3)3)替换loopswitharray_filtaray_filteraray_maparray_mapfordataprocrocessing,4)conformentnginxasaseproxy,5)

phpemailvalidation invoLvesthreesteps:1)格式化进行regulareXpressecthemailFormat; 2)dnsvalidationtoshethedomainhasavalidmxrecord; 3)


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

Dreamweaver Mac版
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

WebStorm Mac版
好用的JavaScript开发工具

Atom编辑器mac版下载
最流行的的开源编辑器

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中