Skip to content

Commit

Permalink
NGSTACK-807 added handler for canonical url
Browse files Browse the repository at this point in the history
  • Loading branch information
Miljenko Muha committed Dec 7, 2023
1 parent e2d774c commit 1f9dc9f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
39 changes: 39 additions & 0 deletions bundle/Handler/FieldType/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\FieldType;

use Ibexa\Contracts\Core\Repository\Values\Content\Field;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Netgen\Bundle\OpenGraphBundle\MetaTag\Item;
use Symfony\Component\Routing\RouterInterface;
class CanonicalUrl extends Handler

{
private RouterInterface $router;
public function __construct(RouterInterface $router)
{
$this->router = $router;
}
public function getMetaTags($tagName, array $params = []): array
{
$value = $this->router->generate(
'ibexa.url.alias',
[
'locationId' => $this->content->contentInfo->mainLocationId,
],
UrlGeneratorInterface::ABSOLUTE_URL,
);
return array(
new Item(
$tagName,
$value
),
);
}
protected function supports(Field $field): bool
{
return true;
}
}
11 changes: 11 additions & 0 deletions bundle/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,14 @@ services:

netgen_open_graph.meta_tag_renderer:
class: Netgen\Bundle\OpenGraphBundle\MetaTag\Renderer

app.opengraph.handler.url:
class: Netgen\Bundle\OpenGraphBundle\Handler\FieldType\CanonicalUrl
public: false
lazy: true
arguments:
- "@router"
- '@Ibexa\Core\Helper\TranslationHelper'
- "@ibexa.api.service.content"
tags:
- { name: netgen_open_graph.meta_tag_handler, alias: app/canonical }

0 comments on commit 1f9dc9f

Please sign in to comment.