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

This commit is contained in:
Björn Fromme
2026-03-16 11:59:11 +01:00
parent 5d69e4a8aa
commit ff39bf365c
8 changed files with 211 additions and 82 deletions
+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;
}
}