Shopware 6 - 用于管理中实体扩展的表单字段

Gab*_*iel 3 shopware shopware6

这是 Shopware 6 的问题。我想PromotionEntity通过添加一个max_budget字段并将其显示为管理中的表单字段来扩展。目前仅存在max_redemptions_global和字段。max_redemptions_per_customer该字段应出现在和字段max_budget下方的管理中。其行为与其他两个类似。如果此促销活动的总订单折扣达到 max_budget 中的值,则促销活动不再有效。max_redemptions_globalmax_redemptions_per_customermax_budget

所以我创建了一个实体扩展,如下所示:

class PromotionMaxBudgetExtension extends EntityExtension
{
    public function extendFields(FieldCollection $collection): void
    {
        $collection->add(
            new OneToOneAssociationField('maxBudget', 'id', 'promotion_id', PromotionMaxBudgetDefinition::class, false)
        );
    }

    public function getDefinitionClass(): string
    {
        return PromotionDefinition::class;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后是扩展的定义:

class PromotionMaxBudgetDefinition extends EntityDefinition
{
    public const ENTITY_NAME = 'promotion_max_budget';

    public function getEntityName(): string
    {
        return self::ENTITY_NAME;
    }

    public function getEntityClass(): string
    {
        return PromotionMaxBudgetEntity::class;
    }

    protected function defineFields(): FieldCollection
    {
        return new FieldCollection([
            (new IdField('id', 'id'))->addFlags(new Required(), new PrimaryKey()),
            (new FkField('promotion_id', 'promotionId', PromotionDefinition::class)),
            (new IntField('max_budget', 'maxBudget')),
            (new IntField('left_budget', 'leftBudget')),

            (new OneToOneAssociationField('promotion', 'promotion_id', 'id', PromotionDefinition::class, false))
        ]);
    }
}
Run Code Online (Sandbox Code Playgroud)

为了显示该字段,我必须覆盖sw-promotion-v2-detail-base. 所以我修改 sw-promotion-v2-detail-base.html.twig如下:

{% block sw_promotion_v2_detail_base_general_max_uses_customer %}
{% parent %}

<sw-number-field
        v-model="??????"
        class="sw-promotion-v2-detail-base__field-max-uses-per-customer"
        number-type="int"
        :label="Max budget"
        :placeholder="$tc('sw-promotion-v2.detail.base.general.maxUsesPerCustomerPlaceholder')"
        :disabled="!acl.can('promotion.editor')"
        allow-empty
/>
{% endblock %}
Run Code Online (Sandbox Code Playgroud)

所以,我的问题是,我如何告诉 Shopware 该字段用于 max_budget 实体扩展,以便它保存我对其所做的更改,在onSave似乎即使实体扩展存在,它也不会作为促销的关联来获取(即使自动加载为 true)。

Gab*_*iel 6

好的,我明白了如何才能完美地工作。

首先,我必须重写sw-promotion-v2-detail组件并添加以下 js:

Shopware.Component.override('sw-promotion-v2-detail', {
    computed: {
        promotionCriteria() {
            const criteria = this.$super('promotionCriteria');

            criteria.addAssociation('maxBudget');

            return criteria;
        }
    },
});
Run Code Online (Sandbox Code Playgroud)

其次,在 sw-promotion-v2-detail-base.html.twig 中,该字段将如下所示:

<sw-number-field
        v-if="promotion.extensions.maxBudget"
        v-model="promotion.extensions.maxBudget.maxBudget"
        class="sw-promotion-v2-detail-base__field-max-budget"
        number-type="int"
        :label="$tc('vouchers.maxBudget')"
        :placeholder="$tc('sw-promotion-v2.detail.base.codes.individual.generateModal.labelPrefix')"
        :disabled="!acl.can('promotion.editor')"
        allow-empty
/>
Run Code Online (Sandbox Code Playgroud)

请注意,v 模型更改为promotion.extensions......

所以答案是这样的: 要在管理中为实体扩展添加 Shopware 6 表单字段,您必须将扩展的实体关联添加到提取扩展实体的组件(在本例中为 sw-promotion-v2-detail )。之后,您可以使用 twig 中的扩展名promotion.extensions.extensionName......