1

Need to override method getImgUrl() from vendor\magento\module-captcha\Helper\Data.php but not success

app/code/YourCompany/YourModule/etc/di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Captcha\Helper\Data" type="YourCompany\YourModule\Helper\Data" />     

Override helper method code

namespace YourCompany\YourModule\Helper;
class Data extends \Magento\Captcha\Helper\Data
{
    public function __construct() {
        echo "Helper Rewrite Working"; die() // working till here
    }


    public function getImgUrl($website = null)
    {
        return $this->_storeManager->getStore()->getBaseUrl(
            DirectoryList::MEDIA
        ) . 'testhelper' . '/' . $this->_getWebsiteCode(
            $website
        ) . '/';
    }
}
  • @magefms - Do you have an idea regarding this? – Danes May 29 at 3:25
  • what do you mean by // working till here? when you use die() the remaining codes will not be executed – fmsthird May 29 at 5:04
0

Try using plugin:

app/code/SR/MagentoCommunity/etc/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">
    <type name="Magento\Captcha\Helper\Data">
        <plugin name="sr_captcha_data"
                type="SR\MagentoCommunity\Plugin\Captcha\Helper\Data" sortOrder="1"/>
    </type>
</config>

app/code/SR/MagentoCommunity/Plugin/Captcha/Helper/Data.php

<?php
namespace SR\MagentoCommunity\Plugin\Captcha\Helper;


class Data
{
    public function afterGetImgUrl(
        \Magento\Captcha\Helper\Data $subject,
        $url
    ) {
        // add your logic

        return $url;
    }
}
  • Do you think it will override the getImgUrl() from vendor\magento\module-captcha\Helper\Data.php? – Danes May 29 at 4:37
  • Overwrite is not good idea if you can pluginize. So avoid overwrite whole class when you can plugin method. – Sohel Rana May 29 at 4:56
  • After adjust my answer with your module, just clear cache will work. I have tested 2.3.1 – Sohel Rana May 29 at 4:56

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.