Home >Backend Development >PHP Tutorial >Drupal 8 Custom Plugin Types

Drupal 8 Custom Plugin Types

Joseph Gordon-Levitt
Joseph Gordon-LevittOriginal
2025-02-16 10:15:10201browse

Drupal 8's robust plugin system empowers backend developers with reusable functionality. This article (part one of two) details building a feature enabling custom forms with node entities, allowing configuration of node bundles to utilize various form types alongside node displays. New form types are easily defined by extending a provided base class. (For a complete code example, refer to this repository).

Drupal 8 Custom Plugin Types

This tutorial avoids in-depth plugin mechanics, assuming familiarity with the underlying theory. We'll construct our custom plugin type using two interfaces and six classes—a seemingly large number, but mostly straightforward boilerplate code. Part two will demonstrate attaching these reusable forms to nodes.

Key Concepts:

  • Drupal 8's plugin system facilitates reusable functionality, enabling custom forms for node entities. Node bundles can be configured to use multiple form types within node displays.
  • The plugin manager, essential for plugin discovery and loading, leverages Drupal's default base class for easy extension. All plugins must implement a defined interface.
  • Plugin definitions utilize annotations containing crucial information: the plugin subdirectory, the required interface, and the annotation class defining plugin properties.
  • A custom plugin type requires a base class extensible by all plugins. This class implements the interface and uses dependency injection for the form_builder service, essential for form construction. The plugin interacts with form classes; the next step is integrating these forms with node displays.

Plugin Manager:

The plugin manager, crucial for discovering and loading plugins, extends Drupal's DefaultPluginManager. Within the module's /src directory, ReusableFormManager.php contains:

<code class="language-php"><?php namespace Drupal\reusable_forms;

use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;

class ReusableFormsManager extends DefaultPluginManager {

  public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
    parent::__construct('Plugin/ReusableForm', $namespaces, $module_handler, 'Drupal\reusable_forms\ReusableFormPluginInterface', 'Drupal\reusable_forms\Annotation\ReusableForm');
    $this->alterInfo('reusable_forms_info');
    $this->setCacheBackend($cache_backend, 'reusable_forms');
  }
}</code>

This extends DefaultPluginManager, overriding the constructor. Key parameters define:

  • Plugin/ReusableForm: Plugin subdirectory.
  • Drupalreusable_formsReusableFormPluginInterface: Required plugin interface.
  • Drupalreusable_formsAnnotationReusableForm: Annotation class defining plugin properties.

An alter hook (reusable_forms_info) allows modules to modify plugin definitions, and the cache backend is configured.

Plugin Interface:

The interface (ReusableFormPluginInterface.php in /src) defines methods all plugins must implement:

<code class="language-php"><?php namespace Drupal\reusable_forms;

use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;

class ReusableFormsManager extends DefaultPluginManager {

  public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
    parent::__construct('Plugin/ReusableForm', $namespaces, $module_handler, 'Drupal\reusable_forms\ReusableFormPluginInterface', 'Drupal\reusable_forms\Annotation\ReusableForm');
    $this->alterInfo('reusable_forms_info');
    $this->setCacheBackend($cache_backend, 'reusable_forms');
  }
}</code>

getName() returns the plugin name; buildForm() accepts an entity and returns a render array for a form implementing Drupalreusable_formsFormReusableFormInterface. It extends PluginInspectionInterface and ContainerFactoryPluginInterface for added functionality and dependency injection.

Plugin Annotation:

The annotation class (ReusableForm.php in /src/Annotation) defines plugin properties:

<code class="language-php"><?php
namespace Drupal\reusable_forms;

use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Component\Plugin\PluginInspectionInterface;

interface ReusableFormPluginInterface extends PluginInspectionInterface, ContainerFactoryPluginInterface {

  public function getName();

  public function buildForm($entity);
}</code>

id, name, and form (the fully qualified form class name) are defined here.

Plugin Base Class:

The plugin base class (ReusableFormPluginBase.php in /src) provides defaults:

<code class="language-php"><?php
namespace Drupal\reusable_forms\Annotation;

use Drupal\Component\Annotation\Plugin;

/**
 * @Annotation
 */
class ReusableForm extends Plugin {

  public $id;
  public $name;
  public $form;
}</code>

This extends PluginBase, implements ReusableFormPluginInterface, and uses dependency injection for form_builder. getName() and buildForm() are implemented, using the form class specified in the annotation.

Form Interface and Base Class:

A simple form interface (ReusableFormInterface.php in /src/Form) and base class (ReusableFormBase.php in /src/Form) are created for consistency: (These are shown in the original response, and are not repeated here for brevity).

Conclusion (Part 1):

This first part sets up the custom plugin type, preparing it for integration with form classes. Part two will cover displaying these forms with nodes, involving node type configuration and form rendering within content view modes.

The above is the detailed content of Drupal 8 Custom Plugin Types. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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