简体   繁体   English

Shopware6 中具有自定义事件的自定义实体

[英]Custom Entity with Custom Event in Shopware6

For several days I have been trying to create plugin with custom entity and event in Shopware6 because I want to add my own entity to email templates.几天来,我一直在尝试在 Shopware6 中创建带有自定义实体和事件的插件,因为我想将自己的实体添加到 email 模板中。

What do I want to do?我想做什么?

In each email template I want to use my entity: {{ customEntity.technicalName[0] }} (the field is loaded from the database).在每个 email 模板中,我想使用我的实体: {{ customEntity.technicalName[0] }} (该字段是从数据库加载的)。

so I choose this repository from Shopware github: swag-docs-custom-entity and I tried to create a custom event.所以我从 Shopware github: swag swag-docs-custom-entity选择了这个存储库,并尝试创建一个自定义事件。

<?php
namespace Swag\CustomEntity\Event;

use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\Event\EventData\EntityType;
use Shopware\Core\Framework\Event\EventData\EventDataCollection;
use Shopware\Core\Framework\Event\EventData\MailRecipientStruct;
use Shopware\Core\Framework\Event\MailActionInterface;
use Swag\CustomEntity\Custom\CustomEntity;
use Swag\CustomEntity\Custom\CustomEntityDefinition;

class CustomEvent extends Event implements MailActionInterface
{
    public const EVENT_NAME = 'custom.entity';

    /** @var CustomEntity */
    private $customEntity;

    /** @var Context */
    private $context;

    public function __construct(CustomEntity $customEntity, Context $context)
    {
        $this->customEntity = $customEntity;
        $this->context = $context;
    }
    public static function getAvailableData(): EventDataCollection
    {
        return (new EventDataCollection())
            ->add('customEntity', new EntityType(CustomEntityDefinition::class));
    }
    public function getCustomEntity(): CustomEntity
    {
        return $this->customEntity;
    }
    public function getName(): string
    {
        return self::EVENT_NAME;
    }
    public function getMailStruct(): MailRecipientStruct
    {
        $technicalName = $this->customEntity->getTechnicalName();
        return new MailRecipientStruct([
            $technicalName
        ]);
    }
    public function getSalesChannelId(): ?string
    {
        return null;
    }
    public function getContext(): Context
    {
        return $this->context;
    }
}

and my services.xml:和我的服务。xml:

<services>
    <service id="Swag\CustomEntity\Custom\CustomEntityDefinition">
        <tag name="shopware.entity.definition" entity="custom_entity" />
    </service>

    <service id="Swag\CustomEntity\Event\CustomEvent" public="true">
        <argument type="collection">
            <argument key="custom.entity" type="collection">
                <argument key="customEntity" type="collection">
                    <argument key="type">entity</argument>
                    <argument key="entityClass">Swag\CustomEntity\Custom\CustomEntityDefinition</argument>
                </argument>
            </argument>
        </argument>
        <argument type="service" id="assets.context"/>
    </service>
</services>

and I edited available_entites in contact form:我在联系表格中编辑了 available_entites:

{"salesChannel":"sales_channel","customEntity":"custom_entity"}

and now when I try to send the contact form, my rest is 302 FOUND and the mail is not sent.现在当我尝试发送联系表时,我的 rest 是302 FOUND并且邮件没有发送。

What does your template look like?你的模板是什么样的? If the getSalesChannelId function returns null you don't have access to the SalesChannel in the Template.如果getSalesChannelId function 返回null您无权访问模板中的SalesChannel So your available entites would be:因此,您可用的实体将是:

{"customEntity":"custom_entity"}

Since it's for the contact form, you should return the sales channel id.由于它用于联系表单,因此您应该返回销售渠道 ID。


If not already done you need to add an entry to the event_action table, so when the BusinessEvent is dispatched the mail will be send.如果尚未完成,您需要在event_action表中添加一个条目,以便在发送 BusinessEvent 时发送邮件。

Should the mail still not be send, have a look on var/log/dev.log may be there will the error shown.如果邮件仍未发送,请查看var/log/dev.log可能会显示错误。

HoelShare, I saw your repository name ExampleEventEntity and it works. HoelShare,我看到了您的存储库名称 ExampleEventEntity 并且它有效。 but I would like to add my entity to existing mail templates.但我想将我的实体添加到现有的邮件模板中。 Can it be changed at all?它可以改变吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM