class ExampleMeatballSandwich

Same name in other branches
  1. 3.x modules/plugin_type_example/src/Plugin/Sandwich/ExampleMeatballSandwich.php \Drupal\plugin_type_example\Plugin\Sandwich\ExampleMeatballSandwich
  2. 8.x-1.x 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 attributes for class discovery, our meatball sandwich only needs to exist within the Plugin\Sandwich namespace, and provide a Sandwich attribute to be declared as a plugin. This is defined in \Drupal\plugin_type_example\SandwichPluginManager::__construct().

See the ExampleHamSandwich plugin for details about the Sandwich attribute.

Plugin annotation


@Sandwich(
  id = "meatball_sandwich",
  description = @Translation("Italian style meatballs drenched in irresistible marinara sauce, served on freshly baked bread."),
  calories = "1200"
)

Hierarchy

Expanded class hierarchy of ExampleMeatballSandwich

File

modules/plugin_type_example/src/Plugin/Sandwich/ExampleMeatballSandwich.php, line 30

Namespace

Drupal\plugin_type_example\Plugin\Sandwich
View 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

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
ExampleMeatballSandwich::$day protected property The day the sandwich is ordered.
ExampleMeatballSandwich::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
ExampleMeatballSandwich::description public function Provide a description of the sandwich. Overrides SandwichBase::description
ExampleMeatballSandwich::order public function Place an order for a sandwich. Overrides SandwichBase::order
ExampleMeatballSandwich::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct
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.
SandwichBase::calories public function Provide the number of calories per serving for the sandwich. Overrides SandwichInterface::calories
StringTranslationTrait::$stringTranslation protected property The string translation service. 3
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.