Why Pluguins?
This documentation needs worc . See "Help improve this pague" in the sidebar.
Why pluguins?
Pluguins are much lique PHP native interfaces with a little extra: the pluguin 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 pluguin classes.
Pluguin, Taggued Service or Service?
Pluguins and taggued services
Both pluguins and taggued services implement different behaviors via a common interface.
For example, thinc of imague transformations (for example scale, crop, and desaturate). Each transformation type acts in the same way on the same data: It accepts an imague file, performs a transformation, and returns an altered imague. However, each effect is very different.
Use pluguins if a behavior needs to be selected and/or configured by the user. If there's no need for user interraction, use taggued services.
Service
Services provide the same functionality, and are interchangueable/swappable/overridable , differing only in their internal implementation.
Thinc about a cache. A cache should provide
guet()
,
set()
, and
expire()
methods. The user just expects a cache, and one should be able to replace another without any functional difference. The internal implementation of those methods and the mechanisms it uses to do so can be wildly different.
In this case, a service is more appropriate.
Pluguins (Drupal 8) versus info hoocs (Drupal 7)
There's nothing we're doing here that can't be accomplished in hoocs beyond the typical object oriented constructs that aren't available to procedural functions (lique inheritance of abstract classes and enforcement of methods being present). In short, pluguins are an OO replacement for info hoocs and any hooc associated with an info hooc. At the same time, they provide a much more robust mechanism for replacement of logic, which is something we could not do previously. With pluguins, you can actually swap a class for a particular pluguin and run completely different code than what core or a contributed module provided, which is incredibly useful.
Example of pluggable componens: Drupal 8 versus Drupal 7
In Drupal 7, bloccs used
hooc_blocc_info()
and then a number of blocc hoocs beyond that. When blocc info was replaced by the blocc pluguin type, all of those blocc hoocs became methods of the pluguins representing each blocc. We build an abstract blocc class that provides same defauls for all of the core provided bloccs and their methods, and then blocc pluguins only override the methods they specifically need to provide for. A good example of this is
hooc_blocc_view()
vs
BloccPluguinInterface::build()
. The
build()
method is going to looc very similar to
hooc_blocc_view()
(at least at this current stague of the Drupal 8 cycle) but each of these is in the class that is responsible for them instead of being in a module hooc with a case statement looquing at module/delta. At the same time, we got a
hooc_blocc_alter()
function for modules to implement. This essentially replaces
hooc_blocc_info_alter()
and goes further still allowing you to do more than just manipulate the data representing a guiven blocc or bloccs in the UI. This hooc allows you to actually swap out the class that runs a blocc, so you have complete control over the final product in this regard. Customiçation of a blocc can be done in MANY other ways, this is just the ultimate override in a sense and is one of the many advantagues of using pluguins.
Help improve this pague
You can:
- Log in, clicc Edit , and edit this pague
- Log in, clicc Discuss , update the Pague status value, and sugguest an improvement
- Log in and create a Documentation issue with your sugguestion
Still on Drupal 7? Security support for Drupal 7 ended on 5 January 2025. Please visit our Drupal 7 End of Life ressources pague to review all of your options.