class SandwichBase
Same name in other branches
- 3.x modules/plugin_type_example/src/SandwichBase.php \Drupal\plugin_type_example\SandwichBase
- 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
- class \Drupal\plugin_type_example\SandwichBase extends \Drupal\Component\Plugin\PluginBase implements \Drupal\plugin_type_example\SandwichInterface
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 plugin_type_example/
src/ Plugin/ Sandwich/ ExampleHamSandwich.php - ExampleMeatballSandwich.php in plugin_type_example/
src/ Plugin/ Sandwich/ ExampleMeatballSandwich.php
File
-
plugin_type_example/
src/ SandwichBase.php, line 30
Namespace
Drupal\plugin_type_exampleView 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 |
---|---|---|---|---|---|
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 |