diff --git a/plugin_type_example/src/Plugin/Sandwich/ExampleMeatballSandwich.php b/plugin_type_example/src/Plugin/Sandwich/ExampleMeatballSandwich.php index 505e53c..9df63c7 100644 --- a/plugin_type_example/src/Plugin/Sandwich/ExampleMeatballSandwich.php +++ b/plugin_type_example/src/Plugin/Sandwich/ExampleMeatballSandwich.php @@ -2,8 +2,11 @@ namespace Drupal\plugin_type_example\Plugin\Sandwich; -use Drupal\plugin_type_example\SandwichBase; +use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\StringTranslation\StringTranslationTrait; +use Drupal\Core\StringTranslation\TranslationInterface; +use Drupal\plugin_type_example\SandwichBase; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Provides a meatball sandwich. @@ -29,9 +32,10 @@ use Drupal\Core\StringTranslation\StringTranslationTrait; * calories = "1200" * ) */ -class ExampleMeatballSandwich extends SandwichBase { - // Include Drupal\Core\StringTranslation\StringTranslationTrait in order to - // so we can use $this->t() to translate strings in our plugin. +class ExampleMeatballSandwich extends SandwichBase implements ContainerFactoryPluginInterface { + + // Use Drupal\Core\StringTranslation\StringTranslationTrait to define + // $this->t() for string translations in our plugin. use StringTranslationTrait; /** @@ -51,23 +55,35 @@ class ExampleMeatballSandwich extends SandwichBase { /** * {@inheritdoc} */ - public function __construct(array $configuration, $plugin_id, $plugin_definition) { + 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); } /** - * Place an order for a sandwich. - * - * This is just an example method on our plugin that we can call to get - * something back. - * - * @param array $extras - * Array of extras to include with this order. - * - * @return string - * A description of the sandwich ordered. + * {@inheritdoc} */ public function order(array $extras) { $ingredients = array('meatballs', 'irresistible marinara sauce'); diff --git a/plugin_type_example/src/SandwichInterface.php b/plugin_type_example/src/SandwichInterface.php index 98a32bf..e8a5fe3 100644 --- a/plugin_type_example/src/SandwichInterface.php +++ b/plugin_type_example/src/SandwichInterface.php @@ -40,6 +40,9 @@ interface SandwichInterface { /** * Place an order for a sandwich. * + * This is just an example method on our plugin that we can call to get + * something back. + * * @param array $extras * An array of extra ingredients to include with this sandwich. *