I am trying to override Block \Magento\Directory\Block\Data with following method getCountryHtmlSelect() with following script, but it's not executing.


<?xml version="1.0"?> 
<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="Magenticians\Modulecontact\Block\Data" /> 


namespace Magenticians\Modulecontact\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')

    public function test()
  • Can you tell me where is your di.xml file avilable? – Dhiren Vasoya Jan 22 '18 at 12:35

Seems your class has no closing brace. It can cause an error. Additional do not forget to clean cache and remove all files in the var/generation or generated directory (from Magento root) or just run bin/magento setup:di:compile.

You can check the code using next script in the root of Magento:

Create file testsomething.php:

use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);

$obj = $bootstrap->getObjectManager();

$state = $obj->get('Magento\Framework\App\State');

$block = $obj->get('\Magento\Directory\Block\Data');
echo get_class($block);

How can I bootstrap Magento 2 in a test.php script?

Open this file in the browser (http://yormagentohost.com/testsomething.php). Result should looks like this:


I tested this code locally and for me it works fine.

| improve this answer | |
  • i done all things and i am able to override different block classes expect this – Ramesh Jan 22 '18 at 12:37
  • Maybe some other module overwriten this class too? Check what is returned by a `get_class' function in the template or somewhere else where you can access the block object. I'll update my answer. – Sergey Uchuhlebov Jan 22 '18 at 12:55
  • your code is useful, it showing my overrider class but method not applying – Ramesh Jan 22 '18 at 13:42
  • @Ramesh where you checking the overwriten class on the frontend? – Sergey Uchuhlebov Jan 22 '18 at 13:47
  • @sergey..yes on fontend – Ramesh Jan 22 '18 at 15:33

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.