mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-27 04:08:57 +02:00
Added placeholders filter to utilize the placeholders in twig mode
Fixes #546
This commit is contained in:
parent
757201cafa
commit
dff1ef04bf
5 changed files with 83 additions and 3 deletions
|
@ -53,7 +53,14 @@ use Twig\Error\Error;
|
|||
|
||||
final class LabelHTMLGenerator
|
||||
{
|
||||
public function __construct(private readonly ElementTypeNameGenerator $elementTypeNameGenerator, private readonly LabelTextReplacer $replacer, private readonly Environment $twig, private readonly LabelBarcodeGenerator $barcodeGenerator, private readonly SandboxedTwigFactory $sandboxedTwigProvider, private readonly Security $security, private readonly string $partdb_title)
|
||||
public function __construct(
|
||||
private readonly ElementTypeNameGenerator $elementTypeNameGenerator,
|
||||
private readonly LabelTextReplacer $replacer,
|
||||
private readonly Environment $twig,
|
||||
private readonly LabelBarcodeGenerator $barcodeGenerator,
|
||||
private readonly SandboxedTwigFactory $sandboxedTwigProvider,
|
||||
private readonly Security $security,
|
||||
private readonly string $partdb_title)
|
||||
{
|
||||
}
|
||||
|
||||
|
|
|
@ -64,6 +64,17 @@ final class LabelTextReplacer
|
|||
* @return string If the placeholder was valid, the replaced info. Otherwise the passed string.
|
||||
*/
|
||||
public function handlePlaceholder(string $placeholder, object $target): string
|
||||
{
|
||||
return $this->handlePlaceholderOrReturnNull($placeholder, $target) ?? $placeholder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Similar to handlePlaceholder, but returns null if the placeholder is not known (instead of the original string)
|
||||
* @param string $placeholder
|
||||
* @param object $target
|
||||
* @return string|null
|
||||
*/
|
||||
public function handlePlaceholderOrReturnNull(string $placeholder, object $target): ?string
|
||||
{
|
||||
foreach ($this->providers as $provider) {
|
||||
/** @var PlaceholderProviderInterface $provider */
|
||||
|
@ -73,7 +84,7 @@ final class LabelTextReplacer
|
|||
}
|
||||
}
|
||||
|
||||
return $placeholder;
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -64,6 +64,7 @@ use App\Twig\BarcodeExtension;
|
|||
use App\Twig\EntityExtension;
|
||||
use App\Twig\FormatExtension;
|
||||
use App\Twig\Sandbox\InheritanceSecurityPolicy;
|
||||
use App\Twig\Sandbox\SandboxedLabelExtension;
|
||||
use App\Twig\TwigCoreExtension;
|
||||
use InvalidArgumentException;
|
||||
use Twig\Environment;
|
||||
|
@ -92,6 +93,9 @@ final class SandboxedTwigFactory
|
|||
|
||||
//FormatExtension:
|
||||
'format_money', 'format_si', 'format_amount', 'format_bytes',
|
||||
|
||||
//SandboxedLabelExtension
|
||||
'placeholders',
|
||||
];
|
||||
|
||||
private const ALLOWED_FUNCTIONS = ['country_names', 'country_timezones', 'currency_names', 'cycle',
|
||||
|
@ -103,7 +107,8 @@ final class SandboxedTwigFactory
|
|||
'entity_type', 'entity_url',
|
||||
//BarcodeExtension:
|
||||
'barcode_svg',
|
||||
|
||||
//SandboxedLabelExtension
|
||||
'placeholder',
|
||||
];
|
||||
|
||||
private const ALLOWED_METHODS = [
|
||||
|
@ -143,6 +148,7 @@ final class SandboxedTwigFactory
|
|||
private readonly BarcodeExtension $barcodeExtension,
|
||||
private readonly EntityExtension $entityExtension,
|
||||
private readonly TwigCoreExtension $twigCoreExtension,
|
||||
private readonly SandboxedLabelExtension $sandboxedLabelExtension,
|
||||
)
|
||||
{
|
||||
}
|
||||
|
@ -172,6 +178,7 @@ final class SandboxedTwigFactory
|
|||
$twig->addExtension($this->barcodeExtension);
|
||||
$twig->addExtension($this->entityExtension);
|
||||
$twig->addExtension($this->twigCoreExtension);
|
||||
$twig->addExtension($this->sandboxedLabelExtension);
|
||||
|
||||
return $twig;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue