13

How is it possible to output the current store name in a Magento 2 custom template with a custom block?

  • what template?? – Marius Dec 14 '15 at 8:44
  • custom template using a custom block – Dominik Barann Dec 14 '15 at 9:06
17

you need to use the instance of \Magento\Framework\App\Config\ScopeConfigInterface in your block:

Create the method getStoreName()

public function getStoreName()
{
    return $this->_scopeConfig->getValue(
        'general/store_information/name',
        \Magento\Store\Model\ScopeInterface::SCOPE_STORE
    );
}

and call in your template echo $this->getStoreName()

  • Loos like a great solution, but I get the following error: Fatal error: Call to a member function dispatch() on a non-object in /vendor/magento/framework/View/Element/AbstractBlock.php on line 637. – Dominik Barann Dec 14 '15 at 9:36
  • clear the var/generation folder – Marius Dec 14 '15 at 9:37
  • 1
    @Marius 's approach is right, but it didn't work for me. Instead I've used \Magento\Store\Model\StoreManagerInterface $storeManager in constructor and public function getStoreName() { return $this->storeManager->getStore()->getName(); } Instead of getName() you can use getCode(), getId(). – Razvan May 19 '16 at 9:37
9

Use store manager, which holds information about active store. If custom block is not inherited from Template block, inject dependency on \Magento\Store\Model\StoreManagerInterface in construct.

<?php
namespace VendorName\ModuleName\Block;

class CustomBlock extends \Magento\Framework\View\Element\Template
{
    /**
     * Get current store name.
     *
     * @return string
     */
    public function getCurrentStoreName()
    {
        return $this->_storeManager->getStore()->getName();
    }
}

Then in template:

<?php
/**
 * @var $block \VendorName\ModuleName\Block\CustomBlock
 */
echo "<h1>Current store name is '{$block->getCurrentStoreName()}'</h1>";
?>
  • Thanks for your solution, but I don't want to show the store view name.Searched for the store name of the configuration. – Dominik Barann Dec 14 '15 at 10:38
4

To get a store configuration value like general/store_information/name you can use the following

$config = new \Magento\Framework\App\Config\ScopeConfigInterface();

echo $config->getValue('general/store_information/name');

However, doing this from a block or helper would be cleaner. Below is a helper class that would exist in your own custom module

namespace [Namespace]\[Module]\Helper;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
    /**
     * Retrieve store name
     *
     * @return string|null
     */
    public function getStoreName()
    {
        return $this->scopeConfig->getValue(
            'general/store_information/name',
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE
        );
    }
}

Which you would inject as dependency in your block class

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.