Add custom event

Overview

In this guide you'll learn how to create your own event. You can read more about events in the Symfony documentation.

Prerequisites

In order to create your own event for your plugin, you first need a plugin as base. Therefore, you can refer to the Plugin Base Guide.
Here's a live coding example on custom events from our free online training "Backend Development".

Event interfaces and classes

In Shopware, you have multiple interfaces and classes for different types of events, in the following you can find a list of them:
  • ShopwareEvent: This interface is just a basic event providing a Context, we need for almost all events.
  • ShopwareSalesChannelEvent: This interface extends from ShopwareEvent and additionally provides a SalesChannelContext.
  • SalesChannelAware: This interface provides the SalesChannelId.
  • GenericEvent: This interface will be used if you want to give your event a specific name like the database events (e.g. product.written.). Otherwise, you have to reference to the event class.
  • NestedEvent: This class will be used for events using other events for example the EntityDeletedEvent extends from the EntityWrittenEvent.
  • BusinessEventInterface: This interface extends from ShopwareEvent and will be used for dynamically assignment and is always named.

Create the event class

First, we create a new class for our event, which we name ExampleEvent. In this example we implement the Shopware\Core\Framework\Event\ShopwareSalesChannelEvent. As mentioned above our class already implements a method for the SalesChannelContext and the Context. Now we pass an ExampleEntity and the SalesChannelContext through the constructor and create a function which returns our ExampleEntity.
Therefore, this is how your event class could look like:
<plugin root>/src/Core/Content/Example/Event/ExampleEvent.php
1
<?php declare(strict_types=1);
2
3
namespace Swag\BasicExample\Core\Content\Example\Event;
4
5
use Shopware\Core\Framework\Context;
6
use Shopware\Core\Framework\Event\ShopwareSalesChannelEvent;
7
use Shopware\Core\System\SalesChannel\SalesChannelContext;
8
use Swag\BasicExample\Core\Content\Example\ExampleEntity;
9
10
class ExampleEvent implements ShopwareSalesChannelEvent
11
{
12
protected ExampleEntity $exampleEntity;
13
14
protected SalesChannelContext $salesChannelContext;
15
16
public function __construct(ExampleEntity $exampleEntity, SalesChannelContext $context)
17
{
18
$this->exampleEntity = $exampleEntity;
19
$this->salesChannelContext = $context;
20
}
21
22
public function getExample(): ExampleEntity
23
{
24
return $this->exampleEntity;
25
}
26
27
public function getContext(): Context
28
{
29
return $this->salesChannelContext->getContext();
30
}
31
32
public function getSalesChannelContext(): SalesChannelContext
33
{
34
return $this->salesChannelContext;
35
}
36
}
Copied!

Fire the event

After we've created our entity class, we need to fire our new event. For this we need the service event_dispatcher which provides a method called dispatch. In this example we created a service ExampleEventService which fires our event. Below you can find the example implementation.
<plugin root>/src/Service/ExampleEventService.php
1
<?php declare(strict_types=1);
2
3
namespace Swag\BasicExample\Service;
4
5
use Shopware\Core\System\SalesChannel\SalesChannelContext;
6
use Swag\BasicExample\Core\Content\Example\Event\ExampleEvent;
7
use Swag\BasicExample\Core\Content\Example\ExampleEntity;
8
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
9
10
class ExampleEventService
11
{
12
private EventDispatcherInterface $eventDispatcher;
13
14
public function __construct(EventDispatcherInterface $eventDispatcher)
15
{
16
$this->eventDispatcher = $eventDispatcher;
17
}
18
19
public function fireEvent(ExampleEntity $exampleEntity, SalesChannelContext $context)
20
{
21
$this->eventDispatcher->dispatch(new ExampleEvent($exampleEntity, $context));
22
}
23
}
Copied!

Next steps

Now that you know how to create your own event, you may want to act on it. To get a grip on this, head over to our Listening to events guide.
Last modified 18d ago