1

I am attempting to use a pre-configured instance of a plugin. The plugin is of type @CommercePaymentGateway.

I am currently trying to use something like:

/** @var PaymentGatewayManager $manager */
$manager = \Drupal::service('plugin.manager.commerce_payment_gateway');
$manager->getInstance(['module' => 'commerce_paypal']);

But I'm getting the error Error: Call to a member function getInstance() on null. However, using kint(), $manager is clearly an instance of Drupal\commerce_payment\PaymentGatewayManager.

I see what I think is the instance configuration in the config table under commerce_payment.commerce_payment_gateway.paypal_ec. But that paypal_ec part is specific to my site.

How do I get an instance of a plugin?

0

It turns out, in my case, I'm asking the wrong question. All that I need is the parent config entity (@bojanz pointed this out in slack), so I was over-complicating the issue:

$gateway = \Drupal::entityTypeManager()
  ->getStorage('commerce_payment_gateway')->loadByProperties([
    'plugin' => 'paypal_express_checkout',
]);

This will return an array of gateways, get a specific one:

// Get the first one
$plugin = reset($gateway);
$plugin = $plugin->getPlugin();

Now you have the $plugin to be used elsewhere.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.