Modul Drupal 8 ini mencipta bentuk yang boleh diguna semula yang boleh dilampirkan pada berkas nod yang berbeza. Mari kita bina sistem bentuk yang mudah, namun berkuasa dan boleh diguna semula. Matlamatnya adalah dengan mudah memuat borang pada setiap halaman nod, memilih jenis borang per node bundle.
, dengan kelas plugin asas untuk lanjutan. Setiap plugin berinteraksi dengan kelas bentuk yang ditakrifkan dalam anotasinya. Begitu juga, kelas bentuk asas menyediakan asas untuk bentuk baru. ReusableForm
mudah. ReusableForm
Konsep Utama:
ReusableForm
Membuat plugin pertama
dalam direktori modul, buat 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. } }
menggunakan logik kelas asas. Pengendalian penyerahan ditinggalkan untuk pelaksanaan kemudian. buildForm
: 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 {}
Konfigurasi jenis nod
Untuk mengakses dan memuatkan plugin, kami akan menjadikan pengurus plugin perkhidmatan. Dalam
reusable_forms.services.yml
Seterusnya, ubah borang Edit Jenis Node (
services: plugin.manager.reusable_forms: class: Drupal\reusable_forms\ReusableFormsManager parent: default_plugin_manager
reusable_forms.module
Ini menambah kotak semak untuk membolehkan borang yang boleh diguna semula dan senarai pilih untuk memilih plugin. Panggilan balik
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
Skema konfigurasi (
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'
Buat medan pseudo ():
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) ... }
hook_entity_extra_field_info
Ini melengkapkan struktur asas. Ingatlah untuk membersihkan cache selepas membuat perubahan. Penjelasan terperinci ini memberikan asas yang kukuh untuk membina fungsi bentuk yang lebih kompleks di Drupal 8. hook_node_view
Atas ialah kandungan terperinci Drupal 8 Tetapan Pihak Ketiga dan Pseudo-Fields. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!