Magento 2: How to override Zend library files function only?
-
05-10-2020 - |
Question
I would like to override below file
magento\vendor\magento\zendframework1\library\Zend\Captcha\Image.php
Can we do with
magento\app\code\zendframework1\library\Zend\Captcha\Image.php ?
What will be the class extends?
Following http://webkul.com/blog/overriding-rewriting-classes-magento2/
It's giving 90% answer.
magento\app\code\Zend\Captcha\Image.php
class Zend_Captcha_Image extends Zend_Captcha_Word
{
protected function _generateImage($id, $word)
{
}
}
I want only this function. It gives abstract class error. If i only put this.
Can we override composer.json
for this? Don't want to change in Magento Default
When u click on "Reload Captcha" it gives big DIV blank then it loads. It's not working as default works. Something still missing in override.
Solution
The Magento_Captcha
module use Zend_Captcha_Image
for generate of captcha images.
Magento\Captcha\Model\DefaultModel:
class DefaultModel extends \Zend_Captcha_Image implements \Magento\Captcha\Model\CaptchaInterface
For extending image generation functionality, you can add you custom preference on Magento\Captcha\Model\DefaultModel
.
di.xml
<preference for="Magento\Captcha\Model\DefaultModel" type="Vendor\Module\Model\Captcha\MyCaptchaModel"/>
MyCaptchaModel.php:
/**
* Class MyCaptchaModel.
*/
class MyCaptchaModel extends \Magento\Captcha\Model\DefaultModel
{
/**
* {@inheritdoc}
*/
protected function _generateImage($id, $word)
{
//@TODO
}
}
Alternatively, you can add plugin on \Magento\Captcha\Model\CaptchaFactory::create()
and define your custom logic for creating instances of captcha models.