Shopware 6全局自定义字段在次要语言上下文返回null问题咨询
解决Shopware自定义字段多语言上下文返回null的问题
要实现自定义字段仅存储单语言值、所有语言上下文返回一致结果,需修改两个关键配置:
- 为子自定义字段添加顶级
translated属性并设为false:这是控制字段是否启用多语言存储的核心配置,之前仅在config中设置的translated仅影响后台表单显示,不改变数据存储逻辑。 - 确保字段集和子字段的
config.translated保持false,避免后台出现多语言输入选项。
修改后的完整自定义字段集代码:
$customFields = [ [ 'name' => self::CUSTOM_FIELD_NAME, 'active' => true, 'global' => true, 'config' => [ 'label' => [ 'en-GB' => 'Password Protected Page', 'de-DE' => 'Passwortgeschützte Seite' ], 'translated' => false ], 'customFields' => [ [ 'name' => 'password_site_password', 'type' => \Shopware\Core\System\CustomField\CustomFieldTypes::TEXT, 'translated' => false, // 新增:控制字段是否启用多语言存储 'config' => [ 'label' => [ 'en-GB' => 'Password', 'de-DE' => 'Passwort', ], 'translated' => false ] ] ], 'relations' => [ [ 'entityName' => 'category' ] ] ] ]; $customFieldSetRepository = $this->getCustomFieldRepository(); if (!$this->customFieldsExist($customFieldSetRepository, $installContext->getContext())) { foreach ($customFields as $customFieldSet) { $customFieldSetRepository->upsert([$customFieldSet], $installContext->getContext()); } }
关键说明
Shopware的自定义字段有两层translated配置:
- 顶级
translated属性:决定字段值是否存储在翻译表中。设为false时,值会存在关联实体的主表,所有语言上下文都会读取同一个值,不会出现次要语言返回null的情况。 config.translated属性:仅控制后台管理界面是否显示多语言输入框,不影响数据的存储与读取逻辑。
如果插件已安装,需确保执行该配置的更新逻辑(比如在插件的update方法中重新执行upsert),让修改生效。
内容的提问来源于stack exchange,提问作者MweisSW
相关产品推荐
相关产品推荐

