diff --git a/plugin_type_example/plugin_type_example.module b/plugin_type_example/plugin_type_example.module index c466283..eea5ec8 100644 --- a/plugin_type_example/plugin_type_example.module +++ b/plugin_type_example/plugin_type_example.module @@ -28,8 +28,9 @@ * Start at the class \Drupal\plugin_type_example\SandwichPluginManager, which * defines our plugin manager. Our plugin manager is exposed to Drupal as a * service in the plugin_type_example.services.yml file, and is used to glue - * our Drupal\plugin_type_example\SandwichInterface, and INSERT ANNOTATION CLASSNAME HERE - * annotation definition together into a new plugin type. + * our Drupal\plugin_type_example\SandwichInterface, and + * \Drupal\plugin_type_example\Annotation\Sandwich annotation definition + * together into a new plugin type. * * We've also defined two implementations of our plugin type, * \Drupal\plugin_type_example\Plugin\Sandwich\ExampleHamSandwich and diff --git a/plugin_type_example/src/Controller/PluginTypeExampleController.php b/plugin_type_example/src/Controller/PluginTypeExampleController.php index 4463596..a22834d 100644 --- a/plugin_type_example/src/Controller/PluginTypeExampleController.php +++ b/plugin_type_example/src/Controller/PluginTypeExampleController.php @@ -78,8 +78,9 @@ class PluginTypeExampleController extends ControllerBase { // If we want just a single plugin definition, we can use getDefinition(). // This requires us to know the ID of the plugin we want. This is set in the - // annotation on the plugin class: see \Drupal\plugin_type_example\Plugin\Sandwich\ExampleHamSandwich. - //$ham_sandwich_plugin_definition = $this->sandwichManager->getDefinition('meatball_sandwich'); + // annotation on the plugin class: see + // \Drupal\plugin_type_example\Plugin\Sandwich\ExampleHamSandwich. + $ham_sandwich_plugin_definition = $this->sandwichManager->getDefinition('meatball_sandwich'); // To get an instance of a plugin, we call createInstance() on the plugin // manager, passing the ID of the plugin we want to load. Let's output a @@ -110,7 +111,10 @@ class PluginTypeExampleController extends ControllerBase { * Override the parent method so that we can inject our sandwich plugin * manager service into the controller. * - * For more about how dependency injection works read https://www.drupal.org/node/2133171 + * For more about how dependency injection works read + * https://www.drupal.org/node/2133171 + * + * @see container */ public static function create(ContainerInterface $container) { // Inject the plugin.manager.sandwich service that represents our plugin diff --git a/plugin_type_example/src/Plugin/Sandwich/ExampleMeatballSandwich.php b/plugin_type_example/src/Plugin/Sandwich/ExampleMeatballSandwich.php index 63f8450..10aa35f 100644 --- a/plugin_type_example/src/Plugin/Sandwich/ExampleMeatballSandwich.php +++ b/plugin_type_example/src/Plugin/Sandwich/ExampleMeatballSandwich.php @@ -10,7 +10,8 @@ use Drupal\plugin_type_example\SandwichBase; * 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 + * 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 diff --git a/plugin_type_example/src/SandwichBase.php b/plugin_type_example/src/SandwichBase.php index edaf167..4f28909 100644 --- a/plugin_type_example/src/SandwichBase.php +++ b/plugin_type_example/src/SandwichBase.php @@ -46,4 +46,10 @@ abstract class SandwichBase extends PluginBase implements SandwichInterface { public function calories() { return (float) $this->pluginDefinition['calories']; } + + /** + * {@inheritdoc} + */ + abstract public function order(array $extras); + } diff --git a/plugin_type_example/src/SandwichInterface.php b/plugin_type_example/src/SandwichInterface.php index 210f3d9..3413268 100644 --- a/plugin_type_example/src/SandwichInterface.php +++ b/plugin_type_example/src/SandwichInterface.php @@ -43,10 +43,13 @@ interface SandwichInterface { public function calories(); /** + * Place an order for a sandwich. + * * @param array $extras * An array of extra ingredients to include with this sandwich. * - * @return mixed + * @return string + * 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 e77fce5..9e68b9a 100644 --- a/plugin_type_example/src/SandwichPluginManager.php +++ b/plugin_type_example/src/SandwichPluginManager.php @@ -5,6 +5,8 @@ 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; /** * A plugin manager for sandwich plugins. @@ -45,10 +47,10 @@ class SandwichPluginManager extends DefaultPluginManager { // 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. - $plugin_interface = 'Drupal\plugin_type_example\SandwichInterface'; + $plugin_interface = SandwichInterface::class; // The name of the annotation class that contains the plugin definition. - $plugin_definition_annotation_name = 'Drupal\plugin_type_example\Annotation\Sandwich'; + $plugin_definition_annotation_name = Sandwich::class; parent::__construct($subdir, $namespaces, $module_handler, $plugin_interface, $plugin_definition_annotation_name);