The question:
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.
The Solutions:
Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.
Method 1
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}HelperData $helperData,
....
) {
....
$this->helperData = $helperData;
....
}
public function doSomething()
{
return $this->helperData->doSomething();
}
Then you can call in your template $block->doSomething()
Method 2
You have to use like this:
$helper = $block->helper('{Vendor}{Module}HelperData');
$values = $helper->YourHelperMethod();
Method 3
You need to write whole class name in helper as below:
$this->helper('vendorenamemodulenameHelperhelpername')
You can use it in phtml file using above code
Method 4
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 CustommoduleReviewRatingBlock;
class HomehorizontalWidget extends MagentoFrameworkViewElementTemplate
{
protected $_helper;
public function __construct(
MagentoFrameworkViewElementTemplateContext $context,
array $data = [],
CustommoduleReviewRatingHelperData $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 CustommoduleReviewRatingHelper;
class Data extends MagentoFrameworkAppHelperAbstractHelper {
/** * @var MagentoFrameworkAppConfigScopeConfigInterfac
*/
protected $_scopeConfig;
CONST ENABLE = 'reviewrating/general/enable_module';
public function __construct( MagentoFrameworkAppHelperContext $context,
MagentoFrameworkAppConfigScopeConfigInterface $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
Method 5
Try this code in your Block:
protected $helperData;
public function __construct(
....
{Vendor}{Module}HelperData $helperData,
....
) {
....
$this->helperData = $helperData;
....
}
public function getHelper()
{
return $this->helperData;
}
And in you template, you can call:
$helper = $block->getHelper();
Method 6
$helper = $this->helper('{Vendor}{Module}HelperData::class');
$values = $helper->YourHelperMethod();
Method 7
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:
app/code/Vendor/Module/Helpers/Data.php:
class Data extends AbstractHelper
{
...
public function test($firstParam, $secondParam)
{
return "Test successfull p1: $firstParam, p2: $secondParam";
}
}
Just add this inside your block:
<block class="MagentoFrameworkViewElementTemplate"
name="example"
template="Vendor_Module::mytemplate.phtml">
<arguments>
<argument name="foo_bar" xsi:type="helper" helper="VendorModuleHelperData::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 8
Method 1.
$helper=
$this->helper(‘VendorModuleHelperData’)->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}HelperData $helperData,
....
) {
....
$this->helperData = $helperData;
....
}
public function getHelper()
{
return $this->helperData;
}
And in you template, you can call:
$helper = $block->getHelper();
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0