Search code examples
pluginsshopware6

Shopware 6.5.0 : Get the current page ID using a event subscriber


I would like to know how to retrieve the current loaded page ID in the event listener like StorefrontRenderEvent or PageLoadedEvent. Alternatively, is there another method to fetch the ID of the loaded page in the twig template? I am using Shopware 6.5.0.


Solution

  • Programmatically you can listen to the CmsPageLoadedEvent.

    class MySubscriber implements EventSubscriberInterface
    {
        public static function getSubscribedEvents(): array
        {
            return [
                CmsPageLoadedEvent::class => 'onCmsPageLoaded',
            ];
        }
    
        public function onProductsLoaded(CmsPageLoadedEvent $event)
        {
            var_dump($event->getResult()->first()->getId());
        }
    }
    

    In the twig template you may get the id of cms_page like this:

    {% set cmsPage = page.landingPage ? page.landingPage.cmsPage : page.cmsPage %}
    {{ cmsPage.id }}