Presented by: Tom Friedhof
...around since Drupal 4.7
ActiveLAMP partners with organizations to develop compelling digital experiences that solve specific organizational goals.
Not a deep dive of the plugin system:
Basic OOP knowledge
Plugins are small pieces of functionality that are swappable. Plugins that perform similar functionality are of the same plugin type.https://www.drupal.org/developing/api/8/plugins
I want to build a webpage that shows all the open tasks for a project, and the tasks exist in an external system (i.e. Jira)
I can write a module to do that!
jira_tasklistPlugins are small pieces of functionality that are swappable. Plugins that perform similar functionality are of the same plugin type.
Object interfaces allow you to create code which specifies which methods a class must implement, without having to define how these methods are handled.
/**
* Defines an interface for for interacting with project management systems.
*/
interface ProjectManagementInterface {
/**
* Authenticate with the the PM system.
*/
public function auth();
/**
* Return a task list from PM system.
*/
public function getTasks();
}
/**
* Get stuff from Jira.
*/
class JiraTaskList implements ProjectManagementInterface {}
/**
* Get stuff from Trello.
*/
class TrelloTaskList implements ProjectManagementInterface {}
tasklist.services.yml
services:
tasklist.pm_system:
class: Drupal\tasklist\JiraTaskList
MyModuleController.php
/**
* Module Controller.
*/
class MyController extends ControllerBase {
function index() {
$taskList = \Drupal::getContainer()->get('tasklist.pm_system');
...
}
}
BTW... don't access the service container this way.
Plugins are much like PHP native interfaces with a little extra: the plugin system can discover every implementation of an interface (the default is magic namespacing), deals with metadata (by default this is provided by annotations) and provides a factory for the plugin classes.https://www.drupal.org/node/1637614
The Drupal 8 plugin system provides a way to discover plugins within modules, grab metadata about the plugin, and provide a method of instantiating the plugin class when needed.
D8 needs to know:
Start by extending a base class: DefaultPluginManager
/**
* Project management system plugin manager.
*/
class ProjectManagementManager extends DefaultPluginManager {
/**
* Constructs an ProjectManagementManager object.
*
* @param \Traversable $namespaces
* An object that implements \Traversable which contains the root paths
* keyed by the corresponding namespace to look for plugin implementations,
* @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
* Cache backend instance to use.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler to invoke the alter hook with.
*/
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
parent::__construct('Plugin/ProjectManagement', $namespaces, $module_handler, 'Drupal\projectmanagement\ProjectManagementInterface', 'Drupal\projectmanagement\Annotation\ProjectManagement');
}
}
use Drupal\Component\Annotation\Plugin;
/**
*
* @Annotation
*/
class ProjectManagment extends Plugin {
/**
* The plugin name.
*
* @var string
*/
public $name;
/**
* The plugin description.
*
* @var string
*/
public $description;
}
/**
* Define the Jira plugin.
*
* @ProjectManagment(
* id = "jira"
* )
*/
class Jira {}
/**
* Defines an interface for for interacting with project management systems.
*/
interface ProjectManagementInterface extends PluginInspectionInterface {
/**
* Authenticate with the the PM system.
*/
public function auth();
/**
* Return a task list from PM system.
*/
public function getTasks();
}
class ProjectManagementBase extends PluginBase implements ProjectManagmentInterface {
}
/**
* Define the Jira plugin.
*
* @ProjectManagment(
* id = "jira",
* label = "Atlassian Jira"
* )
*/
class Jira extends ProjectManagementBase {
public function auth() {}
public function getTasks() {}
}
Notice that the metadata is baked into the same plugin file
Send me an email:
tom@activelamp.com