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 Answer 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– TS Guhan2016年09月22日 09:51:25 +00:00Commented 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.TS Guhan– TS Guhan2017年03月23日 07:36:56 +00:00Commented Mar 23, 2017 at 7:36
-
1this 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– Marius2017年03月23日 08:04:49 +00:00Commented Mar 23, 2017 at 8:04