1

I am employing the code below to check what deployment mode server is on. This is throwing the error. Please point out the issue with the usage of the object $this->_appState to get the the mode. Thanks.

public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Framework\App\State $appState,
        \Magento\Framework\Filesystem $fileSystem) {
    $this->_appState  = $appState->getMode();
    // Code to get the absolute path to the pub folder.
    $this->_pubPath = $fileSystem->getDirectoryRead(\Magento\Framework\App\Filesystem\DirectoryList::PUB)->getAbsolutePath();
    parent::__construct($context);
}

/**
 * getAllPagesJSContent() returns the custom JS contents to calling custom allpages.js.
 * This function checks if the deployment mode is production or not, and returns
 * pmm/custom/js/allpages.min.js or pmm/custom/js/allpages.js
 *
 * @param void
 * @return string file contents or empty
 */
public function getAllPagesJSContent() {
    echo $this->_appState;
    // Check current environment. If it is the production retrieve the allpages.min.js file.
    if ($this->_appState == "production") {
        echo "Hello from production 1";
        if (($custom_js_file_contents = $this->get_custom_js_file_contents('allpages.min.js')) !== FALSE) {
            return $custom_js_file_contents;
        }
    }

1 Answer 1

1

Please try the below code if it helps!

/** @var \Magento\Framework\App\State $_appState **/
protected $_appState;

public function __construct(
    ......
    \Magento\Framework\App\State $appState
    ......
)
{
    $this->_appState = $appState;
}

public function getCurrentMode() {

    // Check current environment
    if ($this->_appState->getMode() == \Magento\Framework\App\State::MODE_PRODUCTION) {
        die("Current Mode is Production");      
    }
}
2
  • Yes, thanks, I did the same thing finally leaving me wondering why $this->_appState = $appState->getMode(); shouldn't work. What makes it different from $this->_appState->getMode()? Commented May 17, 2020 at 6:39
  • Not sure about it, maybe issue with the object conversion to string. Commented May 17, 2020 at 7:13

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.