将Shopware 6标签附加到属性值

huangapple go评论72阅读模式
英文:

Attach Shopware 6 tags to Property values

问题

有人能否就如何将Shopware 6的标签附加到属性(仅一个值)上提供建议?

是否有一种方法可以重用某些组件?我认为标签是使用数据关联完成的。

--

我已经设置好了,但现在我遇到了这个错误(在单击后端管理选项列表中的属性选项详细信息模态框时无法打开):

app.js?16886789168584353:2 在当前模块中捕获到了一个错误:ReferenceError: option未定义

当我删除选择器时,它可以正常打开:

<sw-entity-tag-select
    v-if="option"
    v-model="option.extensions.myTags"
    :label="$tc('global.sw-tag-field.title')"
/>

以下是js代码:

Shopware.Component.override('sw-property-option-detail', {
    inject: [
        'repositoryFactory'
    ],

    template,

    data: function () {
        return {
            option: undefined
        }
    },

    computed: {
        repository() {
            return this.repositoryFactory.create('a7filter_property_group_option_tag');
        },
        criteria() {
            const criteria = new Criteria();
            criteria.addAssociation('myTags');

            return criteria;
        },
    },

    created() {
        this.repository = this.repositoryFactory.create('a7filter_property_group_option_tag');

        this.repository.search(this.criteria, Shopware.Context.api)
            .then(option => {
                this.option = option;
            });
    }
});

也许有人可以提供建议?

英文:

Can someone advice on making it possible to attach Shopware 6 tags to Property (only one) values?

Is there a way to reuse some components? I assume tags are done using data associations.

--

I have set it up, but now I get this error (while not being able to open Property option detail modal on click in a backend administration options list):

app.js?16886789168584353:2 An error was captured in current module: ReferenceError: option is not defined

It opens well, when I remove the selector:

    &lt;sw-entity-tag-select
        v-if=&quot;option&quot;
        v-model=&quot;option.extensions.myTags&quot;
        :label=&quot;$tc(&#39;global.sw-tag-field.title&#39;)&quot;
    /&gt;

Here is the js:

Shopware.Component.override(&#39;sw-property-option-detail&#39;, {
    inject: [
    	&#39;repositoryFactory&#39;
	],

	template,

	data: function () {
        return {
            option: undefined
        }
    },

    computed: {
        repository() {
            return this.repositoryFactory.create(&#39;a7filter_property_group_option_tag&#39;);
        },
        criteria() {
            const criteria = new Criteria();
            criteria.addAssociation(&#39;myTags&#39;);

            return criteria;
        },
    },

    created() {
        this.repository = this.repositoryFactory.create(&#39;a7filter_property_group_option_tag&#39;);

        this.repository.search(this.criteria, Shopware.Context.api)
            .then(option =&gt; {
                this.option = option;
            });
    }
});

Maybe someone can give an advice?

答案1

得分: 3

Create a custom entity for the mapping

class MyPropertyGroupOptionTagDefinition extends MappingEntityDefinition
{
    final public const ENTITY_NAME = 'my_property_group_option_tag';

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

    protected function defineFields(): FieldCollection
    {
        return new FieldCollection([
            (new FkField('option_id', 'optionId', PropertyGroupOptionDefinition::class))->addFlags(new PrimaryKey(), new Required()),
            (new FkField('tag_id', 'tagId', TagDefinition::class))->addFlags(new PrimaryKey(), new Required()),
            new ManyToOneAssociationField('option', 'option_id', PropertyGroupOptionDefinition::class, 'id', false),
            new ManyToOneAssociationField('tag', 'tag_id', TagDefinition::class, 'id', false),
        ]);
    }
}

Create an EntityExtension

class PropertyGroupOptionExtension extends EntityExtension
{
    public function extendFields(FieldCollection $collection): void
    {
        $collection->add(
            new ManyToManyAssociationField('myTags', TagDefinition::class, MyPropertyGroupOptionTagDefinition::class, 'option_id', 'tag_id'),
        );
    }

    public function getDefinitionClass(): string
    {
        return PropertyGroupOptionDefinition::class;
    }
}

Add association to criteria for property_group_option where they are fetched

criteria.addAssociation('myTags');

In the template of the component you can use sw-entity-tag-select for assigning tags

<sw-entity-tag-select
    v-if="option"
    v-model="option.extensions.myTags"
    :label="$tc('global.sw-tag-field.title')"
/>
英文:

Create a custom entity for the mapping

class MyPropertyGroupOptionTagDefinition extends MappingEntityDefinition
{
    final public const ENTITY_NAME = &#39;my_property_group_option_tag&#39;;

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

    protected function defineFields(): FieldCollection
    {
        return new FieldCollection([
            (new FkField(&#39;option_id&#39;, &#39;optionId&#39;, PropertyGroupOptionDefinition::class))-&gt;addFlags(new PrimaryKey(), new Required()),
            (new FkField(&#39;tag_id&#39;, &#39;tagId&#39;, TagDefinition::class))-&gt;addFlags(new PrimaryKey(), new Required()),
            new ManyToOneAssociationField(&#39;option&#39;, &#39;option_id&#39;, PropertyGroupOptionDefinition::class, &#39;id&#39;, false),
            new ManyToOneAssociationField(&#39;tag&#39;, &#39;tag_id&#39;, TagDefinition::class, &#39;id&#39;, false),
        ]);
    }
}

Create an EntityExtension

class PropertyGroupOptionExtension extends EntityExtension
{
    public function extendFields(FieldCollection $collection): void
    {
        $collection-&gt;add(
            new ManyToManyAssociationField(&#39;myTags&#39;, TagDefinition::class, MyPropertyGroupOptionTagDefinition::class, &#39;option_id&#39;, &#39;tag_id&#39;),
        );
    }

    public function getDefinitionClass(): string
    {
        return PropertyGroupOptionDefinition::class;
    }
}

Add association to criteria for property_group_option where they are fetched

criteria.addAssociation(&#39;myTags&#39;);

In the template of the component you can use sw-entity-tag-select for assigning tags

&lt;sw-entity-tag-select
    v-if=&quot;option&quot;
    v-model=&quot;option.extensions.myTags&quot;
    :label=&quot;$tc(&#39;global.sw-tag-field.title&#39;)&quot;
/&gt;

huangapple
  • 本文由 发表于 2023年6月22日 01:23:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76525763.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定