Hung Mac
Hung Mac's memory

Hung Mac's memory

How to add data to the cms element in Shopware 6

How to add data to the cms element in Shopware 6

Subscribe to my newsletter and never miss my upcoming articles

In the Backend

you need to create a new class and extend AbstractCmsElementResolver

class MyCmsNameDataResolver extends AbstractCmsElementResolver
{
    public function getType(): string
    {
        // Change it to your cms name
        return 'my-cms-name';
    }

    public function collect(CmsSlotEntity $slot, ResolverContext $resolverContext): ?CriteriaCollection
    {
        // Here for the CriteriaCollection
        return null;
    }

    public function enrich(CmsSlotEntity $slot, ResolverContext $resolverContext, ElementDataCollection $result): void
    {
        // Add your data here, I suggest you should create a new struct for your data instead of ArrayStruct
        $slot->setData(ArrayStruct(['demo' => 'Hung Mac']));
    }
}

In your services.xml

Remember change id part of the service to yours

<service id="MacMemory\DataResolver\Element\MyCmsNameDataResolver">
    <tag name="shopware.cms.data_resolver"/>
</service>

In the Frontend

For example, your cms path here src/Resources/views/storefront/element/cms-element-my-cms-name.html.twig

Add this code to your cms file

{% block element_my_cms_name %}
    {{ dump(element.data) }}
{% endblock %}
 
Share this