0

I am trying to create a helper that i can use from a few blocks i have extended however am getting errors.

<?php 
namespace Vendor\Module\Helper;

class Promotion extends \Magento\Framework\App\Helper\AbstractHelper{


public function __construct(
    \Magento\Framework\App\Helper\Context $context,
    array $data = []
)
{
    parent::__construct($context, $data);
}


    public function testFunc() {
        return "test";
    }
}

Following error shows on page load:

PHP Fatal error:  Uncaught Error: Call to a member function dispatch() on null in /mage/root/path/vendor/magento/framework/View/Element/AbstractBlock.php:644\n

I am running in developer have cleared the var/generation folder however still i get this error. I have tried adding construct however get a range of different errors doing so wanted some advice on here about where i have gone wrong?

My helper function is being called using below code from blocks:

namespace Vendor\Module\Block;

class CategoryPromote extends \Magento\Catalog\Block\Product\ListProduct
{
public function __construct(\Vendor\Module\Helper\Promotion $promote) {
    $this->promote = $promote;
}

public function getSale() {
    $this->promote->testFunc();
}
1

Add namaspace then use file as i mention below, i am sure it will work for you

namespace Vendor\Module\Helper;

use Magento\Framework\App\Helper\Context;
use Magento\Framework\App\Helper\AbstractHelper;

class Promotion extends AbstractHelper
{

 public function __construct(
        Context $context
    ) {
        parent::__construct($context);

    }

}

Block file should like this

namespace Vendor\Module\Block\YourBlockPath;

use Magento\Framework\View\Element\AbstractBlock;
use Magento\Framework\View\Element\Template\Context;
use Vendor\Module\Helper\Promotion as HelperData;

class Condition
    extends \Magento\Catalog\Block\Product\View\AbstractView
{
    /**
     * @var HelperData
     */
    protected $_hlp;

    public function __construct(
    \Magento\Catalog\Block\Product\Context $context,
    \Magento\Framework\Stdlib\ArrayUtils $arrayUtils,
    \Magento\Catalog\Helper\Product $catalogProduct,
    HelperData $helperData,
    array $data = []
) {
        $this->_hlp = $helperData;

        parent::__construct(
        $context,
        $arrayUtils,
        $data
    );
    }

}

You can see in Abstractview there in __construct function and you should have 3 params along with data like

public function __construct(
        \Magento\Catalog\Block\Product\Context $context,
        \Magento\Framework\Stdlib\ArrayUtils $arrayUtils,
        array $data = []
    ) {
}

so once you extend this class parent construct function should received params as same in child class.

Hope this is good to understand.

  • Why you repeat my answer? – PY Yick Aug 17 '17 at 9:51
  • @PYYick, i am not repeating it, my answer id different then you. please check, also it is common thing to use helper in m2 framework. you are new to this forum i think, i am using it since more then 4 year so i know all rules – liyakat Aug 17 '17 at 9:52
  • OK it's not about how long you and I stay here. I don't think it matters. Of course it's a better practice in coding. – PY Yick Aug 17 '17 at 10:00
  • Same error guys – harri Aug 17 '17 at 10:03
  • yes it's not about stay but see answer is totally different then yours. so you don't have any rights to say or written this comment. – liyakat Aug 17 '17 at 10:03
1

\Magento\Framework\App\Helper\AbstractHelper::__construct does not inject $data. So that your __construct should be:

public function __construct(
    \Magento\Framework\App\Helper\Context $context
)
{
    parent::__construct($context);
}

Also on your block file:

public function __construct(
    \Vendor\Module\Helper\Promotion $promote,
    \Magento\Catalog\Block\Product\Context $context,
    array $data = []
) {
    parent::__construct(
        $context,
        $data
    );
    $this->promote = $promote;
}

Of course you need to clear cache and delete the files in var/cache and var/generation

Ref: Fatal Error Call to a member function dispatch() while call my Block in magento 2

  • Have tried this initially had the same error will give it another go tho. – harri Aug 17 '17 at 9:43
  • If error persist please let me know – PY Yick Aug 17 '17 at 9:47
  • Yeah have been doing the cache and generation clearing but still getting the error. – harri Aug 17 '17 at 9:56
  • 1
    Are you sure the error is related to this helper? – PY Yick Aug 17 '17 at 9:57
  • 1
    In your case, I'd suggest you to inject value from block to helper. The helper just handle calculations. It don't need extra injections in helper and keep you changes minimal. – PY Yick Aug 17 '17 at 10:56
1

Try this,

You need to add Helper Context Object.

<?php 
namespace Vendor\Module\Helper;

class Promotion extends \Magento\Framework\App\Helper\AbstractHelper{


    public function __construct(
    \Magento\Framework\App\Helper\Context $context,
    array $data = []

    )
    {
    parent::__construct($context, $data);

    }

    public function testFunc() {
    return "test";
    }
}

Then, add Context object to You Block.

namespace Vendor\Module\Block;

class CategoryPromote extends \Magento\Catalog\Block\Product\ListProduct
{

 protected $promote;

public function __construct(
    \Magento\Catalog\Block\Product\Context $context,
    \Vendor\Module\Helper\Promotion $promote,
    array $data = []
    ) {
    $this->promote = $promote;
    parent::__construct(
        $context,
        $data
    );
    }


public function getSale() {
    $this->promote->testFunc();
}

Then, remove var/cache , var/generation and check it.

Let me know, if you have issue.

  • Thanks for reply , i tried that, updated my helper in my answer to yours. Cleared var/generation and refreshed page however have the same error still. – harri Aug 17 '17 at 9:11
  • @Harri I update my answer add this then clear chache and generation then, check it. – Rajkumar .E Aug 17 '17 at 9:23
  • Still no luck unfortunately getting the same error – harri Aug 17 '17 at 9:28
  • Could it may be be the use of context within the block? My block extends \Magento\Catalog\Block\Product\View and previously i didnt have a construct. I have added teh construct just for the helper but wondering if i have to modify for parent class? – harri Aug 17 '17 at 9:40
  • yes, you need to use context object, with in block object. go to \Magento\Catalog\Block\Product\View class and copy constrctor and paste to exrtent block then run it. – Rajkumar .E Aug 17 '17 at 10:10

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.