1

Using Magento 2.3.0 with php7.1

I'm trying to use this module to get store information.

I have modified the folder names as such

app/code/Vendor/SiteInfo/Block/SiteInfo.php

And used the following code in SiteInfo.php

<?php
namespace Vendor\SiteInfo\Block;
class SiteInfo extends \Magento\Framework\View\Element\Template
{
    protected $_storeManager;    

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,        
        \Magento\Store\Model\StoreManagerInterface $storeManager,        
        array $data = []
    )
    {        
        $this->_storeManager = $storeManager;        
        parent::__construct($context, $data);
    }

    /**
     * Get store identifier
     *
     * @return  int
     */
    public function getStoreId()
    {
        return $this->_storeManager->getStore()->getId();
    }

    /**
     * Get website identifier
     *
     * @return string|int|null
     */
    public function getWebsiteId()
    {
        return $this->_storeManager->getStore()->getWebsiteId();
    }

    /**
     * Get Store code
     *
     * @return string
     */
    public function getStoreCode()
    {
        return $this->_storeManager->getStore()->getCode();
    }

    /**
     * Get Store name
     *
     * @return string
     */
    public function getStoreName()
    {
        return $this->_storeManager->getStore()->getName();
    }

    /**
     * Get current url for store
     *
     * @param bool|string $fromStore Include/Exclude from_store parameter from URL
     * @return string     
     */
    public function getStoreUrl($fromStore = true)
    {
        return $this->_storeManager->getStore()->getCurrentUrl($fromStore);
    }

    /**
     * Check if store is active
     *
     * @return boolean
     */
    public function isStoreActive()
    {
        return $this->_storeManager->getStore()->isActive();
    }
}
?>

This is where I need help, it then says

Using the following script in the .phtml file and print the store information.

echo $block->getStoreId() . '<br />';
echo $block->getStoreCode() . '<br />';
echo $block->getWebsiteId() . '<br />';
echo $block->getStoreName() . '<br />';
echo $block->getStoreUrl() . '<br />';
echo $block->isStoreActive() . '<br />';

I tried using it directly in the page using

<?php echo $block->getStoreName() . '<br />';?>

that just shows

$block->getStoreName() . '
';


So how do I go about this, for example if I want to show the "Store name" in a page or a block I've created from the admin panel, how do I go about using the echo script.


Update:

I've created a file

storename.phtml

in

app/code/Vendor/Siteinfo/view/frontend/templates

With this code

<?php 
/** @var \Vendor\ModuleName\Block\Hello $block */ 
?> 
<?php 
echo $block->getStoreName(); 
?>

and used this block on a page from admin panel

{{block class="Vendor\SiteInfo\Block\SiteInfo" name="site-info" as="site-info" template="Vendor_SiteInfo::storename.phtml" }}

flushed magento cache, and get this error

"We're sorry, an error has occurred while generating this content."

This is module I have created, that is at this path

app/code

4
  • any error in log ?
    – Pawan
    Commented Jan 8, 2019 at 17:34
  • No error in the logs Commented Jan 9, 2019 at 14:57
  • I have tried your code and it is working. in admin you are using Vendor instead of Website ?
    – Pawan
    Commented Jan 9, 2019 at 16:24
  • I have shared the module I have created above. Commented Jan 9, 2019 at 16:33

3 Answers 3

1

There are mainly 2 issue.

  1. Your Registration.php is placed at wrong place Currently it is placed at

    Vendor/Siteinfo/etc/registration.php

it should be:

