這個Drupal 8模塊創建可重複使用的表單,可以連接到不同的節點束。 讓我們構建一個簡單但功能強大,可重複使用的形式系統。 目標是在每個節點頁面上輕鬆加載一個表單,選擇每個節點捆綁包的表單類型。
>
接下來,我們將配置核心節點類型以利用這些插件並在節點顯示過程中渲染適當的表單。 首先,讓我們創建一個簡單的ReusableForm
插件。
ReusableForm
密鑰概念:
>
> reusableform插件:ReusableForm
>第三方設置:在模塊的>目錄中,創建:
>
src/Form
這擴展了我們的基本表單,實現了所需的方法。 BasicForm.php
方法利用基類邏輯。 提交處理將供以後實施。
<?php namespace Drupal\reusable_forms\Form; use Drupal\Core\Form\FormStateInterface; class BasicForm extends ReusableFormBase { public function getFormId() { return 'basic_form'; } public function buildForm(array $form, FormStateInterface $form_state) { $form = parent::buildForm($form, $form_state); return $form; } public function submitForm(array &$form, FormStateInterface $form_state) { // Handle form submission. } }
中創建插件:buildForm
>
註釋定義了插件的ID,名稱和關聯的表單類。 src/Plugin/ReusableForm/BasicForm.php
>
<?php namespace Drupal\reusable_forms\Plugin\ReusableForm; use Drupal\reusable_forms\ReusableFormPluginBase; /** * @ReusableForm( * id = "basic_form", * name = @Translation("Basic Form"), * form = "Drupal\reusable_forms\Form\BasicForm" * ) */ class BasicForm extends ReusableFormPluginBase {}
>要訪問和加載插件,我們將使插件管理器成為服務。在
中:
接下來,更改節點類型編輯表單():reusable_forms.services.yml
services: plugin.manager.reusable_forms: class: Drupal\reusable_forms\ReusableFormsManager parent: default_plugin_manager
>
reusable_forms.module
use Drupal\Core\Form\FormStateInterface; use Drupal\node\NodeTypeInterface; function reusable_forms_form_node_type_form_alter(&$form, FormStateInterface $form_state) { // ... (Code to add checkbox and radios for enabling and selecting forms) ... $form['#entity_builders'][] = 'reusable_forms_form_node_type_form_builder'; } function reusable_forms_form_node_type_form_builder($entity_type, NodeTypeInterface $type, &$form, FormStateInterface $form_state) { // ... (Code to save/unset third-party settings) ... }
#entity_builders
>
節點視圖reusable_forms.schema.yml
node.type.*.third_party.reusable_forms: type: mapping label: 'Reusable Forms' mapping: enabled: type: boolean label: 'Enable reusable forms' plugin: type: string label: 'Form Plugin'
>
>定義偽字段,> reusable_forms.module
呈現所選表單。
use Drupal\node\Entity\NodeType; use Drupal\Core\Entity\Display\EntityViewDisplayInterface; use Drupal\Core\Entity\EntityInterface; function reusable_forms_entity_extra_field_info() { // ... (Code to add 'reusable_form' pseudo field to enabled node types) ... } function reusable_forms_node_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode, $langcode) { // ... (Code to render the form using the plugin manager) ... }
以上是Drupal 8第三方設置和偽場的詳細內容。更多資訊請關注PHP中文網其他相關文章!