Search code examples
shopware

How to access custom cms element config values in Shopware 6?


FYI: Products is my cms element name.

As shown in shopware 6 guides, I have created a file

DataResolver/ProductsCmsElementResolver.php

which has an enrich method which helps to extend data. In there I try to access the configs of my custom cms element with:

$config = $slot->getFieldConfig();
$productListType = $config->get('products')->getValue();

That, however always returns the default value that was set during the registration of the element:

Shopware.Service('cmsService').registerCmsElement({
    name: 'products',
    label: 'das.elements.customProductsElement.label',
    component: 'sw-cms-el-products',
    configComponent: 'sw-cms-el-config-products',
    previewComponent: 'sw-cms-el-preview-products',
    defaultConfig: {
        products: {
            source: 'static',
            value: ''
        }
    }
});

I did it exactly as it is shown in the following guides:

https://developer.shopware.com/docs/guides/plugins/plugins/content/cms/add-cms-element

https://developer.shopware.com/docs/guides/plugins/plugins/content/cms/add-data-to-cms-elements#create-a-data-resolver

Could anyone share a sample of code where you get the value of config as a variable and not static value?


Solution

  • What I was doing wrong was that I forgot to write the .value in computed methods:

    computed: {
            products() {
                return this.element.config.products.value;
            }
        },
    

    However I also found such function call in shopware source codes which was not mentioned in the docs:

    methods: {
            createdComponent() {
                this.initElementConfig('youtube-video');
                this.initElementData('youtube-video'); // this line was not present in docs
            }
        }