0

I'm trying to insert the current store country name through a CMS Page/Block by following this article.

I've created a Block file countryname.php in

app/code/Vendor/Countryname/Block

with the following

<?php
namespace Vendor\Countryname\Block;
class Countryname extends \Magento\Directory\Model\CountryFactory

{  
    public function __construct(
            \Magento\Directory\Model\CountryFactory $countryFactory
        ) {
            $this->_countryFactory = $countryFactory;
        }

    public function getCountryname($countryCode){    
        $country = $this->_countryFactory->create()->loadByCode($countryCode);
        echo $country->getName();
    }
    ?>

Then created a countryname.phtml in

app/code/Vendor/Countryname/view/frontend/templates

with the following

<?php
echo $block->getCountryname($countryCode);
?>

and have used this to call the block from CMS Page/Static Block

{{block class="Vendor\Countryname\Block\Countryname" template="Vendor_Countryname::countryname.phtml" }}

However I get this error on the frontend

Error filtering template: Invalid block type: Vendor\Countryname\Block\Countryname

and this is error in the exception log

main.CRITICAL: Invalid block type: Vendor\Countryname\Block\Countryname {"exception":"[object] (Magento\Framework\Exception\LocalizedException(code: 0): Invalid block type: Vendor\Countryname\Block\Countryname at /home/xyz/m230.xyz.com/vendor/magento/framework/View/Layout/Generator/Block.php:275, ReflectionException(code: -1): Class Vendor\Countryname\Block\Countryname does not exist at /home/xyz/m230.xyz.com/vendor/magento/framework/Code/Reader/ClassReader.php:19)"} []


UPDATE

Thanks to magefms this is a partial solution he provided.

In the countryname.php

<?php 

namespace Koshmart\Countryname\Block; 

class Countryname extends \Magento\Framework\View\Element\Template 
{ 
protected $_countryFactory; 


public function __construct( 
\Magento\Framework\View\Element\Template\Context $context, 
\Magento\Directory\Model\CountryFactory $countryFactory, 
array $data = [] 
){ 
parent::__construct($context, $data); 
$this->_countryFactory = $countryFactory; 

} 

public function getCountryname($countryCode){ 

$country = $this->_countryFactory->create()->loadByCode($countryCode); 
echo $country->getName(); 
} 
}
?>

in the countryname.phtml

<?php 
echo $block->getCountryname('us');
?>

and use this in CMS page/static block to get the country name

{{block name="koshmart_countryname" class="Koshmart\Countryname\Block\Countryname" template="countryname.phtml"}}

However the country ID "us" etc has to be manually entered in the phtml file. It does not dynamically get the country ID and then get the country name on the CMS Page/static block.

  • Have you found solution ? – Pawan Jan 10 at 17:36
  • Yes thanks to magefms, but not a full solution, its does not work dynamically. The country ID needs to be manually inserted in the phtml file for it to show the country name in CMS page/static block. – user2240778 Jan 10 at 17:38
0

Try to change the file name to be the same with the class name

Like this one :

class Countryname extends....

File name should be

Countryname.php

  • I changed the file name as suggested, now the page itself doesn't load. If I remove the call block shortcode from the CMS page then the page loads, appears like its a issue with the code? – user2240778 Jan 10 at 6:54
  • try to declare this _countryFactory on top of the public function line , declare it like protected $_countryFactory; – fmsthird Jan 10 at 6:59
  • I'm sorry, but could you post the full code, as I'm not sure where I should be declaring it. – user2240778 Jan 10 at 7:02
  • <?php namespace Vendor\Countryname\Block; class Countryname extends \Magento\Directory\Model\CountryFactory { protected $_countryFactory; public function __construct( \Magento\Directory\Model\CountryFactory $countryFactory ) { $this->_countryFactory = $countryFactory; } public function getCountryname($countryCode){ $country = $this->_countryFactory->create()->loadByCode($countryCode); echo $country->getName(); } ?> – fmsthird Jan 10 at 7:03
  • Still doesn't work, page does'nt load, however in the editor I use it shows this error on the code " syntax error, unexpected '; ' ,expecting, 'T_FUNCTION or T_CONST" – user2240778 Jan 10 at 7:15
0

Add this in your block file

blockfilename.php

use Magento\Directory\Model\CountryFactory;

class blockName extends \Magento\Framework\View\Element\Template
{

protected $_locationFactory;

public function __construct(
        ...................................................... 
        \Magento\Directory\Model\CountryFactory $countryFactory,
        ..................................................
    ) {
        .........................................
        $this->_countryFactory = $countryFactory;
        .........................................
    }

   public function getCountryname($countryCode)
  {    
    $country = $this->_countryFactory->create()->loadByCode($countryCode);
    return $country->getName();
}

}

In You .phtml file call this like

filename.phtml

<?php

 $countryName = $block->getCountryname($country_code);  // pass your country code herer

echo $countryname;

?>

add this both file into one layout file

layputfilename.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">

    <head>
        <title>Storelocator</title>
    </head> 

      <body>
            <referenceContainer name="content">

                  <block class="[vendore_name]\[module_name]\Block\blockfilename" name="block_file_name" template="VendoreName_ModuleName::filename.phtml" />

            </referenceContainer>

      </body>
</page>

I Hope This Helps You.

  • if anyone have any problem related to above code please ask here – Msquare Sep 29 at 9:58
  • If it's works for you VOTE UP – Msquare 2 days ago

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.