Not able to override collapsible.js in magento 2

Not able to override collapsible.js in magento 2

July 14, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Not able to override collapsible.js in magento 2

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:

  1. Using Mixins:

    Your first attempt involved creating a mixin file and mapping it using the mixins property in your requirejs-config.js file. However, you encountered an exception due to the way Magento loads its scripts.

  2. Mapping with Map Property:

    In your second try, you attempted to map the collapsible alias 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.

  3. Extending the Widget:

    Finally, you tried extending the existing mage.collapsible widget using a collapsible-map.js file. This approach follows Magento’s recommended pattern for extending core widgets, but it failed due to an undefined reference issue with the $.mage.collapsible object.

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:

  1. Create a New JavaScript File

    In your module directory (e.g., app/code//), create a new folder named view and then create another folder named frontend. Inside the frontend folder, create yet another folder named requirejs-config-overrides.

  2. Override the Configuration

    Inside the requirejs-config-overrides folder, add a file named main.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.

  1. Create Your Override File

    Create a new folder named js inside the frontend/requirejs-config-overrides directory, and add your override JavaScript file (your-collapsible-override.js).

  2. Implement Your Custom Logic

    In the your-collapsible-override.js file, extend the existing mage.collapsible widget:

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.

Magento 2 Themes Configuration Errors

Our network