7

I am developing a custom module for a payment method in magento 2. Currently I am using cc-form.html from the vendor directory and the module is working fine. see below path.

vendor/magento/module-payment/view/frontend/web/template/payment/cc-form.html

Is there any way to override the HTML file?

TylerH
21.3k84 gold badges84 silver badges121 bronze badges
asked May 25, 2016 at 7:06

1 Answer 1

18

Yes, there is. You can look in pub static to see how path to static asset constructed.

How it works

Every asset is accessible from the page by itenter code heres "RequireJS ID". It similar to real path, but varied.

For example file http://magento.vg/static/adminhtml/Magento/backend/en_US/Magento_Theme/favicon.ico.

It's real path is /app/code/Magento/Theme/view/adminhtml/web/favicon.ico.
It's RequireJS ID is Magento_Theme/favicon.ico. This means that file could be accessible via require("text!Magento_Theme/favicon.ico") or similar command.

You can find that RequireJS ID consist with module name and useful part of path (after folder web).

How can I replace a file

So you have file
vendor/magento/module-payment/view/frontend/web/template/payment/cc-form.html

On the page it loaded with src as
http://magento.vg/static/frontend/Magento/luma/en_US/Magento_Payment/template/payment/cc-form.html

So its RequireJS ID is
Magento_Payment/template/payment/cc-form.html

Side note: Inside UI components stuff it equals to Magento_Payment/payment/cc-form. Words "template" and ".html" are added automatically.

And now you can replace this file for application via RequireJS config

var config = {
 "map": {
 "*": {
 "Magento_Payment/template/payment/cc-form.html": 
 "<OwnBrand>_<OwnModule>/template/payment/cc-form.html"
 }
 }
};

This code snippet you place in requirejs-config.js file in your module. That is all.

answered May 26, 2016 at 15:10
Sign up to request clarification or add additional context in comments.

2 Comments

An additional note of use: in my case the template was for an admin UI component, which had a path map included like so: 'ui/template': 'Magento_Ui/templates' (see Composer-based file at vendor/magento/module-ui/view/base/requirejs-config.js). Therefore, in my re-map entry, the target path (left side) had to be changed from Magento_Ui/template/form/element/media.html to ui/template/form/element/media.html.
thank u so much

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.