1

My custom module xml file contains the block code. I want to call that block code directly using createBlock method and set template to show in specific template. My block code is adding animated fancing lines to form.

If i mention in xml using following format then it works fine. How can i display same block using createBlock method?

<referenceContainer name="form.contact.additional.info">
        <block class="Jackson\Wyss\Block\Frontend\Wyss" name="Wyss-reca" after="-"
               template="Jackson_Wyss::wyss.phtml">

            <arguments>
                <argument name="jsLayout" xsi:type="array">
                    <item name="components" xsi:type="array">
                        <item name="wyssdata" xsi:type="array">
                            <item name="component" xsi:type="string">Jackson_Wyss/js/reca</item>
                            <item name="zone" xsi:type="string">contact</item>
                        </item>
                    </item>
                </argument>
            </arguments>
        </block>
    </referenceContainer>

for example:

 $block->getLayout()->createBlock('Jackson\wyss\Block\Frontend\Reca')->setData('jsLayout', '5')->setTemplate('goodtest/test.phtml')->toHtml();

In above xml code zone is form type name. I want to set different name for all the forms in createBlock method. is there anyway we can createBlock method for above xml code?

How can i make createBlock method of my above xml code?

  • You can try using as childblock. Add this block as as child where in you want to call and then call using getChildBlock – Narendra Vyas Apr 1 at 11:48
  • Thanks for your reply. So if i call it using the childBlock will it consider the paramaters defined under the (in your example)<item name="instant-purchase" xsi:type="array"> <item name="component" xsi:type="string">Magento_InstantPurchase/js/view/instant-purchase</item> <item name="config" xsi:type="array"> I want to make sure that in zone (in my code) applied there. – Jackson Wyss Apr 1 at 14:35
  • Yes it do. I have used it as mentioned in below code. YOu can try and let me know if in case of any issue – Narendra Vyas Apr 2 at 5:47
0

Try using calling as ChildBlock, like the bellow example:

<!-- Add Instant purchase on Recently viewed products -->
    <referenceBlock name="catalog.product.related" >
        <block name="product.info.addtocart.instantPurchase.additional.recently" class="Magento\InstantPurchase\Block\Button" template="Magento_InstantPurchase::button.phtml" before="-">
            <arguments>
                <argument name="jsLayout" xsi:type="array">
                    <item name="components" xsi:type="array">
                        <item name="instant-purchase" xsi:type="array">
                            <item name="component" xsi:type="string">Magento_InstantPurchase/js/view/instant-purchase</item>
                            <item name="config" xsi:type="array">
                                <item name="template" xsi:type="string">Magento_InstantPurchase/instant-purchase</item>
                            </item>
                        </item>
                    </item>
                </argument>
            </arguments>
        </block>
    </referenceBlock>

In parent .phtml:

<?php
   if($block->getChildBlock('product.info.addtocart.instantPurchase.additional.recently', true))
  {
                                                    $block->getChildBlock('product.info.addtocart.instantPurchase.additional.recently')->setProduct($_product);

 echo $block->getChildHtml('product.info.addtocart.instantPurchase.additional.recently', false);
}

?>

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.