Title: Overriding collapsible.js in Magento EE 2.1.1: A Comprehensive Guide
The collapsible.js file is a core JavaScript component used extensively throughout the Magento EE 2.x platform to provide interactive, user-friendly elements such as expandable and collapsible sections on the frontend of your store. If you’re looking to extend or override the activate method functionality present in this library, you’ve come to the right place.
Understanding the Challenge
You’ve already attempted three approaches to achieve this:
-
Using Mixins:
Your first attempt involved creating a mixin file and mapping it using the
mixinsproperty in your requirejs-config.js file. However, you encountered an exception due to the way Magento loads its scripts. -
Mapping with Map Property:
In your second try, you attempted to map the
collapsiblealias to a custom JavaScript file containing your override. While this approach should theoretically work based on RequireJS principles, Magento’s automatic configuration generation seems to be overriding your mapping setup. -
Extending the Widget:
Finally, you tried extending the existing
mage.collapsiblewidget using acollapsible-map.jsfile. This approach follows Magento’s recommended pattern for extending core widgets, but it failed due to an undefined reference issue with the$.mage.collapsibleobject.
The Root Cause: Magento’s RequireJS Configuration
The primary issue lies in how Magento automatically generates its requirejs-config.js file. Magento uses a dynamic configuration process that overrides any custom mapping or mixin configurations defined by third-party extensions during runtime. This is why your attempts at using the map and mixins properties weren’t successful.
The Solution: Using RequireJS Config Overrides
To successfully override the collapsible.js file, you need to follow a different approach that leverages Magento’s requirejs-config-overrides.js mechanism. Here’s how:
-
Create a New JavaScript File
In your module directory (e.g., app/code/
/ ), create a new folder named viewand then create another folder namedfrontend. Inside thefrontendfolder, create yet another folder namedrequirejs-config-overrides. -
Override the Configuration
Inside the
requirejs-config-overridesfolder, add a file namedmain.js. In this file, define your configuration override:
var config = {
map: {
'*': {
'mage/collapsible': '<vendor>/<module>/js/your-collapsible-override'
}
}
};
Replace <vendor> and <module> with the actual vendor and module names. The your-collapsible-override.js file should contain your custom activate method implementation.
-
Create Your Override File
Create a new folder named
jsinside thefrontend/requirejs-config-overridesdirectory, and add your override JavaScript file (your-collapsible-override.js). -
Implement Your Custom Logic
In the
your-collapsible-override.jsfile, extend the existingmage.collapsiblewidget:
define([
'jquery',
'Magento_Ui/js/modal/overlay'
], function ($) {
$.widget('Magento_Collapsible', $.mage.collapsible, {
activate: function () {
if (!this.options.disabled) {
// Your custom logic here
this.content.show();
this._open();
}
}
});
return $.Magento_Collapsible;
});
This approach ensures that your custom activate method is correctly loaded and replaces the default implementation without conflicts.
Conclusion
Overriding core Magento JavaScript components like collapsible.js requires a deep understanding of the platform’s RequireJS configuration process. By using the requirejs-config-overrides mechanism, you can successfully implement your desired functionality while avoiding the pitfalls associated with Magento’s automatic configuration generation.
Remember to always follow best practices when overriding core files:
- Avoid direct modifications to core files.
- Use overrides or mixins whenever possible.
- Keep your customizations separate from the core codebase.
By following this approach, you can effectively extend Magento’s JavaScript capabilities without compromising the stability and upgradeability of your store.
Happy coding! Don’t forget to explore more solutions and best practices on our blog at Laravel Company.