feat: improved handling of fields to be rendered as static text

This commit is contained in:
Björn Fromme
2025-10-27 13:38:40 +01:00
parent eee7be0c1e
commit 51dbf20118
8 changed files with 211 additions and 82 deletions
+3
View File
@@ -27,6 +27,9 @@ class AppExtension extends AbstractExtension
new TwigFunction('icon', [AppRuntime::class, 'renderIcon'], ['needs_environment' => true, 'is_safe' => ['html']]),
new TwigFunction('is_participant_eligible', [AppRuntime::class, 'isParticipantEligible']),
new TwigFunction('qa_attribute', [AppRuntime::class, 'renderQaAttribute'], ['is_safe' => ['html']]),
new TwigFunction('is_static_text', [AppRuntime::class, 'isStaticText']),
new TwigFunction('is_hidden', [AppRuntime::class, 'isHidden']),
new TwigFunction('gravatar_url', [AppRuntime::class, 'getGravatarUrl']),
];
}
}
+74
View File
@@ -4,7 +4,10 @@ namespace App\Twig;
use App\BusProNet\DataProvider\CountryDataProvider;
use App\Form\Model\BookingDto;
use App\Form\Service\CreateFieldStateProvider;
use App\Form\Service\EditFieldStateProvider;
use App\Service\ParticipantEligibilityService;
use Symfony\Component\Form\FormView;
use Twig\Environment;
use Twig\Extension\RuntimeExtensionInterface;
use Twig\Extra\Intl\IntlExtension;
@@ -15,6 +18,8 @@ class AppRuntime implements RuntimeExtensionInterface
private readonly IntlExtension $intlExtension,
private readonly CountryDataProvider $countryDataProvider,
private readonly ParticipantEligibilityService $participantEligibilityService,
private readonly CreateFieldStateProvider $createFieldStateProvider,
private readonly EditFieldStateProvider $editFieldStateProvider,
private readonly string $environment,
) {
}
@@ -123,4 +128,73 @@ class AppRuntime implements RuntimeExtensionInterface
return sprintf(' data-qa-%s="%s"', strtolower($label), $value);
}
/**
* Checks if a form field should be rendered as static text.
*
* Accepts either a FormView (when field is in form structure) or a string field name
* (when field is excluded from form but needs to be checked).
*
* @param FormView|string $field The FormView or field name
* @param BookingDto $bookingDto The booking context (create or edit mode)
* @param int $participantIndex The participant index
*
* @return bool True if field should render as static text
*/
public function isStaticText(FormView|string $field, BookingDto $bookingDto, int $participantIndex): bool
{
$fieldName = $field instanceof FormView ? $field->vars['name'] : $field;
$provider = $this->getProvider($bookingDto);
return $provider->shouldRenderAsStaticText($fieldName, $bookingDto, $participantIndex);
}
/**
* Checks if a form field should be completely hidden (not rendered at all).
*
* Accepts either a FormView (when field is in form structure) or a string field name
* (when field is excluded from form but needs to be checked).
*
* @param FormView|string $field The FormView or field name
* @param BookingDto $bookingDto The booking context (create or edit mode)
* @param int $participantIndex The participant index
*
* @return bool True if field should be hidden (not rendered)
*/
public function isHidden(FormView|string $field, BookingDto $bookingDto, int $participantIndex): bool
{
$fieldName = $field instanceof FormView ? $field->vars['name'] : $field;
$provider = $this->getProvider($bookingDto);
return !$provider->shouldIncludeField($fieldName, $bookingDto, $participantIndex);
}
/**
* Generates a Gravatar URL for the given email address.
*
* @param string $email The email address
* @param int $size The size of the avatar in pixels (default: 80)
*
* @return string The Gravatar URL
*/
public function getGravatarUrl(string $email, int $size = 80): string
{
$hash = md5(strtolower(trim($email)));
return sprintf('https://www.gravatar.com/avatar/%s?s=%d&d=404', $hash, $size);
}
/**
* Gets the appropriate field state provider based on booking mode.
*
* @param BookingDto $bookingDto The booking context
*
* @return CreateFieldStateProvider|EditFieldStateProvider The appropriate provider
*/
private function getProvider(BookingDto $bookingDto): CreateFieldStateProvider|EditFieldStateProvider
{
return BookingDto::MODE_CREATE === $bookingDto->getMode()
? $this->createFieldStateProvider
: $this->editFieldStateProvider;
}
}
-28
View File
@@ -1,28 +0,0 @@
<?php
declare(strict_types=1);
namespace App\Twig;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
final class GravatarExtension extends AbstractExtension
{
public function getFunctions(): array
{
return [
new TwigFunction('gravatar_url', [$this, 'getGravatarUrl']),
];
}
/**
* Generates a Gravatar URL for the given email address.
*/
public function getGravatarUrl(string $email, int $size = 80): string
{
$hash = md5(strtolower(trim($email)));
return sprintf('https://www.gravatar.com/avatar/%s?s=%d&d=404', $hash, $size);
}
}