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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a Twig function to generate content URLs #6738

Merged
merged 10 commits into from
Jan 26, 2024
5 changes: 5 additions & 0 deletions core-bundle/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1131,6 +1131,11 @@ services:
- '@contao.framework'
- '@contao.resource_finder'

contao.twig.content_url_runtime:
class: Contao\CoreBundle\Twig\Runtime\ContentUrlRuntime
arguments:
- '@contao.routing.content_url_generator'

contao.twig.csp_runtime:
class: Contao\CoreBundle\Twig\Runtime\CspRuntime
arguments:
Expand Down
5 changes: 5 additions & 0 deletions core-bundle/src/Twig/Extension/ContaoExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Contao\CoreBundle\Twig\Interop\PhpTemplateProxyNodeVisitor;
use Contao\CoreBundle\Twig\ResponseContext\AddTokenParser;
use Contao\CoreBundle\Twig\ResponseContext\DocumentLocation;
use Contao\CoreBundle\Twig\Runtime\ContentUrlRuntime;
use Contao\CoreBundle\Twig\Runtime\CspRuntime;
use Contao\CoreBundle\Twig\Runtime\FigureRuntime;
use Contao\CoreBundle\Twig\Runtime\FormatterRuntime;
Expand Down Expand Up @@ -219,6 +220,10 @@ function (Environment $env, $context, $template, $variables = [], $withContext =
'csp_hash',
[CspRuntime::class, 'addHash'],
),
new TwigFunction(
'content_url',
[ContentUrlRuntime::class, 'generate'],
),
];
}

Expand Down
37 changes: 37 additions & 0 deletions core-bundle/src/Twig/Runtime/ContentUrlRuntime.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

/*
* This file is part of Contao.
*
* (c) Leo Feyer
*
* @license LGPL-3.0-or-later
*/

namespace Contao\CoreBundle\Twig\Runtime;

use Contao\CoreBundle\Routing\ContentUrlGenerator;
use Symfony\Component\Routing\Exception\ExceptionInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Twig\Extension\RuntimeExtensionInterface;

final class ContentUrlRuntime implements RuntimeExtensionInterface
{
/**
* @internal
*/
public function __construct(private readonly ContentUrlGenerator $urlGenerator)
{
}

public function generate(object $content, array $parameters = [], bool $relative = false): string|null
{
try {
return $this->urlGenerator->generate($content, $parameters, $relative ? UrlGeneratorInterface::ABSOLUTE_PATH : UrlGeneratorInterface::ABSOLUTE_URL);
} catch (ExceptionInterface) {
return null;
}
}
}
1 change: 1 addition & 0 deletions core-bundle/tests/Twig/Extension/ContaoExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ public function testAddsTheFunctions(): void
'csp_nonce' => [],
'csp_source' => [],
'csp_hash' => [],
'content_url' => [],
];

$functions = $this->getContaoExtension()->getFunctions();
Expand Down