EldoRay
EldoRay

Reputation: 84

Is there a way to get the URI on which page a flow action has been triggered?

Currently I am using a custom action which is being triggered on Newsletter/Sign-up.

Since we have multiple Occasions on registering for the Newsletter like account Registration, Checkout Registering and after login within customer edit I would like to know which way the Recipient chose for registering up.

FYI: We still use 6.4.x since we can not update currently and used this documentation for creating the action: https://developer.shopware.com/docs/v/6.4/guides/plugins/plugins/framework/flow/add-flow-builder-action

Neither inside the $event of FlowEvent or its context I can find any Information about any kind of referrer. Is there any Shopware native way in order to receive the referrer like '/account/login' or '/checkout/register'?

I already have a solution using the $_SERVER['HTTP-REFERER'] but since this way is not exactly secure and has GDPR concerns I would like to find a different approach.

Thanks in advance!

Upvotes: 2

Views: 79

Answers (1)

dneustadt
dneustadt

Reputation: 13161

You could listen to SalesChannelContextResolvedEvent with a subscriber and add the path info from the current request stack as an extension to the instance of Context. In your implementation of FlowAction you can then get the instance of Context and subsequently the added extension with the path.

<service id="SwagBasicExample\Subscriber\SalesChannelContextResolvedSubscriber">
    <argument type="service" id="request_stack"/>
    <tag name="kernel.event_subscriber"/>
</service>
use Shopware\Core\Content\Cms\SalesChannel\Struct\TextStruct;
use Shopware\Core\Framework\Routing\Event\SalesChannelContextResolvedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;

class SalesChannelContextResolvedSubscriber implements EventSubscriberInterface
{
    private RequestStack $requestStack;
    
    public function __construct(RequestStack $requestStack) 
    {
        $this->requestStack = $requestStack;
    }

    public static function getSubscribedEvents(): array
    {
        return [
            SalesChannelContextResolvedEvent::class => 'onSalesChannelContextResolved',
        ];
    }

    public function onSalesChannelContextCreated(SalesChannelContextResolvedEvent $event): void
    {
        $context = $event->getSalesChannelContext()->getContext();
        $request = $this->requestStack->getMainRequest();
        
        if (!$request) {
            return;
        }
        
        $context->addExtension(
            'currentRequestPath',
            new TextStruct($request->getPathInfo())
        );
    }
}

And then in your FlowAction implementation:

public function handle(FlowEvent $event): void
{
    $currentRequestPathExtension = $event->getContext()->getExtension('currentRequestPath');
    $pathInfo = null;

    if ($currentRequestPathExtension instanceof TextStruct) {
        $pathInfo = $currentRequestPathExtension->getContent();
    }

    // ...
}

Upvotes: 1

Related Questions