Search code examples
shopwareshopware6

Load a layout in my custom plugin storefront page


I have a custom plugin built for Shopware 6. I created a new route /certificate with a certificate.html.twig view to show a list of the custom entity along with other html contents.

I want to be able to edit the HTML contents of this page (not the list) from the administration area, so I went to 'Content' > 'Shopping Experience' and created a custom layout.

I inserted my HTML tags as text type (in the source code interface).

The problem: I want to link this created layout with the custom twig template I created (or any other approach to make this HTML content editable from the administration).

How to load the layout in my controller at least?


Solution

  • In your controller you fetch the cms_page entity with the id of your shopping experience layout. You then pass it to your template

    $criteria = new Criteria([$cmsPageId]);
    $criteria->addAssociation('sections.blocks');
    
    $cmsPage = $this->container->get('cms_page.repository')->search($criteria, $context)->first();
    
    return $this->renderStorefront('@MyPlugin/storefront/page/certificate/certificate.html.twig', ['cmsPage' => $cmsPage]);
    

    In your custom template you can then include the template for rendering the shopping experience.

    {% set cmsPageClasses = ('cms-page ' ~ cmsPage.cssClass|striptags)|trim %}
    <div class="{{ cmsPageClasses }}">
        {% sw_include "@Storefront/storefront/page/content/detail.html.twig" with {'cmsPage': cmsPage} %}
    </div>