ホームページ >バックエンド開発 >PHPチュートリアル >Drupal 8カスタムプラグインタイプ
Drupal 8の堅牢なプラグインシステムは、再利用可能な機能を備えたバックエンド開発者に力を与えます。この記事(2つのパート1の1つ)は、ノードエンティティを使用してカスタムフォームを有効にする機能を構築し、ノードバンドルの構成がノードディスプレイとともにさまざまなフォームタイプを利用できるようにします。 新しいフォームタイプは、提供されたベースクラスを拡張することで簡単に定義できます。 (完全なコードの例については、このリポジトリを参照してください)。
Drupal 8のプラグインシステムは、再利用可能な機能を促進し、ノードエンティティのカスタムフォームを有効にします。 ノードバンドルは、ノードディスプレイ内の複数のフォームタイプを使用するように構成できます。 プラグインマネージャーは、プラグインの発見と読み込みに不可欠なプラグインマネージャーであり、簡単な拡張機能のためにDrupalのデフォルトのベースクラスを活用します。 すべてのプラグインは、定義されたインターフェイスを実装する必要があります
form_builder
を拡張します。 モジュールのこれにより
が拡張され、コンストラクターがオーバーライドされます。 キーパラメーターが定義します:
DefaultPluginManager
/src
ReusableFormManager.php
:プラグインサブディレクトリ。
<code class="language-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'); } }</code>
:必要なプラグインインターフェイス。DefaultPluginManager
Plugin/ReusableForm
Drupalreusable_formsReusableFormPluginInterface
プラグインインターフェイス:Drupalreusable_formsAnnotationReusableForm
インターフェイス(<code class="language-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'); } }</code>
getName()
プラグイン名を返します。 buildForm()
エンティティを受け入れ、Drupalreusable_formsFormReusableFormInterface
を実装するフォームのレンダリング配列を返します。 機能と依存関係のインジェクションを追加するためにPluginInspectionInterface
およびContainerFactoryPluginInterface
を拡張します。
プラグインアノテーション:
アノテーションクラス(inReusableForm.php
)は、プラグインプロパティを定義します
/src/Annotation
<code class="language-php"><?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); }</code>、
、およびid
(完全に資格のあるフォームクラス名)はここで定義されています。
name
form
プラグインベースクラス:
プラグインベースクラス(in
)はデフォルトを提供します:
ReusableFormPluginBase.php
/src
これは
<code class="language-php"><?php namespace Drupal\reusable_forms\Annotation; use Drupal\Component\Annotation\Plugin; /** * @Annotation */ class ReusableForm extends Plugin { public $id; public $name; public $form; }</code>を実装し、
の依存関係注射を使用します。 アノテーションで指定されたフォームクラスを使用して、PluginBase
およびReusableFormPluginInterface
が実装されています。
form_builder
getName()
フォームインターフェイスとベースクラス:buildForm()
in)およびベースクラス(in
)は、一貫性のために作成されます:(これらは元の応答で表示され、ここではbrevityのために繰り返されません) 。
ReusableFormInterface.php
/src/Form
結論(パート1):ReusableFormBase.php
/src/Form
以上がDrupal 8カスタムプラグインタイプの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。