Skip to content

Commit

Permalink
Use current request to fetch content for canonical url handler
Browse files Browse the repository at this point in the history
  • Loading branch information
emodric committed Dec 8, 2023
1 parent 948714c commit 76734af
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
28 changes: 22 additions & 6 deletions bundle/Handler/Literal/CanonicalUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,40 @@
use Ibexa\Core\MVC\Symfony\Routing\UrlAliasRouter;
use Netgen\Bundle\OpenGraphBundle\Handler\HandlerInterface;
use Netgen\Bundle\OpenGraphBundle\MetaTag\Item;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RouterInterface;
use Netgen\IbexaSiteApi\API\Values\Content as SiteApiContent;
use Ibexa\Contracts\Core\Repository\Values\Content\Content as IbexaContent;

final class CanonicalUrl implements HandlerInterface
{
private RouterInterface $router;
private RequestStack $requestStack;

public function __construct(RouterInterface $router)
private UrlGeneratorInterface $urlGenerator;

public function __construct(RequestStack $requestStack, UrlGeneratorInterface $urlGenerator)
{
$this->router = $router;
$this->requestStack = $requestStack;
$this->urlGenerator = $urlGenerator;
}

public function getMetaTags($tagName, array $params = []): array
{
$value = $this->router->generate(
$currentRequest = $this->requestStack->getCurrentRequest();
if (!$currentRequest instanceof Request) {
return [];
}

$content = $currentRequest->attributes->get('content');
if (!$content instanceof IbexaContent && !$content instanceof SiteApiContent) {
return [];
}

$value = $this->urlGenerator->generate(
UrlAliasRouter::URL_ALIAS_ROUTE_NAME,
[
'locationId' => $this->content->contentInfo->mainLocationId,
'locationId' => $content->contentInfo->mainLocationId,
],
UrlGeneratorInterface::ABSOLUTE_URL,
);
Expand Down
1 change: 1 addition & 0 deletions bundle/Resources/config/handlers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ services:
netgen_open_graph.handler.literal.canonical_url:
class: Netgen\Bundle\OpenGraphBundle\Handler\Literal\CanonicalUrl
arguments:
- "@request_stack"
- "@router"
tags:
- { name: netgen_open_graph.meta_tag_handler, alias: literal/canonical_url }
Expand Down

0 comments on commit 76734af

Please sign in to comment.