Hopefully I'm asking the right question here, but I may have some wrong assumptions so apologies in advance. NOTE: Feel free to link to documents if there are any that explain this distinction!

I've been reading up on D8 and I was wondering if someone could explain the distinction between services and plugins? For instance, BreadcrumbManager is a core service; BlockBase is a plugin. In both cases it looks like you define the class, but with differences:

Services implement interfaces and are plugged into the global service container via a YML file
Plugins extend base classes and are autodiscovered (their 'registration' being defined in annotations rather than hook_info() of yore)

So if this is correct - what is the further distinction between these two things? Why is BreadcrumbManager specifically a service and not a plugin? Or vice versa for BlockBase? Can you give some more examples of services and plugins, and some rationale for why?

Comments

bruvers’s picture

This page sums it up pretty good:

Plugins implement different behaviors via a common interface.

For example, think of image transformations. Common image transformations are scale, crop, desaturate, and so on. Each transformation type acts in the same way on the same data - it accepts an image file, performs a transformation, and then returns an altered image. However, each effect is very different.

Services provide the same functionality, and are interchangeable, differing only in their internal implementation.

Think about a cache. A cache should provide get, 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.

Exception

If you expose a UI through which people can configure or select which implementation they want, you should use the plugin system - even if it is mostly the implementation details only which vary.