Skip to main content
Extend or Override Contributed Plugins in our Custom Module in Drupal 8.

The basic Explanation of Drupal plugins is to extend the functionality of a particular module or Drupal subsystem. The module defines the interface for the functionality and other modules can create plugins with particular behaviors.
Plugins are grouped into Plugins Type, each generally defined by an interface. Each plugin type is managed by a Plugin manager Service, which uses a Plugin Discovery Method to discover provided plugins of that type and instantiate them using a Plugin Factory.

Example:

If I want to override the do Payment () method of Payment Gateway plugin in the commerce fund module and change 'status=>complete' to 'status=>finish'.

namespace Drupal\commerce_funds\Plugin\Commerce\PaymentGateway;
class BalanceGateway extends PaymentGatewayBase implements BalanceGatewayInterface, ContainerFactoryPluginInterface {
protected function doPayment(PaymentMethodInterface $payment_method, PaymentInterface $payment) {
       // field in dopayment method 
      'status' => 'Completed',
}
}

So there's no generic way that works for all plugins, however, you can find out the way that works for the particular plugin type that you want to override. Plugins are collected/managed by a service. So I would have a look in the commerce_funds.services.yml file and see if you can identify which class it is that will be managing the type of plugin that you wish to override.

Problem:


The problem here is that no one of the listed services in this file is associated with the payment gateway class.

Solution:


 Payment gateway plugins are provided by commerce_payment, within commerce itself. So have a look at commerce_payment.services.yml - which includes a payment gateway manager class ... which again, declares what hook to use here.

i.e. by following the same steps, for the appropriate plugin type, you can find the alter hook name from code.

For example, is it a doPayment Method plugin you want to override? if so, the first-class in that file Drupal\commerce_payment

\PaymentGatewayManager looks appropriate. In that class, its constructor declares an alter hook, which you can then use: 

hook_commerce_payment_gateway_info_alter ().

Final Step:

So Your Custom module Structure Will Be.
Custom_module is our module directory

  • Custom_module.info.yml
  • Custom_module.module
  • Src/Plugin/CustomModule
  • CustomModule.php

Now use the Existing class in CustomModule Plugin.

namespace Drupal\custom_module \Plugin;
use Drupal\commerce_funds\Plugin\Commerce\PaymentGateway;
Now extend the Contrib module class.
Class CustomModule extends PaymentGateway {
protected function doPayment(PaymentMethodInterface $payment_method, PaymentInterface $payment) {
       // field in dopayment method 
      'status' => 'Finished',
}
}

In .module file implement hook alter

function Custom_module_commerce_payment_gateway_info_alter(array &$info) {
  if (isset($info['funds_balance'])) {
    $info['funds_balance']['class'] = '\Drupal\ Custom_module \Plugin\CustomModule ';
  }

 

sajideen

Drupal Developer

Add new comment