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.