Vendor/Siteinfo/registration.php

  1. Wrong Vendor name is defined in registration.php
  2. Wrong Vendor name in module.xml

    Please follow below

    Create

    app/code/Vendor/Siteinfo/registration.php

    <?php
    \Magento\Framework\Component\ComponentRegistrar::register(
        \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vendor_Siteinfo',
    __DIR__
    );
    

    app/code/Vendor/Siteinfo/etc/module.xml

    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Vendor_Siteinfo" setup_version="1.0.0">
    </module>
    

    app/code/Vendor/Siteinfo/Block/Siteinfo.php

    <?php
    namespace Vendor\Siteinfo\Block;
    class Siteinfo extends \Magento\Framework\View\Element\Template
    {    
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,        
        array $data = []
    )
    {            
        parent::__construct($context, $data);
    }
    
    /**
     * Get store identifier
     *
     * @return  int
     */
    public function getStoreId()
    {
        return $this->_storeManager->getStore()->getId();
    }
    
    /**
     * Get website identifier
     *
     * @return string|int|null
     */
    public function getWebsiteId()
    {
        return $this->_storeManager->getStore()->getWebsiteId();
    }
    
    /**
     * Get Store code
     *
     * @return string
     */
    public function getStoreCode()
    {
        return $this->_storeManager->getStore()->getCode();
    }
    
    /**
     * Get Store name
     *
     * @return string
     */
    public function getStoreName()
    {
        return $this->_storeManager->getStore()->getName();
    }
    
    /**
     * Get current url for store
     *
     * @param bool|string $fromStore Include/Exclude from_store parameter from URL
     * @return string     
     */
    public function getStoreUrl($fromStore = true)
    {
        return $this->_storeManager->getStore()->getCurrentUrl($fromStore);
    }
    
    /**
     * Check if store is active
     *
     * @return boolean
     */
    public function isStoreActive()
    {
        return $this->_storeManager->getStore()->isActive();
    }
    }
    ?>
    

    app/code/Vendor/Siteinfo/view/frontend/templates/storename.phtml

    <?php echo $block->getStoreName(); ?>
    

    After that you need to run:

    php bin/magento setup:upgrade

    php bin/magento cache:flush

Finally call:

{{block class="Vendor\Siteinfo\Block\Siteinfo" name="site-info" as="site-info" template="Vendor_Siteinfo::storename.phtml" }}

You can download module at GitHub

5
  • Have done exactly as you have listed, the config end tag was missing on the module.xml, I added that and ran upgrade, the module is enabled under etc/config.php, however I'm getting this error "Error filtering template: Invalid block type: Vendor\Siteinfo\Block\Siteinfo" Commented Jan 9, 2019 at 18:34
  • From exception log [2019-01-09 18:56:02] main.CRITICAL: Invalid block type: Vendor\Siteinfo\Block\Siteinfo {"exception":"[object] (Magento\\Framework\\Exception\\LocalizedException(code: 0): Invalid block type: Vendor\\Siteinfo\\Block\\Siteinfo at /home/xyz/m230.xyz.com/Vendor/magento/framework/View/Layout/Generator/Block.php:275, ReflectionException(code: -1): Class Vendor\\Siteinfo\\Block\\Siteinfo does not exist at /home/xyz/m230.xyz.com/vendor/magento/framework/Code/Reader/ClassReader.php:19)"} [] Commented Jan 9, 2019 at 18:58
  • Your block name file should be Siteinfo.php. I have upload module on github, Please download and it. I have tested and it s working.If still have any issue, let me know.
    – Pawan
    Commented Jan 10, 2019 at 2:28
  • Thank you very much Pawan, much appreciated, it works! :-) Commented Jan 10, 2019 at 4:32
  • Glad to know. Welcome :)
    – Pawan
    Commented Jan 10, 2019 at 11:53
1

If you want this in block from admin panel. Call from block or from CMS page like this

{{block class="Mynamespace\Mymodule\Block\SiteInfo" name="site-info" as="site-info" template="Magento_Theme::siteinfo.phtml" }}

It will call the function from related class.

Thanks

0

In order to use the following block scripts
you need to create a template

Example: Create a template hello.phtml to your custom module following the namespace structure below

Vendor\ModuleName\view\frontend\templates\hello.phtml

<?php
/** @var \Vendor\ModuleName\Block\Hello $block */
?>
<?php echo $block->getStoreName(); ?>
2
  • I've created a storename.phtml file in app/code/vendor/Siteinfo/view/frontend/templates With this code <?php /** @var \Vendor\ModuleName\Block\Hello $block */ ?> <?php echo $block->getStoreName(); ?> and used this block on a page from admin panel {{block class="vendor\SiteInfo\Block\SiteInfo" name="site-info" as="site-info" template="Magento_Theme::storename.phtml" }} flushed magento cache, and get this error "We're sorry, an error has occurred while generating this content." Should I run a upgrade from SSH? Commented Jan 8, 2019 at 11:34
  • can you post your code for the page in admin in your post above. I will check. no need to run upgrade from ssh
    – fmsthird
    Commented Jan 8, 2019 at 12:05

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.