prestaconcept / PrestaSitemapBundle

A symfony bundle that provides tools to build a rich application sitemap. The main goals are : simple, no databases, various namespace (eg. google image), respect constraints etc.
MIT License
347 stars 100 forks source link

Decorating in AddUrlEvent is impossible #321

Closed pculka closed 5 months ago

pculka commented 8 months ago

PHP version(s) affected: 8.2

Package version(s) affected: 3.3.1

Description
I'm trying to use SitemapAddUrlEvent to decorate my Urls (as per docblock I should subcribe if I want to decorate) Though the $event->getUrl() is empty, meaning it is impossible to decorate the URL... Also

How to reproduce
create a simple event listener:

readonly class SitemapSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents(): array
    {
        return [
            SitemapAddUrlEvent::class => 'addUrl',
            SitemapPopulateEvent::class => 'populate',
        ];
    }
   public function addUrl(SitemapAddUrlEvent $event)
    {
        dd($event->getUrl());
    }
}

Also, there is no UrlContainerInterface like in SitemapPopulateEvent, meaning there is no way to add the decorated URL to the "pool" for dumping...

Possible Solution

Additional Context

yann-eugone commented 8 months ago

Hi @pculka

Maybe the doc is not clear enough, or maybe the behaviour is not what you are expecting, but the URL property is expected NULL when the event is dispatched. You will have access to the route name and the routing sitemap options. And if you set that URL, it will be what you set that will be added.

Originally, this was created to handle translated routes, and this is why we are not creating the UrlConcrete

You can have a look to https://github.com/prestaconcept/PrestaSitemapBundle/blob/v3.3.1/src/EventListener/RouteAnnotationEventListener.php for an example of how you can use this event.

Hope that helps

yann-eugone commented 5 months ago

Not a bug, and no answer received, closing