|
7 | 7 | use Ibexa\Core\MVC\Symfony\Routing\UrlAliasRouter; |
8 | 8 | use Netgen\Bundle\OpenGraphBundle\Handler\HandlerInterface; |
9 | 9 | use Netgen\Bundle\OpenGraphBundle\MetaTag\Item; |
| 10 | +use Symfony\Component\HttpFoundation\Request; |
| 11 | +use Symfony\Component\HttpFoundation\RequestStack; |
10 | 12 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
11 | | -use Symfony\Component\Routing\RouterInterface; |
| 13 | +use Netgen\IbexaSiteApi\API\Values\Content as SiteApiContent; |
| 14 | +use Ibexa\Contracts\Core\Repository\Values\Content\Content as IbexaContent; |
12 | 15 |
|
13 | 16 | final class CanonicalUrl implements HandlerInterface |
14 | 17 | { |
15 | | - private RouterInterface $router; |
| 18 | + private RequestStack $requestStack; |
16 | 19 |
|
17 | | - public function __construct(RouterInterface $router) |
| 20 | + private UrlGeneratorInterface $urlGenerator; |
| 21 | + |
| 22 | + public function __construct(RequestStack $requestStack, UrlGeneratorInterface $urlGenerator) |
18 | 23 | { |
19 | | - $this->router = $router; |
| 24 | + $this->requestStack = $requestStack; |
| 25 | + $this->urlGenerator = $urlGenerator; |
20 | 26 | } |
21 | 27 |
|
22 | 28 | public function getMetaTags($tagName, array $params = []): array |
23 | 29 | { |
24 | | - $value = $this->router->generate( |
| 30 | + $currentRequest = $this->requestStack->getCurrentRequest(); |
| 31 | + if (!$currentRequest instanceof Request) { |
| 32 | + return []; |
| 33 | + } |
| 34 | + |
| 35 | + $content = $currentRequest->attributes->get('content'); |
| 36 | + if (!$content instanceof IbexaContent && !$content instanceof SiteApiContent) { |
| 37 | + return []; |
| 38 | + } |
| 39 | + |
| 40 | + $value = $this->urlGenerator->generate( |
25 | 41 | UrlAliasRouter::URL_ALIAS_ROUTE_NAME, |
26 | 42 | [ |
27 | | - 'locationId' => $this->content->contentInfo->mainLocationId, |
| 43 | + 'locationId' => $content->contentInfo->mainLocationId, |
28 | 44 | ], |
29 | 45 | UrlGeneratorInterface::ABSOLUTE_URL, |
30 | 46 | ); |
|
0 commit comments