Pergunta

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?

Foi útil?

Solução 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"/>

Outras dicas

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() ?>

I faced same behavior recently, I removed some blocks and containers of XML layout. I was looking for get only a piece of a page without anything else (with update handle). but instead of add a new block I just:

<move element="formkey" destination="myContainerOrMyBlock"/>

because I was defined in theme default.xml:

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

Once I removed it with:

 <referenceContainer name="page.wrapper" remove="true"/>
Licenciado em: CC-BY-SA com atribuição
Não afiliado a magento.stackexchange
scroll top