Magento2 – How to Display PageBuilder Content in Custom Product Tab

frontendmagento2.4.3page-builder

I have several custom tabs on my product pages that pull data from product variables. The tabs are defined in catalog_product_view.xml and it all seems to work as expected except the data are PageBuilder fields and do not render the HTML. Instead I get "raw" HTML dumped out as the contents of the tab.

Tab definition:

<referenceBlock name="product.info.details">
        <block class="Magento\Catalog\Block\Product\View" name="galleryinfo.tab" as="galleryinfo" template="Magento_Catalog::product/view/custom_attribute_tab.phtml" group="detailed_info" >
           <arguments>
              <argument translate="true" name="title" xsi:type="string">Gallery</argument>
              <argument name="code" xsi:type="string">gallery_tab</argument>              
                <argument name="sort_order" xsi:type="string">60</argument> 
           </arguments>             
        </block>
        <block class="Magento\Catalog\Block\Product\View" name="compatinfo.tab" as="compatibilityinfo" template="Magento_Catalog::product/view/custom_attribute_tab.phtml" group="detailed_info" >
             <arguments>
                <argument translate="true" name="title" xsi:type="string">Compatibility</argument>
              <argument name="code" xsi:type="string">compatibility_tab</argument>                 
                <argument name="sort_order" xsi:type="string">50</argument>
             </arguments>
          </block>

Essentially, I want to duplicate how the "Description" field works in the backend and display content on a tab like the product description.

Template:

<?php echo $block->escapeHtml($block->getProduct()->getData($this->getCode())); ?>

I'm sure this is where the problem is but I just don't find how to render the content. Seems like there should be a ->toHtml() in there, but it doesn't help. How do I render PageBuilder data field(s) associated with a product and render them as HTML in product page tabs?

Glorious Output:

<div data-content-type="block" data-appearance="default" data-element="main">{{widget type="Magento\Cms\Block\Widget\Block" template="widget/static_block/default.phtml" block_id="34" type_name="CMS Static Block"}}</div>

Best Answer

The following, adapted for your compatibility tab needs, is how the native description tab ("Details" - vendor/magento/module-catalog/view/frontend/layout/catalog_product_view.xml) would added in Magento:

<referenceBlock name="product.info.details">
    <block class="Magento\Catalog\Block\Product\View\Description" name="compatibility.tab" as="compatibility_tab" template="Magento_Catalog::product/view/attribute.phtml" group="detailed_info">
        <arguments>
            <argument name="at_call" xsi:type="string">getCompatibilityTab</argument>
            <argument name="at_code" xsi:type="string">compatibility_tab</argument>
            <argument name="css_class" xsi:type="string">your_compatibility_tab_css_class</argument>
            <argument name="at_label" xsi:type="string">none</argument>
            <argument name="title" translate="true" xsi:type="string">Compatibility</argument>
            <argument name="sort_order" xsi:type="string">50</argument>
        </arguments>
    </block>
</block>

Since "description" is an attribute of input type "Page Builder" already, this approach should be working for your custom attribute PB-type fields too without even needing to create a custom template.

Related Topic