Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NGSTACK-807 added handler for canonical url #13

Merged
merged 12 commits into from
Dec 7, 2023
39 changes: 39 additions & 0 deletions bundle/Handler/Literal/CanonicalUrl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Netgen\Bundle\OpenGraphBundle\Handler\Literal;

use Ibexa\Core\MVC\Symfony\Routing\UrlAliasRouter;
use Netgen\Bundle\OpenGraphBundle\Handler\HandlerInterface;
use Netgen\Bundle\OpenGraphBundle\MetaTag\Item;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RouterInterface;

final class CanonicalUrl implements HandlerInterface
{
private RouterInterface $router;

public function __construct(RouterInterface $router)
{
$this->router = $router;
}

public function getMetaTags($tagName, array $params = []): array
{
$value = $this->router->generate(
UrlAliasRouter::URL_ALIAS_ROUTE_NAME,
[
'locationId' => $this->content->contentInfo->mainLocationId,
],
UrlGeneratorInterface::ABSOLUTE_URL,
);

return [
new Item(
$tagName,
$value
),
];
}
}
7 changes: 7 additions & 0 deletions bundle/Resources/config/handlers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ services:
tags:
- { name: netgen_open_graph.meta_tag_handler, alias: literal/url }

netgen_open_graph.handler.literal.canonical_url:
class: Netgen\Bundle\OpenGraphBundle\Handler\Literal\CanonicalUrl
arguments:
- "@router"
tags:
- { name: netgen_open_graph.meta_tag_handler, alias: literal/canonical_url }

netgen_open_graph.handler.field_type.text_line:
class: Netgen\Bundle\OpenGraphBundle\Handler\FieldType\TextLine
parent: netgen_open_graph.handler.field_type.abstract
Expand Down
Loading