Question

I'm using the code...

<?php echo $block->getBlockHtml('formkey') ?>

inside my custom phtml file but all I get is blank ''. I see this same code used throughout Magento Core.

How can I enable this functionality in my template?

Was it helpful?

Solution 2

My XML was removing the columns block.

<referenceBlock name="columns" remove="true"/>

I'm not sure why, but this breaks a lot of functionality in Magento, including the ability to use $block-getFormKey() in an unrelated template.

UPDATE: I ran into this issue again and found that the formkey block was not defined. Added this to my xml and things worked.

<block class="Magento\Framework\View\Element\FormKey" name="formkey"/>

OTHER TIPS

Add construct function and try to add in your Customized Block file

 public function __construct(\Magento\Framework\Data\Form\FormKey $formKey){
    $this->formKey = $formKey
 }
 public function getFormKey()
 {
     return $this->formKey;
 }

call this function in your custom phtml file

 <?= $block->getFormKey() ?>
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top