4. Add custom page types

The following describes how you can activate additional page types for login forwarding in your own plugin. This might be necessary if a standard Shopware page type you use is not yet supported by the plugin, or if you have already added new pages in your own plugin or through a third-party plugin. For this tutorial you need programming knowledge.

4.1 Create a Subscriber

First you need to create a subscriber anywhere in your plugin. Here is a complete example first and further below the example is explained:

<?php declare(strict_types=1);

namespace MyPlugin\Subscriber;

use Momocode\MomoLoginRedirectSW6\Redirect\Event\RedirectActiveCheckEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class LoginRedirectSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents(): array
    {
        return [
            RedirectActiveCheckEvent::class => 'onRedirectActiveCheck',
        ];
    }

    public function onRedirectActiveCheck(RedirectActiveCheckEvent $event): void
    {
        if ($event->getRoute() === 'frontend.home.page') {
            $event->setIsActive(true);
        }
    }
}

The example describes the page type "Home Page", which already exists in the plugin. However, you can integrate any other page type according to this logic. The subscriber reacts to the "RedirectActiveCheckEvent". This is triggered after logging in and when the plugin checks if a redirect to the previous page is enabled for this page type. In the example, it then checks if the route stored in the event (the page called before login) is the route "frontend.home.page". This is the Symfony route of the home page. If it is this route, the event will store the information that the redirection to this page should be active. You can include any logic here, and you can also override the plugin's logic for individual routes so that the plugin configuration is ignored and your own logic is executed. You should find the exact name of your route in the corresponding controller of your page type.

4.2 Register your Subscriber

Now you only have to register your subscriber in the service container. To do this, add the following entry to your services.xml:

<service id="MyPlugin\Subscriber\LoginRedirectSubscriber">
    <tag name="kernel.event_subscriber"/>
</service>

Note: If you are not familiar with Shopware plugin development and absolutely need a new page type, feel free to contact me at moritz@momocode.de.

Last updated