3
votes

First of all I was extending the module for contact us and I wanted to check on backend if its enabled or not using ScopeConfig but I've encountered this problem. I already tried deleting the generated files and run setup upgrade command.

On my assumption I think I am lacking something on my codes

CODE:

<?php
namespace SCI\Form\Controller\Index;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface;

class Post extends \Magento\Contact\Controller\Index\Post
{
    protected $scopeConfig;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        ScopeConfigInterface $scopeConfig,
        array $data = []
    ){
        parent::__construct($context,$data);
        $this->_scopeConfig = $scopeConfig;
        $this->_storeManager = $storeManager;

    }

    public function ifEnabled(){
        return $this->_scopeConfig->getValue('extended_contact/config/extended_contact_enabled',ScopeInterface::SCOPE_STORE);
    }

    public function execute()
    {
        die($this->ifEnabled());

        return parent::execute();

    }
}

Note: Its working when I changed the class form \Magento\Contact\Controller\Index\Post to \Magento\Framework\App\Action\Action

ERROR:

Fatal error: Uncaught TypeError: Argument 2 passed to Magento\Contact\Controller\Index\Post::__construct() must implement interface Magento\Contact\Model\ConfigInterface, array given, called in C:\xampp\htdocs\training\app\code\SCI\Form\Controller\Index\Post.php on line 17 and defined in C:\xampp\htdocs\training\vendor\magento\module-contact\Controller\Index\Post.php:49 Stack trace: #0 C:\xampp\htdocs\training\app\code\SCI\Form\Controller\Index\Post.php(17): Magento\Contact\Controller\Index\Post->__construct(Object(Magento\Framework\App\Action\Context), Array) #1 C:\xampp\htdocs\training\generated\code\SCI\Form\Controller\Index\Post\Interceptor.php(14): SCI\Form\Controller\Index\Post->__construct(Object(Magento\Framework\App\Action\Context), Object(Magento\Store\Model\StoreManager), Object(Magento\Framework\App\Config), Array) #2 C:\xampp\htdocs\training\vendor\magento\framework\ObjectManager\Factory\AbstractFactory.php(111): SCI\Form\Controller\Index\Post\Interceptor->__construct(Object(Magento\Framework\App\Action\Conte in C:\xampp\htdocs\training\vendor\magento\module-contact\Controller\Index\Post.php on line 49

1

1 Answers

0
votes

You can try to change your function to below code for fix error.

public function ifEnabled(){
    $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;

    return $this->_scopeConfig->getValue(
        'extended_contact/config/extended_contact_enabled',
        $storeScope
    );
}