3

I'm trying to get store information which are set in backend like email,contact,address etc...(I've taken reference from here : https://magento.stackexchange.com/a/125359/39607 )

To do so I've created a module

Following is my block file...

Contact.php

<?php

namespace Vendor\Module\Block;

class Contact extends \Magento\Framework\View\Element\Template
{
    protected $_storeInfo;

    public function __construct(
        \Magento\Store\Model\Information $storeInfo
    )
    {        
        $this->_storeInfo = $storeInfo;
    }
    public function getPhoneNumber()
    {
        return $this->_storeInfo->getInformationObject()->getPhone();
    }

}

And in my phtml I'm calling like this :

<?php
    echo "Phone: ".$block->getPhoneNumber();
?>

But it's not working and showing me an error :

( ! ) Fatal error: Uncaught Error: Call to a member function dispatch() on null in vendor\magento\framework\View\Element\AbstractBlock.php on line 644 ( ! ) Error: Call to a member function dispatch() on null in vendor\magento\framework\View\Element\AbstractBlock.php on line 644

What can be the issue here??

3 Answers 3

6

It's because your constructor does not match the parent block constructor. (the ... in my answer you linked are important).

public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Store\Model\Information $storeInfo,
    array $data = []
)
{        
    $this->_storeInfo = $storeInfo;
    parent::__construct($context, $data);
}
4
  • But after doing so I'm getting following error now ( ! ) Fatal error: Uncaught Error: Call to undefined method Magento\Store\Model\Information::getInformationObject() in \www\m2\app\code\Package\Module\Block\Contact.php on line 26 Commented Dec 6, 2016 at 10:30
  • @KaushalSuthar there was a mistake in my original post you linked. It must be getStoreInformationObject instead of getInformationObject Commented Dec 6, 2016 at 10:33
  • Now I'm getting error like this : Fatal error: Uncaught TypeError: Argument 1 passed to Magento\Store\Model\Information::getStoreInformationObject() must be an instance of Magento\Store\Model\Store, none given, called in m2\app\code\Package\Module\Block\Contact.php on line 26 and defined in m2\vendor\magento\module-store\Model\Information.php on line 79 :( Commented Dec 6, 2016 at 11:02
  • @KaushalSuthar I get that error as well. I suppose that the context needs to be in its own class. Commented Feb 9, 2018 at 12:53
3

Well, I've done in following way...But I don't it's proper way to do so or not...

    namespace Vendor\Module\Block;

class Contact extends \Magento\Framework\View\Element\Template
{
    protected $_storeInfo;
    protected $_storeManagerInterface;

    public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Store\Model\Information $storeInfo,
    array $data = []
    )
    {
        $this->_storeInfo = $storeInfo;
        parent::__construct($context, $data);
    }
    public function getPhoneNumber()
    {
        return $this->_storeInfo->getStoreInformationObject($this->_storeManager->getStore())->getPhone();
}

and in template file I've called

echo $block->getPhoneNumber();

UPDATE:

We don't need to use \Magento\Store\Model\StoreManagerInterface as it's already in parent class. We can directly use it vai $this->_storeManager

3
  • @Raphael at Digital Pianism : Is it proper way?? In getStoreInformationObject method it require store object that's why I had to use $_storeManagerInterface... Commented Dec 7, 2016 at 11:15
  • Yes that looks good. Not sure if you need to pass null to the getStore method though Commented Dec 7, 2016 at 11:19
  • @RaphaelatDigitalPianism : Yes, You are right as always, No need to pass null :) Thanks Commented Dec 7, 2016 at 11:22
1
 public function __construct(\Magento\Framework\View\Element\Template\Context $context,  
                \Magento\Store\Model\Information $storeInfo,  
                \Magento\Store\Model\StoreManagerInterface $storeManager ,
                array $data = [] ) { 
             $this->_storeInfo = $storeInfo;
             $this->_storeManager = $storeManager;
             parent::__construct($context, $data); 
    }

Make sure you are working in developer mode and clear your generation folder by

rm -rf var/generation/*

did you tried like this?

   public function getPhoneNumber()
    {
        return $this->_storeInfo->getStoreInformationObject($this->_storeManager->getStore())->getPhone();
    }
6
  • Error : Fatal error: Uncaught TypeError: Argument 1 passed to Magento\Store\Model\Information::getStoreInformationObject() must be an instance of Magento\Store\Model\Store, none given, called in m2\app\code\Package\Module\Block\Contact.php on line 26 and defined in m2\vendor\magento\module-store\Model\Information.php on line 79 Commented Dec 7, 2016 at 10:35
  • update your code and clear the generation folder Commented Dec 7, 2016 at 11:06
  • yes I did, But still Commented Dec 7, 2016 at 11:10
  • try my updated code Commented Dec 7, 2016 at 11:19
  • I've already posted answer by myself, thanks anyway Commented Dec 7, 2016 at 11:23

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.