从Shopware的CMS编辑器中检索自定义实体数据,来自组件

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

Retrieving custom entity data in Shopware's CMS editor from component

问题

在一个Shopware的vue组件中用于管理中的编辑器/设计师我有以下代码来从自定义实体中获取数据

这几乎有效例如它向API发送HTTP请求并检索到了预期的数据问题在于数据从未添加到this.element.datateam属性被添加但始终以null作为值填充

我试图从Shopware自己的代码中寻找灵感他们有与此非常相似的东西但我不确定我是否在错误的上下文中使用了这段代码

在开发者工具中我可以看到XHR请求被执行并返回了预期的数据因此这可能只是一个小问题

我想要做的是

1. 与其将所有数据保存在CMS元素配置中我希望在编辑页面时从后端获取它

2. 在使用编辑器保存带有自定义CMS元素的页面时应在元素配置中保存实体的ID该ID在其他地方在编辑器和商店前端用于从实体中检索数据

3. 最后在商店前端中我也会重新获取数据我还没有开始编写这部分代码但希望这是可能的
英文:

In a Shopware vue component, for the editor/designer in the administration, I got the following code to fetch data from a custom entity:

fetchTeam({ targetId }) {
  this.teamRepository.get(targetId).then((team) => {
    this.$set(this.element.data, 'team', team);
  });
},

This almost works. E.g. It sends a HTTP request to the API and retrieves the expected data; the problem is that the data is never added to this.element.data; The team property is added, but it is always filled with null as value.

I was trying to find inspiration from Shopware's own code, and they have something very similar to this, but I am not sure if I am using the code in the wrong context.

In developer tools I can see that the XHR request is performed, and it returns the expected data, so presumably this is just a minor issue.

What I want to do:
My idea is this:

  1. Rather than saving all the data in the CMS element config, I want to have it fetched from the backend when editing a page.

  2. When saving the page with the custom CMS element on from the editor, an ID for the entity should be saved in the element config, which is used elsewhere (in the editor and storefront) to retrieve data from the entity.

  3. Finally, in the storefront I would also re-fetch the data. I have not started this code yet, but hope it is possible.

答案1

得分: 2

你的想法是正确的。在你的元素配置中存储实体的ID,然后创建一个 CMS 元素解析器,使用该ID获取元素的数据。

class MyElementResolver extends AbstractCmsElementResolver
{
    public function getType(): string
    {
        return 'myElementType';
    }

    public function collect(CmsSlotEntity $slot, ResolverContext $resolverContext): ?CriteriaCollection
    {
        $teamConfig = $slot->getFieldConfig()->get('team');

        if (!$teamConfig) {
            return null;
        }

        $teamId = $teamConfig->getValue();
        $criteria = new Criteria([$teamId]);

        $criteriaCollection = new CriteriaCollection();
        $criteriaCollection->add('team_' . $slot->getUniqueIdentifier(), TeamDefinition::class, $criteria);

        return $criteriaCollection;
    }

    public function enrich(CmsSlotEntity $slot, ResolverContext $resolverContext, ElementDataCollection $result): void
    {
        $searchResult = $result->get('team_' . $slot->getUniqueIdentifier());
        if (!$searchResult) {
            return;
        }

        $teamConfig = $slot->getFieldConfig()->get('team');

        $team = $searchResult->get($teamConfig->getValue());
        if (!$team instanceof TeamEntity) {
            return;
        }

        $slot->setData($team);
    }
}
英文:

You've got the right idea. Store the ID of the entity in your elements config and then create a CMS Element Resolver that fetches the data for your element using that ID.

class MyElementResolver extends AbstractCmsElementResolver
{
    public function getType(): string
    {
        return 'myElementType';
    }

    public function collect(CmsSlotEntity $slot, ResolverContext $resolverContext): ?CriteriaCollection
    {
        $teamConfig = $slot->getFieldConfig()->get('team');

        if (!$teamConfig) {
            return null;
        }

        $teamId = $teamConfig->getValue();
        $criteria = new Criteria([$teamId]);

        $criteriaCollection = new CriteriaCollection();
        $criteriaCollection->add('team_' . $slot->getUniqueIdentifier(), TeamDefinition::class, $criteria);

        return $criteriaCollection;
    }

    public function enrich(CmsSlotEntity $slot, ResolverContext $resolverContext, ElementDataCollection $result): void
    {
        $searchResult = $result->get('team_' . $slot->getUniqueIdentifier());
        if (!$searchResult) {
            return;
        }

        $teamConfig = $slot->getFieldConfig()->get('team');

        $team = $searchResult->get($teamConfig->getValue());
        if (!$team instanceof TeamEntity) {
            return;
        }

        $slot->setData($team);
    }
}

huangapple
  • 本文由 发表于 2023年8月10日 15:12:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76873381.html
匿名

发表评论

匿名网友

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

确定