Display Widget Form Programmatically - Alekseon/magento2-widget-forms GitHub Wiki

Its possible to display Widget Form programtically on frontend.

You can display form by Form Id or by Form Identifier:

  • Form Id - entity id from database
  • Form Identifier - editable identifier, which can be set to any post by admin (General tab on manage form view)

Note: widget form will be visible on frontend ony if "Can use as frontend widget" is "Yes".

In layout xml file

Example:

    <referenceContainer name="content">
        <block class="Alekseon\WidgetForms\Block\WidgetForm" name="myForm">
            <arguments>
                <argument name="form_identifier" xsi:type="string">my_form_code</argument>
            </arguments>
        </block>
    </referenceContainer>

Alternatively you can use id instead of identifier:

    <argument name="form_id" xsi:type="string">1</argument>

Optionally You can use below params:

    <argument name="hide_description" xsi:type="boolean">true</argument>
    <argument name="hide_title" xsi:type="boolean">true</argument>

In PHP Code

By Form ID

echo $block->getLayout()->createBlock('Alekseon\WidgetForms\Block\WidgetForm')
           ->setFormId(1)
           ->toHtml();

or by Form code

echo $block->getLayout()->createBlock('Alekseon\WidgetForms\Block\WidgetForm')
           ->setFormIdentifier('my_form_code')
           ->toHtml();
⚠️ **GitHub.com Fallback** ⚠️