0

I'm attempting to attach a library to a CommerceCheckoutPane plugin. I first started with using $pane_form['#attached']['library'][] = 'commerce_paypal/express-checkout';. There were some errors in my commerce_paypal.libraries.yml file but once those were resolved the libaries still don't appear to be loaded.

Right now, my setup looks like:

commerce_paypal.libraries.yml:

paypal.checkout:
  remote: https://www.paypalobjects.com/api/checkout.js
  license:
    name: MIT
    gpl-compatible: true
  js:
    https://www.paypalobjects.com/api/checkout.js
express-checkout:
  js:
    js/paypal_ec.js
  dependencies:
    - commerce_paypal/paypal.checkout

Drupal\commerce_paypal\Plugin\Commerce\CheckoutPane\ExpressCheckout:

public function buildPaneForm(array $pane_form, FormStateInterface $form_state, array &$complete_form) {
  $pane_form['#theme'] = 'commerce_paypal_express_checkout';

  return $pane_form;
}

commerce-paypal-express-checkout.html.twig

{{ attach_library('commerce_paypal/express-checkout') }}

asdfas

js/paypal_ec.js:

console.log('here');

When I place the pane form I see asdfas but the JS is not added. I've checked the source, no sign of paypal_ec.js.

1

As usual, I was messing something up. JS libraries need : {}. Updating commerce_paypal.libraries.yml to this solves it:

paypal.checkout:
  remote: https://www.paypalobjects.com/api/checkout.js
  license:
    name: MIT
    gpl-compatible: true
  js:
    https://www.paypalobjects.com/api/checkout.js: {}
express-checkout:
  js:
    js/paypal_ec.js: {}
  dependencies:
    - commerce_paypal/paypal.checkout

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.