How to overwrite a core element by adding a new defaultConfig field in Shopware 6

How to overwrite a core element by adding a new defaultConfig field in Shopware 6

·

1 min read

I will do an example with the product listing cms and add a new field name is hungMac.

In my plugin, I will create an index.js file at src/Resources/app/administration/src/module/sw-cms/elements/product-listing/index.js

const cmsElements = Shopware.Service('cmsService').getCmsElementRegistry();

// Get the cms element of product-listing cms
const cmsElProductListing = cmsElements['product-listing'];

// Check the field named HungMac does not exist yet
if (!cmsElProductListing.defaultConfig.hungMac) {
    cmsElProductListing.defaultConfig = {
        ...cmsElProductListing.defaultConfig,
        hungMac: {
            source: 'static',
            value: 'this is my data'
        },
    }

Note: after adding, you should remove the old element in your layout and then add it again.