5

I have created a new module and override the

\Magento\Catalog\Block\Product\View\Description

function using preference. In this module we create new helper located in

Custom_Hello/Helper/Data.php

Then how to call this helper in

Custom_Hello/Block/Product/View/Description.php

If we use __construct got some error.

public function __construct(\Custom\Hello\Helper\Data $helper)
    {
        $this->helper = $helper;
    }

Anyone helpme

  • you can use object manager method into this case, As I know that is not peoper method. – Dhiren Vasoya Mar 1 at 11:30
  • I'm new in magento. But using plugin we got solution then using this way error – Soundararajan m Mar 1 at 11:32
  • Could you provide the error you are receiving? The way you are injecting it is the proper approach so if we can resolve the error that would be best. – rain2o Mar 1 at 11:36
  • Fatal error: Uncaught Error: Call to a member function dispatch() on null in /var/www/html/magento2/lib/internal/Magento/Framework/View/Element/AbstractBlock.php:652 Stack trace: #0 /var/www/html/magento2/lib/internal/Magento/Framework/Interception/Interceptor.php(58): Magento\Framework\View\Element\AbstractBlock->toHtml() #1 – Soundararajan m Mar 1 at 11:37
4

You can add your custom helper in constructor, but as this method is present in the class you're extending then you'd pass the right parameters in it, not just your helper

class Description extends \Magento\Catalog\Block\Product\View\Description

You are extending \Magento\Catalog\Block\Product\View\Description so let's see constructor in this class (in 2.3 version, you can check this in your project for other version, if that's the case)

public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Framework\Registry $registry,
    array $data = []
) {
    $this->_coreRegistry = $registry;
    parent::__construct($context, $data);
}

So, in your class you'd pass that $context & $registry parameters. You can do that this way

public function __construct(
    \Custom\Hello\Helper\Data $helper,
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Framework\Registry $registry,
    array $data = []
) {
    $this->helper = $helper;
    parent::__construct($context, $registry, $data);
}
  • thanks for your comments. Already got the solution and again thanks – Soundararajan m Mar 1 at 12:33
2

I got the solution. If we use following way

public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Framework\Registry $registry,
        \Custom\Hello\Helper\Data $dataHelper,
        array $data = []
    ) {
        parent::__construct($context, $registry, $data);
        $this->_dataHelper = $dataHelper;
    }
1

You can use directly without contructor

$this->helper('CompanyName\ModuleName\Helper\Data')->yourfunctionname();
  • We tried this way not working. – Soundararajan m Mar 4 at 4:24
  • This is valid for templates, but not for classes – Raul Sanchez Mar 5 at 10:17
0

In Custom_Hello/Block/Product/View/Description.php File

protected $helper;

public function __construct(
        \Magento\Catalog\Block\Product\Context $context, 
        \Custom\Hello\Helper\Data $helper
        array $data = []
    )
    {
        $this->helper= $heper;
        parent::__construct($context, $data);
    }

And You Can Call Your Helper Function Like this

$this->helper->yourFunctionName() 

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.