Drupal 8の堅牢なプラグインシステムは、再利用可能な機能を備えたバックエンド開発者に力を与えます。この記事(2つのパート1の1つ)は、ノードエンティティを使用してカスタムフォームを有効にする機能を構築し、ノードバンドルの構成がノードディスプレイとともにさまざまなフォームタイプを利用できるようにします。 新しいフォームタイプは、提供されたベースクラスを拡張することで簡単に定義できます。 (完全なコードの例については、このリポジトリを参照してください)。
Drupal 8のプラグインシステムは、再利用可能な機能を促進し、ノードエンティティのカスタムフォームを有効にします。 ノードバンドルは、ノードディスプレイ内の複数のフォームタイプを使用するように構成できます。 プラグインマネージャーは、プラグインの発見と読み込みに不可欠なプラグインマネージャーであり、簡単な拡張機能のためにDrupalのデフォルトのベースクラスを活用します。 すべてのプラグインは、定義されたインターフェイスを実装する必要があります
- プラグインの定義は、重要な情報を含む注釈を利用します:プラグインサブディレクトリ、必要なインターフェイス、およびプラグインプロパティを定義するアノテーションクラス。
- カスタムプラグインタイプには、すべてのプラグインによって拡張可能なベースクラスが必要です。このクラスは、インターフェイスを実装し、フォーム構造に不可欠な サービスに依存関係注入を使用します。 プラグインはフォームクラスと対話します。次のステップは、これらのフォームをノードディスプレイと統合することです。
- プラグインマネージャー:
-
プラグインマネージャーは、プラグインを発見してロードするために重要なプラグインマネージャーが、Drupalの
form_builder
を拡張します。 モジュールの >ディレクトリ内で、
これにより
が拡張され、コンストラクターがオーバーライドされます。 キーパラメーターが定義します:
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
Alter Hook( -
Drupalreusable_formsReusableFormPluginInterface
プラグインインターフェイス: -
Drupalreusable_formsAnnotationReusableForm
インターフェイス( 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
を拡張します。
プラグインアノテーション:
アノテーションクラス(inReusableForm.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
プラグインベースクラス:
プラグインベースクラス(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)およびベースクラス(in
)は、一貫性のために作成されます:(これらは元の応答で表示され、ここではbrevityのために繰り返されません) 。
ReusableFormInterface.php
/src/Form
結論(パート1):ReusableFormBase.php
/src/Form
以上がDrupal 8カスタムプラグインタイプの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

thesecrettokeepingaphp-poweredwebsterunningsmootlyunderheavyloadinvolvesseveralkeystrategies:1)emform opcodecoduceSciptionexecutiontime、2)aatabasequerycachingwithiThing withiThistolessendavasoload、

コードをより明確かつ維持しやすくするため、依存関係が関心(DI)に注意する必要があります。 1)DIは、クラスを切り離すことにより、よりモジュール化されます。2)テストとコードの柔軟性の利便性を向上させ、3)DIコンテナを使用して複雑な依存関係を管理しますが、パフォーマンスの影響と円形の依存関係に注意してください。

はい、最適化されたAphPossibleandessention.1)CachingingusapCutoredatedAtabaseload.2)最適化、効率的なQueries、およびConnectionPooling.3)EnhcodeCodewithBultinctions、Avoididingglobalbariables、およびUsingopcodeching

keyStrategIestsoSificlyvoostphpappliceperformanceare:1)useopcodecachinglikeToreexecutiontime、2)最適化abaseの相互作用とプロペラインデックス、3)3)構成

aphpDependencyInjectionContaineriSATOULTAINATINAGECLASSDEPTINCIES、強化測定性、テスト可能性、および維持可能性。

SELECT DEPENTENCINGINOFCENT(DI)大規模なアプリケーションの場合、ServicElocatorは小さなプロジェクトまたはプロトタイプに適しています。 1)DIは、コンストラクターインジェクションを通じてコードのテスト可能性とモジュール性を改善します。 2)ServiceLocatorは、センター登録を通じてサービスを取得します。これは便利ですが、コードカップリングの増加につながる可能性があります。

phpapplicationscanbeoptimizedforspeedandEfficiencyby:1)enabingopcacheinphp.ini、2)PreparedStatementswithpordatabasequeriesを使用して、3)LoopswithArray_filterandarray_mapfordataprocessing、4)の構成ngincasaSearverseproxy、5)

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

SublimeText3 中国語版
中国語版、とても使いやすい

WebStorm Mac版
便利なJavaScript開発ツール

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

SublimeText3 Linux 新バージョン
SublimeText3 Linux 最新バージョン

ドリームウィーバー CS6
ビジュアル Web 開発ツール
