This Drupal 8 module creates reusable forms that can be attached to different node bundles. Let's build a simple, yet powerful, reusable form system. The goal is to easily load a form on each node page, selecting the form type per node bundle.
We've already created a custom plugin type, ReusableForm
, with a base plugin class for extension. Each plugin interacts with a form class defined in its annotation. Similarly, a base form class provides a foundation for new forms.
Next, we'll configure core node types to utilize these plugins and render the appropriate form during node display. First, let's create a simple ReusableForm
plugin.
Key Concepts:
ReusableForm
plugins.Creating the First Plugin
In the module's src/Form
directory, create 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. } }
This extends our base form, implementing required methods. The buildForm
method utilizes the base class logic. Submission handling is left for later implementation.
Now, create the plugin in 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 {}
The annotation defines the plugin's ID, name, and associated form class.
Node Type Configuration
To access and load plugins, we'll make the plugin manager a service. In reusable_forms.services.yml
:
services: plugin.manager.reusable_forms: class: Drupal\reusable_forms\ReusableFormsManager parent: default_plugin_manager
Next, alter the node type edit form (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) ... }
This adds a checkbox to enable reusable forms and a select list to choose a plugin. The #entity_builders
callback saves the selection in third-party settings.
Configuration Schema (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'
Node View
Create a pseudo field (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
defines the pseudo field, and hook_node_view
renders the selected form.
This completes the basic structure. Remember to clear caches after making changes. This detailed explanation provides a solid foundation for building more complex reusable form functionality in Drupal 8.
The above is the detailed content of Drupal 8 Third Party Settings and Pseudo-Fields. For more information, please follow other related articles on the PHP Chinese website!