0

I would need to override the custom module helper into my custom module.

Custom module code was,

class Data extends AbstractHelper
{
    const XML_PATH_BLOG = 'blog/';
    protected $storeManager;
    protected $objectManager;
    protected $postfactory;
    protected $categoryfactory;
    protected $tagfactory;
    protected $topicfactory;
    protected $scopeConfig;

    public function __construct(
        Context $context,
        ObjectManagerInterface $objectManager,
        StoreManagerInterface $storeManager,
        PostFactory $postFactory,
        CategoryFactory $categoryFactory,
        TagFactory $tagFactory,
        TopicFactory $topicFactory,
        ScopeConfigInterface $scopeConfig
    )
    {
        $this->objectManager   = $objectManager;
        $this->storeManager    = $storeManager;
        $this->postfactory     = $postFactory;
        $this->categoryfactory = $categoryFactory;
        $this->tagfactory      = $tagFactory;
        $this->topicfactory    = $topicFactory;
        $this->scopeConfig     = $scopeConfig;
        parent::__construct($context);
    }

My overriding code was,

class Resize extends \Mageplaza\Blog\Helper\Data
{
    protected $_filesystem;
    protected $_directory;
    protected $_imageFactory;
    protected $_storeManager;

    public function __construct(
        StoreManagerInterface $storeManager,
        \Magento\Framework\Filesystem $filesystem,
        \Magento\Framework\Image\AdapterFactory $imageFactory
    )
    {
        $this->_storeManager   = $storeManager;
        $this->_filesystem     = $filesystem;
        $this->_imageFactory   = $imageFactory;
        $this->_directory      = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
    }

Am I missed anything here? Because, it throws the following error when I was calling the method called getConfigValue() as follow:

Fatal error: Call to a member function getValue() on null in E:\xxxamp\htdocs\accelerator\app\code\Mageplaza\Blog\Helper\Data.php on line 50

public function getConfigValue($field, $storeId = null)
{
    return $this->scopeConfig->getValue($field, \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
}
1

Your constructor in the Resize class should look like this:

public function __construct(
    Context $context,
    ObjectManagerInterface $objectManager,
    StoreManagerInterface $storeManager,
    PostFactory $postFactory,
    CategoryFactory $categoryFactory,
    TagFactory $tagFactory,
    TopicFactory $topicFactory,
    ScopeConfigInterface $scopeConfig,
    \Magento\Framework\Filesystem $filesystem,
    \Magento\Framework\Image\AdapterFactory $imageFactory
)
{
    $this->_filesystem     = $filesystem;
    $this->_imageFactory   = $imageFactory;
    $this->_directory      = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
    parent::__construct($context, $objectManager, $storeManager, $postFactory, $categoryFactory, $tagFactory, $topicFactory, $scopeConfig);
}
  • Yes it is correct. I didn't get any error now. – TS Guhan Sep 22 '16 at 9:51
  • Marius, can you please tell me why should we pass the objects in __contruct(???). Because, In some cases we are not passing those objects. – TS Guhan Mar 23 '17 at 7:36
  • 1
    this is how Dependency Injection works in M2. You have to supply in the class constructor all the dependencies it needs. And Magento will take care of instantiating them. – Marius Mar 23 '17 at 8:04

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.