diff --git a/plugin_type_example/src/Annotation/Sandwich.php b/plugin_type_example/src/Annotation/Sandwich.php index 1b8f5a9..211fdf2 100644 --- a/plugin_type_example/src/Annotation/Sandwich.php +++ b/plugin_type_example/src/Annotation/Sandwich.php @@ -1,14 +1,5 @@ day = date('D'); + 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 @@ -49,12 +72,12 @@ class ExampleMeatballSandwich extends SandwichBase { } /** - * Override the parent class description method. We'll provide our own in this - * case. + * {@inheritdoc} */ public function description() { - // On Sunday we only have day old bread. - if (date('D') == 'Sun') { + // 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(); diff --git a/plugin_type_example/src/SandwichBase.php b/plugin_type_example/src/SandwichBase.php index 4f28909..8a1cfc8 100644 --- a/plugin_type_example/src/SandwichBase.php +++ b/plugin_type_example/src/SandwichBase.php @@ -5,7 +5,7 @@ namespace Drupal\plugin_type_example; use Drupal\Component\Plugin\PluginBase; /** - * Provides \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 @@ -19,10 +19,10 @@ use Drupal\Component\Plugin\PluginBase; * 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 skip 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. + * 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. * * @see \Drupal\plugin_type_example\Annotation\Sandwich * @see \Drupal\plugin_type_example\SandwichInterface @@ -30,20 +30,18 @@ use Drupal\Component\Plugin\PluginBase; abstract class SandwichBase extends PluginBase implements SandwichInterface { /** - * Retrieve the @description property from the annotation and return it. - * - * @return string + * {@inheritdoc} */ public function description() { + // Retrieve the @description property from the annotation and return it. return $this->pluginDefinition['description']; } /** - * Retrieve the @calories property from the annotation and return it. - * - * @return float + * {@inheritdoc} */ public function calories() { + // Retrieve the @calories property from the annotation and return it. return (float) $this->pluginDefinition['calories']; } diff --git a/plugin_type_example/src/SandwichInterface.php b/plugin_type_example/src/SandwichInterface.php index 6d6b0af..98a32bf 100644 --- a/plugin_type_example/src/SandwichInterface.php +++ b/plugin_type_example/src/SandwichInterface.php @@ -47,4 +47,5 @@ interface SandwichInterface { * Description of the sandwich that was just ordered. */ public function order(array $extras); + } diff --git a/plugin_type_example/src/SandwichPluginManager.php b/plugin_type_example/src/SandwichPluginManager.php index 9e68b9a..c6485c3 100644 --- a/plugin_type_example/src/SandwichPluginManager.php +++ b/plugin_type_example/src/SandwichPluginManager.php @@ -5,7 +5,6 @@ namespace Drupal\plugin_type_example; use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Plugin\DefaultPluginManager; -use Drupal\plugin_type_example\SandwichInterface; use Drupal\plugin_type_example\Annotation\Sandwich; /** @@ -41,12 +40,13 @@ class SandwichPluginManager extends DefaultPluginManager { // This tells the plugin manager to look for Sandwich plugins in the // 'src/Plugin/Sandwich' subdirectory of any enabled modules. This also // serves to define the PSR-4 subnamespace in which sandwich plugins will - // live. Example: Drupal\{module_name}\Plugin\Sandwich\MySandwichPlugin; + // live. Modules can put a plugin class in their own namespace such as + // Drupal\{module_name}\Plugin\Sandwich\MySandwichPlugin. $subdir = 'Plugin/Sandwich'; // The name of the interface that plugins should adhere to. Drupal will // enforce this as a requirement. If a plugin does not implement this - // interface, than Drupal will throw an error. + // interface, Drupal will throw an error. $plugin_interface = SandwichInterface::class; // The name of the annotation class that contains the plugin definition. diff --git a/plugin_type_example/src/Tests/PluginTypeExampleTest.php b/plugin_type_example/src/Tests/PluginTypeExampleTest.php index 90f75d2..6ac3eb8 100644 --- a/plugin_type_example/src/Tests/PluginTypeExampleTest.php +++ b/plugin_type_example/src/Tests/PluginTypeExampleTest.php @@ -3,6 +3,7 @@ namespace Drupal\plugin_type_example\Tests; use Drupal\simpletest\WebTestBase; +use Drupal\plugin_type_example\Plugin\Sandwich\ExampleHamSandwich; /** * Test the functionality of the Plugin Type Example module. @@ -32,7 +33,8 @@ class PluginTypeExampleTest extends WebTestBase { * Test the plugin manager can be loaded, and the plugins are registered. */ public function testPluginExample() { - $manager = \Drupal::service('plugin.manager.sandwich'); + /* @var $manager \Drupal\plugin_type_example\SandwichPluginManager */ + $manager = $this->container->get('plugin.manager.sandwich'); $sandwich_plugin_definitions = $manager->getDefinitions(); @@ -49,7 +51,18 @@ class PluginTypeExampleTest extends WebTestBase { // Create an instance of the ham sandwich plugin to check it works. $plugin = $manager->createInstance('ham_sandwich', array('of' => 'configuration values')); - $this->assertEqual(get_class($plugin), 'Drupal\plugin_type_example\Plugin\Sandwich\ExampleHamSandwich', 'The ham sandwich plugin is instantiated and of the correct class.'); + $this->assertEqual(get_class($plugin), ExampleHamSandwich::class, 'The ham sandwich plugin is instantiated and of the correct class.'); + + // Create a meatball sandwich so we can check it's special behavior on + // Sundays. + /* @var $meatball \Drupal\plugin_type_example\SandwichInterface */ + $meatball = $manager->createInstance('meatball_sandwich'); + // Set the $day property to 'Sun'. + $ref_day = new \ReflectionProperty($meatball, 'day'); + $ref_day->setAccessible(TRUE); + $ref_day->setValue($meatball, 'Sun'); + // Check the special description on Sunday. + $this->assertEqual($meatball->description(), 'Italian style meatballs drenched in irresistible marinara sauce, served on day old bread.'); } /**