我正在开发Shopware 5 ShoppingWorld元素的路上。我的计划是用户可以从下拉框中选择类别。到目前为止,这很好。我在下拉框中使用以下代码生成:
$vimeoElement->createComboBoxField([
'name' => 'category_selection',
'fieldLabel' => 'Kategorie-Auswahl',
'allowBlank' => true,
'valueField' => 'id',
'displayField' => 'name',
'store' => 'Shopware.apps.Emotion.store.CategoryPath',
]);
我的问题是,我需要有关此类别(链接,描述等)的所有信息
我在Shopware文档中进行了搜索,发现我需要使用ComponentHandler来准备数据,然后才能传递给前端。
我遵循本教程:https://developers.shopware.com/developers-guide/custom-shopping-world-elements/#process-the-lement-data-before-fore-fore-fore-fore-utput 在/custom/plugins/MyPluginName/ComponentHandler/VimeoComponentHandler.php
并添加以下代码:
<?php
namespace MyPluginNameComponentHandler;
class VimeoComponentHandler implements ComponentHandlerInterface
{
public function supports(Element $element)
{
return true;
// return $element->getComponent()->getTemplate() === 'emotion_vimeo';
}
public function prepare(PrepareDataCollection $collection, Element $element, ShopContextInterface $context)
{
// do some prepare logic, e.g. requesting articles for rendering
}
public function handle(ResolvedDataCollection $collection, Element $element, ShopContextInterface $context)
{
// do some handle logic and fill the element data, which will be available in your template
$element->getData()->set('key', 'value');
}
}
?>
最后,我将此行添加到/custom/plugins/mypluginname/resources/services.xml:
<service id="swag_vimeo_element.vimeo_component_handler" class="MyPluginNameComponentHandlerVimeoComponentHandler">
<tag name="shopware_emotion.component_handler"/>
</service>
现在,我的前端似乎应该得到一个新变量。但事实并非如此。
在我的apache日志中,我可以看到以下错误:
PHP Fatal error: Interface 'MyPluginName\ComponentHandler\ComponentHandlerInterface' not found in C:\xampp\htdocs\custom\plugins\MyPluginName\ComponentHandler\VimeoComponentHandler.php on line 6, referer: http://192.168.3.64/
我需要在前端模板中获取有关类别的所有信息。我只有类别ID。
在您的VimeoComponentHandler
中,看起来您忘了导入ComponentHandlerInterface
:
<?php
namespace MyPluginNameComponentHandler;
// Add following line
use ShopwareBundleEmotionBundleComponentHandlerComponentHandlerInterface;
class VimeoComponentHandler implements ComponentHandlerInterface
{
// ...
}
?>