0

I'm in the process of porting an M1 module over to M2 and am trying to figure out the cleanest approach for conditionally mutating a component based on a system configuration value.

In Magento 1 I did the following:

I've read through Magento's "use custom JavaScript" but it doesn't seem to account for this use case.

The best way I can think of to do this is...

  • Add some dummy element to the document with a data property for the configuration value.
  • Mutate the widget and use jQuery to access the value from the document.

Feels pretty dirty and I'm wondering if there's a cleaner way?

1

You can pass the config value to your JS with something like this in your template file:

<script>
    require([
        'Your_Module/js/your-script'
    ], function(disabler){
        disabler(<?php echo $block->getConfigValue ?>);
    });
</script>

In the JS file:

define([],
function () {
    'use strict';

    return function disabler(configValue) {
        if (configValue === '1') {
            // do something
        }
    }
});

To get the config value in your block class,

public function getConfigValue()
{
    return $this->_scopeConfig->getValue(
        'config/value/path',
        \Magento\Store\Model\ScopeInterface::SCOPE_STORE
    );
}

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.