这个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中文网其他相关文章!