Home > Backend Development > PHP Tutorial > Drupal 8 Third Party Settings and Pseudo-Fields

Drupal 8 Third Party Settings and Pseudo-Fields

Christopher Nolan
Release: 2025-02-16 13:14:09
Original
996 people have browsed it

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.

Drupal 8 Third Party Settings and Pseudo-Fields

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 Plugin: A custom plugin type allowing selection of forms per node bundle.
  • Form Class: Defines the structure and logic of each individual form.
  • Plugin Manager Service: Provides access to available ReusableForm plugins.
  • Third-Party Settings: Stores plugin selection per node type.
  • Pseudo Field: Allows rendering the selected form on the node display.

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.
  }
}
Copy after login

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 {}
Copy after login

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
Copy after login

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) ...
}
Copy after login

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'
Copy after login

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) ...
}
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template