class ExampleMeatballSandwich in Examples for Developers 3.x
Same name and namespace in other branches
- 8 plugin_type_example/src/Plugin/Sandwich/ExampleMeatballSandwich.php \Drupal\plugin_type_example\Plugin\Sandwich\ExampleMeatballSandwich
Provides a meatball sandwich.
Because the plugin manager class for our plugins uses annotated class discovery, our meatball sandwich only needs to exist within the Plugin\Sandwich namespace, and provide a Sandwich annotation to be declared as a plugin. This is defined in \Drupal\plugin_type_example\SandwichPluginManager::__construct().
The following is the plugin annotation. This is parsed by Doctrine to make the plugin definition. Any values defined here will be available in the plugin definition.
This should be used for metadata that is specifically required to instantiate the plugin, or for example data that might be needed to display a list of all available plugins where the user selects one. This means many plugin annotations can be reduced to a plugin ID, a label and perhaps a description.
Plugin annotation
@Sandwich(
id = "meatball_sandwich",
description = @Translation("Italian style meatballs drenched in irresistible marinara sauce, served on freshly baked bread."),
calories = "1200"
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\plugin_type_example\SandwichBase implements SandwichInterface
- class \Drupal\plugin_type_example\Plugin\Sandwich\ExampleMeatballSandwich implements ContainerFactoryPluginInterface uses StringTranslationTrait
- class \Drupal\plugin_type_example\SandwichBase implements SandwichInterface
Expanded class hierarchy of ExampleMeatballSandwich
File
- modules/
plugin_type_example/ src/ Plugin/ Sandwich/ ExampleMeatballSandwich.php, line 35
Namespace
Drupal\plugin_type_example\Plugin\SandwichView source
class ExampleMeatballSandwich extends SandwichBase implements ContainerFactoryPluginInterface {
// Use Drupal\Core\StringTranslation\StringTranslationTrait to define
// $this->t() for string translations in our plugin.
use StringTranslationTrait;
/**
* The day the sandwich is ordered.
*
* Since meatball sandwiches have a special behavior on Sundays, and since we
* want to test that behavior on days other than Sunday, we have to store the
* day as a property so we can test it.
*
* This is the string representation of the day of the week you get from
* date('D').
*
* @var string
*/
protected $day;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
// This class needs to translate strings, so we need to inject the string
// translation service from the container. This means our plugin class has
// to implement ContainerFactoryPluginInterface. This requires that we make
// this create() method, and use it to inject services from the container.
// @see https://www.drupal.org/node/2012118
$sandwich = new static($configuration, $plugin_id, $plugin_definition, $container
->get('string_translation'));
return $sandwich;
}
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, TranslationInterface $translation) {
// Store the translation service.
$this
->setStringTranslation($translation);
// Store the day so we can generate a special description on Sundays.
$this->day = date('D');
// Pass the other parameters up to the parent constructor.
parent::__construct($configuration, $plugin_id, $plugin_definition);
}
/**
* {@inheritdoc}
*/
public function order(array $extras) {
$ingredients = [
'meatballs',
'irresistible marinara sauce',
];
$sandwich = array_merge($ingredients, $extras);
return 'You ordered an ' . implode(', ', $sandwich) . ' sandwich. Enjoy!';
}
/**
* {@inheritdoc}
*/
public function description() {
// We override the description() method in order to change the description
// text based on the date. On Sunday we only have day old bread.
if ($this->day == 'Sun') {
return $this
->t("Italian style meatballs drenched in irresistible marinara sauce, served on day old bread.");
}
return parent::description();
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ExampleMeatballSandwich:: |
protected | property | The day the sandwich is ordered. | |
ExampleMeatballSandwich:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
|
ExampleMeatballSandwich:: |
public | function |
Provide a description of the sandwich. Overrides SandwichBase:: |
|
ExampleMeatballSandwich:: |
public | function |
Place an order for a sandwich. Overrides SandwichBase:: |
|
ExampleMeatballSandwich:: |
public | function |
Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase:: |
|
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
2 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
SandwichBase:: |
public | function |
Provide the number of calories per serving for the sandwich. Overrides SandwichInterface:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |