1

After have read some link i'm still confused about the difference between service and plugin.

I am not speaking about plugin like block or Field formater etc...

i would like to take an example :

I have create a module for add paiement feature to my site.

The question is why use service and not plugin ?

SERVICE

This module has an interface, every paiement type has a setPaiement method.

This is my module :

enter image description here

As you can see i have the interface, and every paiement Type like Paybox, Paypal class implement this interface.

paiement.service.yml

services:
  paybox_services:
    class: Drupal\paiement\Paybox
    arguments: ['@config.factory','@request_stack','@phpmailer_services']

  paypal_services:
    class: Drupal\paiement\Paypal
    arguments: ['@config.factory']

  stripe_services:
    class: Drupal\paiement\Stripe
    arguments: ['@config.factory','@request_stack','@phpmailer_services','@current_user']

PLUGIN

With the plugin method, i ll do the same, but the implementation ll change on the architecture folder.

The main difference is when i call the service or the plugin :

For the service

i ll inject it in the construct where i need it.

For the plugin

i ll use the plugin.manager.archiver where i need it.

So what is the good way to add feature like this ? Plugin or Service ?

Or maybe i miss something.

  • I think this is too broad to be answered and this depends on the features you need. For example how you discover new services. Now you need to know the service name. You could use tagged services to auto discover them, but plugins provide much more, annotation based discovery, altering discovered plugins and you can provide configuration for plugins. – 4k4 Jun 1 '18 at 8:56
  • um i see, for this module i have create a config form for the different paeiment ( url callback, API key etc..). i ll take a look on plugin configuration. – Kevin Jun 1 '18 at 9:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.