class SandwichBase

Same name in other branches
  1. 4.0.x modules/plugin_type_example/src/SandwichBase.php \Drupal\plugin_type_example\SandwichBase

A base class to help developers implement their own sandwich plugins.

This is a helper class which makes it easier for other developers to implement sandwich plugins in their own modules. In SandwichBase we provide some generic methods for handling tasks that are common to pretty much all sandwich plugins. Thereby reducing the amount of boilerplate code required to implement a sandwich plugin.

In this case both the description and calories properties can be read from the @Sandwich annotation. In most cases it is probably fine to just use that value without any additional processing. However, if an individual plugin needed to provide special handling around either of these things it could just override the method in that class definition for that plugin.

We intentionally declare our base class as abstract, and don't implement the order() method required by \Drupal\plugin_type_example\SandwichInterface. This way even if they are using our base class, developers will always be required to define an order() method for their custom sandwich type.

Hierarchy

Expanded class hierarchy of SandwichBase

See also

\Drupal\plugin_type_example\Annotation\Sandwich

\Drupal\plugin_type_example\SandwichInterface

2 files declare their use of SandwichBase
ExampleHamSandwich.php in modules/plugin_type_example/src/Plugin/Sandwich/ExampleHamSandwich.php
ExampleMeatballSandwich.php in modules/plugin_type_example/src/Plugin/Sandwich/ExampleMeatballSandwich.php

File

modules/plugin_type_example/src/SandwichBase.php, line 30

Namespace

Drupal\plugin_type_example
View source
abstract class SandwichBase extends PluginBase implements SandwichInterface {
    
    /**
     * {@inheritdoc}
     */
    public function description() {
        // Retrieve the @description property from the annotation and return it.
        return $this->pluginDefinition['description'];
    }
    
    /**
     * {@inheritdoc}
     */
    public function calories() {
        // Retrieve the @calories property from the annotation and return it.
        return (double) $this->pluginDefinition['calories'];
    }
    
    /**
     * {@inheritdoc}
     */
    public abstract function order(array $extras);

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
PluginBase::$configuration protected property Configuration information passed into the plugin.
PluginBase::$pluginDefinition protected property The plugin implementation definition.
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 8
SandwichBase::calories public function Provide the number of calories per serving for the sandwich. Overrides SandwichInterface::calories
SandwichBase::description public function Provide a description of the sandwich. Overrides SandwichInterface::description 1
SandwichBase::order abstract public function Place an order for a sandwich. Overrides SandwichInterface::order 2