英文:
Retrieving custom entity data in Shopware's CMS editor from component
问题
在一个Shopware的vue组件中,用于管理中的编辑器/设计师,我有以下代码来从自定义实体中获取数据:
这几乎有效。例如,它向API发送HTTP请求并检索到了预期的数据;问题在于数据从未添加到this.element.data;team属性被添加,但始终以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:
-
Rather than saving all the data in the CMS element config, I want to have it fetched from the backend when editing a page.
-
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.
-
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);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论