0

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

di.xml

<?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" /> 
</config>

<?php

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')
    {
        exit;
    }

    public function test()
    {
        exit;
    }
?>
Ramesh
  • 986
  • 1
  • 14
  • 38

1 Answers1

1

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:

<?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');
$state->setAreaCode('frontend');

$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:

Magenticians\Modulecontact\Block\Data

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