2

Can anybody help me to add a class in getCountryHtmlSelect()

I have tried to override \Magento\Directory\Block\Data file using plugin but i can't extend that so i go for perference. added code in di.xml and created file in Vendor\Module\Block\Data please check the code below

<?php
namespace Vendor\Module\Block;
use Magento\Framework\View\Element\Template;
class Data extends \Magento\Directory\Block\Data
{
 public function getCountryHtmlSelect($defValue = null, $name = 'country_id', $id = 'country', $title = 'Country')
 {
 \Magento\Framework\Profiler::start('TEST: ' . __METHOD__, ['group' => 'TEST', 'method' => __METHOD__]);
 if ($defValue === null) {
 $defValue = $this->getCountryId();
 }
 $cacheKey = 'DIRECTORY_COUNTRY_SELECT_STORE_' . $this->_storeManager->getStore()->getCode();
 $cache = $this->_configCacheType->load($cacheKey);
 if ($cache) {
 $options = $this->getSerializer()->unserialize($cache);
 } else {
 $options = $this->getCountryCollection()
 ->setForegroundCountries($this->getTopDestinations())
 ->toOptionArray();
 $this->_configCacheType->save($this->getSerializer()->serialize($options), $cacheKey);
 }
 $html = $this->getLayout()->createBlock(
 \Magento\Framework\View\Element\Html\Select::class
 )->setName(
 $name
 )->setId(
 $id
 )->setTitle(
 $this->escapeHtmlAttr(__($title))
 )->setValue(
 $defValue
 )->setClass(
 'class-name'
 )->setOptions(
 $options
 )->setExtraParams(
 'data-validate="{\'validate-select\':true}"'
 )->getHtml();
 \Magento\Framework\Profiler::stop('TEST: ' . __METHOD__);
 return $html;
 
 }
 
}

and my di.xml file

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
 <preference for="Magento\Directory\Block\Data" type="Vendor\Module\Block\Data" />
</config>

cleared cache and compile the code shows no error.

can anybody give me an idea about this?

Thnaks in Advance

asked Jul 24, 2022 at 5:03

1 Answer 1

0

Try This Code

<?php
namespace VendoreName\ModuleName\Block;
class Data extends \Magento\Directory\Block\Data
{
 /**
 * @var \Magento\Framework\App\Cache\Type\Config
 */
 protected $_configCacheType;
 /**
 * @var \Magento\Directory\Model\ResourceModel\Region\CollectionFactory
 */
 protected $_regionCollectionFactory;
 /**
 * @var \Magento\Directory\Model\ResourceModel\Country\CollectionFactory
 */
 protected $_countryCollectionFactory;
 /**
 * @var \Magento\Framework\Json\EncoderInterface
 */
 protected $_jsonEncoder;
 /**
 * @var \Magento\Directory\Helper\Data
 */
 protected $directoryHelper;
 /**
 * @var \Magento\Framework\Serialize\SerializerInterface
 */
 private $serializer;
 /**
 * @param \Magento\Framework\View\Element\Template\Context $context
 * @param \Magento\Directory\Helper\Data $directoryHelper
 * @param \Magento\Framework\Json\EncoderInterface $jsonEncoder
 * @param \Magento\Framework\App\Cache\Type\Config $configCacheType
 * @param \Magento\Directory\Model\ResourceModel\Region\CollectionFactory $regionCollectionFactory
 * @param \Magento\Directory\Model\ResourceModel\Country\CollectionFactory $countryCollectionFactory
 * @param array $data
 */
 public function __construct(
 \Magento\Framework\View\Element\Template\Context $context,
 \Magento\Directory\Helper\Data $directoryHelper,
 \Magento\Framework\Json\EncoderInterface $jsonEncoder,
 \Magento\Framework\App\Cache\Type\Config $configCacheType,
 \Magento\Directory\Model\ResourceModel\Region\CollectionFactory $regionCollectionFactory,
 \Magento\Directory\Model\ResourceModel\Country\CollectionFactory $countryCollectionFactory,
 array $data = []
 ) {
 $this->directoryHelper = $directoryHelper;
 $this->_jsonEncoder = $jsonEncoder;
 $this->_configCacheType = $configCacheType;
 $this->_regionCollectionFactory = $regionCollectionFactory;
 $this->_countryCollectionFactory = $countryCollectionFactory;
 parent::__construct(
 $context,
 $directoryHelper,
 $jsonEncoder,
 $configCacheType,
 $regionCollectionFactory,
 $countryCollectionFactory,
 $data
 );
 }
 /**
 * Returns country html select
 *
 * @param null|string $defValue
 * @param string $name
 * @param string $id
 * @param string $title
 * @return string
 */
 public function getCountryHtmlSelect($defValue = null, $name = 'country_id', $id = 'country', $title = 'Country')
 {
 \Magento\Framework\Profiler::start('TEST: ' . __METHOD__, ['group' => 'TEST', 'method' => __METHOD__]);
 if ($defValue === null) {
 $defValue = $this->getCountryId();
 }
 $cacheKey = 'DIRECTORY_COUNTRY_SELECT_STORE_' . $this->_storeManager->getStore()->getCode();
 $cache = $this->_configCacheType->load($cacheKey);
 if ($cache) {
 $options = $this->getSerializer()->unserialize($cache);
 } else {
 $options = $this->getCountryCollection()
 ->setForegroundCountries($this->getTopDestinations())
 ->toOptionArray();
 $this->_configCacheType->save($this->getSerializer()->serialize($options), $cacheKey);
 }
 $html = $this->getLayout()->createBlock(
 \Magento\Framework\View\Element\Html\Select::class
 )->setName(
 $name
 )->setId(
 $id
 )->setTitle(
 $this->escapeHtmlAttr(__($title))
 )->setValue(
 $defValue
 )->setOptions(
 $options
 )->setClass(
 'custom-class'
 )->setExtraParams(
 'data-validate="{\'validate-select\':true}"'
 )->getHtml();
 \Magento\Framework\Profiler::stop('TEST: ' . __METHOD__);
 return $html;
 }
 /**
 * Get serializer
 *
 * @return \Magento\Framework\Serialize\SerializerInterface
 */
 private function getSerializer()
 {
 if ($this->serializer === null) {
 $this->serializer = \Magento\Framework\App\ObjectManager::getInstance()
 ->get(\Magento\Framework\Serialize\SerializerInterface::class);
 }
 return $this->serializer;
 }
}

Output: enter image description here

answered Jul 24, 2022 at 5:16
4
  • I tried above code with setClass and the class is not added Commented Jul 25, 2022 at 6:30
  • @Ajith it's working and class name are shown. Please clear the cache or run the Magento command before checking it. Commented Jul 25, 2022 at 6:54
  • Hi @Msquare i tried this with same code and run magento command and the class is not added. addClass works fine in core file.Have you tried this by extended this file? Commented Jul 25, 2022 at 9:40
  • @Ajith Try without di.xml preference. Just comment or remove your preference from di.xml and run Magento comment then check it. Commented Jul 25, 2022 at 9:42

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.