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?

  • Are you using Magento 2 ? – Pawan Jun 18 '18 at 16:22
  • Yes, I've tagged magento 2 and 2.2 – jamil Jun 18 '18 at 16:38

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() ?>
  • Thanks I will try. I'm confused because throughout magento core the function I was trying seems to work without these steps. Any idea why that is? For example: \Magento\Catalog\Block\Product\ListProduct via Magento_Catalog::view/frontend/templates/product/list.phtml – jamil Jun 18 '18 at 16:37
  • Throughout magento core extended AbstractBlock class. they will define the function in that. so we can also extend the same way they did. it also will work. – Vishnunath Jun 18 '18 at 16:49

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"/>

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.