0

I created a backend configuration for my custom plugin

/app/code/Sostanza/LiveHelp/etc/adminhtml/system.xml

https://pastebin.com/2jwnYLwb

/app/code/Sostanza/LiveHelp/Block/Button.php

https://pastebin.com/myadtSZE

I need to get the value of a saved option and I already have a Helper class where I can read the value I need.

I have my helper in

/app/code/Sostanza/LiveHelp/Helper/Data.php

https://pastebin.com/JAUWX41c (Max 2 links with my reputation)

But if in the Button.php I add

Sostanza\LiveHelp\Helper\Data $data

The config section doesn't get rendered anymore I tried also to inject other stuff, but it doesn't work. Of course I ran "magento setup:upgrade" and deleted cache/generation folders.

Thank you so much

2 Answers 2

0

Make your code look like this:

protected $_helper;

public function __construct(
            Context $context,
            \Sostanza\LiveHelp\Helper\Data $helper,
            array $data = []
    ) {
     $this->_helper = $helper;
     parent::__construct($context, $data);
    }

And then you can use functions in your helper class like this:

$this->_helper->someRandomFunction();

So in general: you need to pass your helper into __construct() method, then you can use it.

3
  • Yes, I know that. I sayd that if I add that code into my class, it doesn't work!
    – user57916
    Aug 30, 2017 at 14:18
  • Wait, you injected Helper before array $data. That I haven't tried yet. I'll let you know, maybe the issue is caused by wrong order
    – user57916
    Aug 30, 2017 at 14:21
  • I can't believe this. It was the order...but why? If the parent class wants $context and $data why inject in different order?
    – user57916
    Aug 30, 2017 at 14:23
0

Did you tried it with "\" in the beginning of "\Sostanza\LiveHelp\Helper\Data" ? Because it make a lot of difference.

8
  • Sorry, i can't comment to above answer
    – Obscuro
    Aug 30, 2017 at 14:25
  • And yes, it was me, who answered.
    – Obscuro
    Aug 30, 2017 at 14:26
  • Yes I tried that. But it broke anyway...I'll give another try..Anyway I also tried with "use \Sostanza\LiveHelp..." and "use Sostanza\LiveHelp..."
    – user57916
    Aug 30, 2017 at 14:27
  • I confirm that it doesn't work either with or without "\" if $data is before...I really don't understand...
    – user57916
    Aug 30, 2017 at 14:28
  • Weird, I'm not sure if it's all about order. Maybe you was not paying attention about something :)
    – Obscuro
    Aug 30, 2017 at 14:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.