Skip to content

Commit 76734af

Browse files
committed
Use current request to fetch content for canonical url handler
1 parent 948714c commit 76734af

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

bundle/Handler/Literal/CanonicalUrl.php

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,40 @@
77
use Ibexa\Core\MVC\Symfony\Routing\UrlAliasRouter;
88
use Netgen\Bundle\OpenGraphBundle\Handler\HandlerInterface;
99
use Netgen\Bundle\OpenGraphBundle\MetaTag\Item;
10+
use Symfony\Component\HttpFoundation\Request;
11+
use Symfony\Component\HttpFoundation\RequestStack;
1012
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;
1215

1316
final class CanonicalUrl implements HandlerInterface
1417
{
15-
private RouterInterface $router;
18+
private RequestStack $requestStack;
1619

17-
public function __construct(RouterInterface $router)
20+
private UrlGeneratorInterface $urlGenerator;
21+
22+
public function __construct(RequestStack $requestStack, UrlGeneratorInterface $urlGenerator)
1823
{
19-
$this->router = $router;
24+
$this->requestStack = $requestStack;
25+
$this->urlGenerator = $urlGenerator;
2026
}
2127

2228
public function getMetaTags($tagName, array $params = []): array
2329
{
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(
2541
UrlAliasRouter::URL_ALIAS_ROUTE_NAME,
2642
[
27-
'locationId' => $this->content->contentInfo->mainLocationId,
43+
'locationId' => $content->contentInfo->mainLocationId,
2844
],
2945
UrlGeneratorInterface::ABSOLUTE_URL,
3046
);

bundle/Resources/config/handlers.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ services:
2525
netgen_open_graph.handler.literal.canonical_url:
2626
class: Netgen\Bundle\OpenGraphBundle\Handler\Literal\CanonicalUrl
2727
arguments:
28+
- "@request_stack"
2829
- "@router"
2930
tags:
3031
- { name: netgen_open_graph.meta_tag_handler, alias: literal/canonical_url }

0 commit comments

Comments
 (0)