2

I've implemented swatch-renderer-mixin through require-js. It's working fine but I need to only run it when my module is enabled. What's the best way to check it in my mixin.js file? This is my requirejs-config file:

var config = {
    config: {
        mixins: {
            'Magento_Swatches/js/swatch-renderer': {
                'Vendor_Module/js/swatch-renderer-mixin': true
            },
            'mage/gallery/gallery': {
                'Vendor_Module/js/gallery-mixin': true
            }
        }
    }
};

2 Answers 2

0

Create the mixin as part of your custom module, then whenever the module is disabled your mixin will not load.

5
  • I've updated my answer with requirejs file code. Sorry, I don't understand how can I make mixin as part of my custom module. Can you elaborate a bit? Feb 18, 2021 at 9:56
  • Where are your JS files located, in the theme? If so you can move them over to your module and they will now be included in your module and not the theme, so when the module is disabled the files will not load. Does that help?
    – Ben Crook
    Feb 18, 2021 at 10:26
  • My JS files are in my module. and FYI, it's just like a dummy enable/disable you know, where we need to check isEnabled() at every php function. So disabling that doesn't effect JS files. What's a better way of disabling modules? Feb 18, 2021 at 10:31
  • When you say disabling do you mean disabling the whole module or a custom feature flag?
    – Ben Crook
    Feb 18, 2021 at 11:05
  • I mean disabling from configurations only, the field defined by system.xml and you know it has no effect on anything when we disable our module, unless we check enabled/disable above every module feature by getting configuration value of this field Feb 18, 2021 at 12:37
0

It Can be achieved by this way

Vendor\Module\etc\di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
     <type name="Magento\Framework\View\Page\Config\Renderer">
        <plugin name="Vendor_Module::DisableJsMixins" type="Vendor\Module\Plugin\View\Page\Config\Renderer" />
    </type>
</config>

Vendor\Module\Plugin\View\Page\Config\Renderer.php

<?php

namespace Vendor\Module\Plugin\View\Page\Config;

use Magento\Framework\App\CacheInterface;
use Magento\Framework\View\Asset\File;
use Magento\Framework\View\Page\Config;
use Magento\Framework\View\Page\Config\Renderer as MagentoRenderer;
use Vendor\Module\Helper\Data as helper;
use Magento\Framework\View\Asset\Repository;
use Magento\Framework\View\Asset\GroupedCollection; 

class Renderer 
{
    /**
     * @var Repository
     */
    private $assetRepo;

    /**
     * @var GroupedCollection
     */
    private $pageAssets;

    /**
     * @var helper
     */
    private $helper;

    public function __construct(
        helper $helper,
        Repository $assetRepo,
        GroupedCollection $pageAssets 
    ) {
        $this->helper = $helper;
        $this->assetRepo = $assetRepo;
        $this->pageAssets = $pageAssets; 
    }

    /**
     * Disable js mixins if module is disabled
     *
     * @param MagentoRenderer $subject
     * @param array $resultGroups
     *
     * @return array
     */
    public function beforeRenderAssets(MagentoRenderer $subject, $resultGroups = [])
    {
        if (!$this->helper->getStatus()) { 
            $file = 'Vendor_Module::js/moduledisable.js';
            $asset = $this->assetRepo->createAsset($file);
            $this->pageAssets->insert($file, $asset, 'requirejs/require.js');
            return [$resultGroups];
        }

        return [$resultGroups];
    }
}


Vendor/Module/view/frontend/web/js/moduledisable.js

window.vendor_module_disabled = true;

Now in your requirejs-config.js

var module_mixin_enabled = !window.vendor_module_disabled,
    config;
config = {
    config: {
        mixins: {
            'Magento_Swatches/js/swatch-renderer': {
                'Vendor_Module/js/swatch-renderer-mixin': module_mixin_enabled
            },
            'mage/gallery/gallery': {
                'Vendor_Module/js/gallery-mixin': module_mixin_enabled
            }
        }
    }
};

Your Answer

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

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