diff --git a/src/Canonical.php b/src/Canonical.php index aa0cd4058..1e13295c4 100644 --- a/src/Canonical.php +++ b/src/Canonical.php @@ -183,7 +183,7 @@ public function setPath(?string $route = null, array $params = []): void } } - private function getCanonicalRoute(string $route, array &$params = []): string + public function getCanonicalRoute(string $route, array &$params = []): string { $routes = new Collection($this->router->getRouteCollection()->getIterator()); $currentController = $routes->get($route)->getDefault('_controller'); diff --git a/src/Twig/ContentExtension.php b/src/Twig/ContentExtension.php index 97b005f53..16b9c8d28 100644 --- a/src/Twig/ContentExtension.php +++ b/src/Twig/ContentExtension.php @@ -4,6 +4,7 @@ namespace Bolt\Twig; +use Bolt\Canonical; use Bolt\Configuration\Config; use Bolt\Configuration\Content\ContentType; use Bolt\Entity\Content; @@ -68,6 +69,9 @@ class ContentExtension extends AbstractExtension /** @var TranslatorInterface */ private $translator; + /** @var Canonical */ + private $canonical; + public function __construct( UrlGeneratorInterface $urlGenerator, ContentRepository $contentRepository, @@ -77,7 +81,8 @@ public function __construct( Config $config, Query $query, TaxonomyRepository $taxonomyRepository, - TranslatorInterface $translator + TranslatorInterface $translator, + Canonical $canonical ) { $this->urlGenerator = $urlGenerator; $this->contentRepository = $contentRepository; @@ -88,6 +93,7 @@ public function __construct( $this->query = $query; $this->taxonomyRepository = $taxonomyRepository; $this->translator = $translator; + $this->canonical = $canonical; } /** @@ -390,9 +396,11 @@ public function getStatusLink(Content $content, bool $absolute = false): ?string private function generateLink(string $route, array $params, $canonical = false): string { + $canonicalRoute = $this->canonical->getCanonicalRoute($route, $params); + try { $link = $this->urlGenerator->generate( - $route, + $canonicalRoute, $params, $canonical ? UrlGeneratorInterface::ABSOLUTE_URL : UrlGeneratorInterface::ABSOLUTE_PATH );