Trying to develop something in magento 2. But, I didn't find how to call a Helper method in template(.phtml) file.
I want a replacement of below code:
$this->helper('modulename/helpername')->methodname();
If anyone knows please help me.
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityTrying to develop something in magento 2. But, I didn't find how to call a Helper method in template(.phtml) file.
I want a replacement of below code:
$this->helper('modulename/helpername')->methodname();
If anyone knows please help me.
You should not use helper calls directly in the template.
Have your helper instance provided as a dependency to the block that renders the template and create a method in your block that calls the helper and call that method in the template.
Have your block defined like this
protected $helperData;
public function __construct(
....
\{Vendor}\{Module}\Helper\Data $helperData,
....
) {
....
$this->helperData = $helperData;
....
}
public function doSomething()
{
return $this->helperData->doSomething();
}
Then you can call in your template $block->doSomething()
You have to use like this:
$helper = $this->helper('{Vendor}\{Module}\Helper\Data');
$values = $helper->YourHelperMethod();
You need to write whole class name in helper as below:
$this->helper('vendorename\modulename\Helper\helpername')
You can use it in phtml file using above code
I used this code in one of my module.
Change Custommodule to NameSpace ( Your company Name) change ReviewRating to ( Your Module Name)
In /var/www/html/magento2/app/code/Custommodule/ReviewRating/Block/HomehorizontalWidget.php
<?php
namespace Custommodule\ReviewRating\Block;
class HomehorizontalWidget extends \Magento\Framework\View\Element\Template
{
protected $_helper;
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
array $data = [],
\Custommodule\ReviewRating\Helper\Data $helper
) {
parent::__construct($context, $data);
$this->_helper = $helper;
}
public function getEnable(){
return $this->_helper->getEnable();
}
}
In /var/www/html/magento2/app/code/Custommodule/ReviewRating/view/frontend/templates/homehorizontalwidget.phtml
<?php echo $block->getEnable(); ?>
In /var/www/html/magento2/app/code/Custommodule/ReviewRating/Helper/Data.php
<?php
namespace Custommodule\ReviewRating\Helper;
class Data extends \Magento\Framework\App\Helper\AbstractHelper {
/** * @var \Magento\Framework\App\Config\ScopeConfigInterfac
*/
protected $_scopeConfig;
CONST ENABLE = 'reviewrating/general/enable_module';
public function __construct( \Magento\Framework\App\Helper\Context $context,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig ) {
parent::__construct($context); $this->_scopeConfig = $scopeConfig;
}
public function getEnable(){
return $this->_scopeConfig->getValue(self::ENABLE);
}
}
In /var/www/html/magento2/app/code/Custommodule/ReviewRating/etc/adminhtml/system.xml
system configuration labels created here
Try this code in your Block:
protected $helperData;
public function __construct(
....
\{Vendor}\{Module}\Helper\Data $helperData,
....
) {
....
$this->helperData = $helperData;
....
}
public function getHelper()
{
return $this->helperData;
}
And in you template, you can call:
$helper = $block->getHelper();
$helper = $this->helper('{Vendor}\{Module}\Helper\Data::class');
$values = $helper->YourHelperMethod();
The best way is by injecting the helper in the block layout as shown in the magento documentation. This is called "Argument Injection"
Lets say this is your helper method:
class Data extends AbstractHelper
{
...
public function test($firstParam, $secondParam)
{
return "Test successfull p1: $firstParam, p2: $secondParam";
}
}
Just add this inside your block:
<block class="Magento\Framework\View\Element\Template"
name="example"
template="Vendor_Module::mytemplate.phtml">
<arguments>
<argument name="foo_bar" xsi:type="helper" helper="Vendor\Module\Helper\Data::test">
<param name="firstParam">firstValue123</param>
<param name="secondParam">secondValue456</param>
</argument>
</arguments>
</block>
Then you can call the helper in the template:
/** @var mixed $helperMethodResult */
$helperMethodResult = $block->getData('foo_bar'); // or $block->getFooBar()
Now clear the cache php bin/magento cache:flush
and reload the site where your block is getting loaded.
Output: Test successfull p1: firstValue123, p2: secondValue456
Method 1.
$helper= $this->helper('Vendor\Module\Helper\Data')->YourMethodName();
You can use above code directly in phtml file.
Method 2
Try this code in your Block:
protected $helperData;
public function __construct(
....
\{Vendor}\{Module}\Helper\Data $helperData,
....
) {
....
$this->helperData = $helperData;
....
}
public function getHelper()
{
return $this->helperData;
}
And in you template, you can call:
$helper = $block->getHelper();