如何访问 Shopware 6 中的自定义 cms 元素配置值?

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

仅供参考:产品是我的 cms 元素名称。

如shopware 6教程所示,我已经创建了一个文件

DataResolver/ProductsCmsElementResolver.php

它有一个丰富的方法可以帮助扩展数据。在那里我尝试访问我的自定义 cms 元素的配置:

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

但是总是 returns 在元素注册期间设置的默认值:

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: ''
        }
    }
});

我完全按照以下指南中的说明进行操作:

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

谁能分享一个代码示例,您可以在其中获取配置值作为变量而不是静态值?

我假设您没有对配置组件中的产品数据进行任何进一步处理,因为您没有提及。 我建议查看默认的 cms 组件,例如 shopware/administration/Resources/app/administration/src/module/sw-cms/elements/product-slider/config/index.js,您可以在其中查看产品数据的处理方式:)

我做错的是我忘记在计算方法中写 .value:

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

不过我也在shopware源代码中发现了文档中没有提到的这样的函数调用:

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