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);
}
Marius
199k55 gold badges431 silver badges837 bronze badges
asked Sep 22, 2016 at 8:22

1 Answer 1

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);
}
answered Sep 22, 2016 at 8:35
3
  • Yes it is correct. I didn't get any error now. Commented Sep 22, 2016 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. Commented Mar 23, 2017 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. Commented Mar 23, 2017 at 8:04

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.