尝试订阅现有事件时出现 Symfony 异常

Symfony Exception while trying to subscribe to an existing event

正如标题所说,我想扩展一个电子邮件实体,所以我正在尝试订阅 MAPPING_REGISTER_CUSTOMER 事件。当用户提交表单时,我想扩展外发电子邮件。目前我正在为这个异常而苦苦挣扎

这是例外情况:

Argument 1 passed to RegistrationExtension\Subscriber\RegisterExtensionSubscriber::onCustomRegister() must be an instance of Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent, instance of Shopware\Core\Framework\Event\DataMappingEvent given, called in /app/vendor/symfony/event-dispatcher/Debug/WrappedListener.php on line 126

这是我的代码:)

<?php
namespace RegistrationExtension\Subscriber;

use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Checkout\Customer\CustomerEvents;
use Shopware\Core\Checkout\Customer\CustomerEntity;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Storefront\Controller\StorefrontController;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;

class RegisterExtensionSubscriber implements EventSubscriberInterface{
    public static function getSubscribedEvents(): array{
        return [
            CustomerEvents::MAPPING_REGISTER_CUSTOMER => 'onCustomRegister'
        ];
    }
    public function onCustomRegister(EntityLoadedEvent $event , SalesChannelContext $context){
         /**
         * @var EntityRepositoryInterface $mailTypeRepository
         * @var EntityRepositoryInterface $mailRepository
         */
        $mailTypeRepository = $this->container->get('mail_template_type.repository');
        $mailRepository = $this->container->get('mail_template.repository');
        
        $mailEntity = $mailTypeRepository->search(
            (new Criteria())->addFilter(new EqualsFilter('mail_template_type.technicalName', "customer_register")),
            $context->getContext()
        );

        $mailTypeId = $mailEntity->getEntities()->first()->getId();


        $mailEntity = $mailRepository->search(
            (new Criteria())->addFilter(new EqualsFilter('mail_template.mailTemplateTypeId', $mailTypeId)),
            $context->getContext()
        );
        
        //$mailEntity->getEntities()->setContentHtml("test");
        $mail = $mailEntity->getEntities();

        return $this->renderStorefront(
            '@Storefront/storefront/page/checkout/address/index.html.twig',[
                'page' => $context,
                'mailEntity' => $mail
            ]
        );

        /** @var CustomerEntity $customerRegisEvent */
        foreach ($event->getEntities() as $customerRegisEvent) {
            $customerRegisEvent->addExtension('custom_struct', new RegisterStruct());
        } 
    }
}

感谢任何帮助:)

[编辑]

<?php
namespace RegistrationExtension\Subscriber;

use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Checkout\Customer\CustomerEvents;
use Shopware\Core\Checkout\Customer\CustomerEntity;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Storefront\Controller\StorefrontController;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Checkout\Customer\Event\CustomerRegisterEvent;

class RegisterExtensionSubscriber implements EventSubscriberInterface{
    public static function getSubscribedEvents(): array{
        return [
            \Shopware\Core\Checkout\Customer\Event\CustomerRegisterEvent::class => 'onCustomRegister'
        ];
    }
    public function onCustomRegister(\Shopware\Core\Checkout\Customer\Event\CustomerRegisterEvent $event){
        /** @var CustomerEntity $customerRegisEvent */
        foreach ($event->getMailStruct() as $customerRegisEvent) {
            $customerRegisEvent->addExtension('custom_struct', new RegisterStruct());
        } 

    }
}

您可能需要另一个活动

\Shopware\Core\Checkout\Customer\Event\CustomerRegisterEvent:class => 'onCustomRegister'

并且您还需要更改 onCustomRegister 方法的定义

尝试

public function onCustomRegister(\Shopware\Core\Checkout\Customer\Event\CustomerRegisterEvent $event)

之后,您可以通过以下方式在方法中获取客户 $event->getCustomer() 和上下文 $event->getSalesChannelContext()