1

I am trying to get the store number using a module the correct way and am having a bit of difficulty. I have used the below code which works however when trying to compile i recieve an error. My code is currently as below:

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,
    \Magento\Store\Model\StoreManagerInterface $storeManagerInterface,
    array $data = []
)
{
    parent::__construct($context, $data); 
    $this->_storeInfo = $storeInfo;
    $this->_storeManagerInterface = $storeManagerInterface;
}
public function getPhoneNumber()
{
    return $this->_storeInfo->getStoreInformationObject($this->_storeManagerInterface->getStore(null))->getPhone();
}
}

This appears to work fine and number is shown on front-end as expected. However upon running 'bin/magento setup:di:compile' the following errors are shown and compilation fails.

Incorrect dependency in class Vendor\Module\Block\Storenumber
\Magento\Store\Model\StoreManagerInterface already exists in context object

I can see that the storeManager interface is already called within \Magento\Framework\View\Element\Template however removing this from construct also does not work. How do i correctly call the store number?

1

Just keep below code in your __construct() method of Block PHP file,

Already Magento\Framework\View\Element\Template\Context file contains Storemanager defination so you have to use those method as directly from $context object.

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

Remove var folder and check again.

| improve this answer | |
  • I think ive just got it working anyway by removing the store manager and using the original $this->_storeManager rather than $this->_storeManagerInterface – harri Jun 7 '17 at 8:49
  • This is the correct way to define your storemanager in constuct() method when you have extends Magento\Framework\View\Element\Template method – Rakesh Jesadiya Jun 7 '17 at 8:51
  • Is this basically renaming the instance of the Storemanager to storemanagerinterface? Similar to what i just did but other way around? – harri Jun 7 '17 at 8:51

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.