Skip to content

Commit

Permalink
Implement ID for checkbox fields
Browse files Browse the repository at this point in the history
  • Loading branch information
robertvanlienden committed Dec 30, 2024
1 parent 3f21e94 commit 266d6c2
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 24 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ Put this in your `config/packages/user_agreements.yaml`
```yaml
user_agreements:
agreements:
- label: 'I agree with the terms and conditions' ## Label for form. Without any special characters because of technical limitations.
- id: "checkbox_field_id" ## ID for checkbox field
label: 'I agree with the terms and conditions'
route_name: 'terms_conditions_page' ## Symfony route name to the agreement
required: true ## Required agreement or optional
version: '1.0' ##Optional
Expand Down
2 changes: 1 addition & 1 deletion src/Form/AgreementFormType.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
foreach ($agreements as $agreement) {
$url = $this->urlGenerator->generate($agreement['route_name']);
$builder->add(
'agree_' . strtolower(str_replace(' ', '_', $agreement['label'])),
$agreement['id'],
CheckboxType::class,
[
'label' => sprintf('<a href="%s">%s</a>', $url, $agreement['label']),
Expand Down
16 changes: 0 additions & 16 deletions src/Service/AgreementHandlingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,13 @@ public function handleAgreementsFromFormType(array $agreements, UserInterface $u
);
}

$agreements = $this->normalizeAgreementsLabels($agreements);
$this->createUserAgreements($agreements, $user);

if ($flushEntities) {
$this->entityManager->flush();
}
}

private function normalizeAgreementsLabels(array $agreements): array
{
$normalizedAgreements = [];

foreach ($agreements as $key => $value) {
// Remove the 'agree_' prefix from the key
$normalizedKey = substr($key, 6);
// Replace underscores with spaces in the key
$normalizedKey = str_replace('_', ' ', $normalizedKey);
$normalizedAgreements[$normalizedKey] = $value;
}

return $normalizedAgreements;
}

private function createUserAgreements(array $agreements, UserInterface $user): void
{
foreach ($agreements as $key => $value) {
Expand Down
4 changes: 2 additions & 2 deletions src/Service/AgreementService.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public function getAgreements(): array
return $this->agreements;
}

public function findAgreement(string $label): ?array
public function findAgreement(string $id): ?array
{
$arrayKey = array_search(strtolower($label), array_map('strtolower', array_column($this->agreements, 'label')));
$arrayKey = array_search(strtolower($id), array_map('strtolower', array_column($this->agreements, 'id')));

return $arrayKey !== false ? $this->agreements[$arrayKey] : null;
}
Expand Down
4 changes: 4 additions & 0 deletions src/UserAgreementsBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ public function configure(DefinitionConfigurator $definition): void
->arrayNode('agreements')
->arrayPrototype()
->children()
->scalarNode('id')
->isRequired()
->cannotBeEmpty()
->end()
->scalarNode('label')
->isRequired()
->cannotBeEmpty()
Expand Down
17 changes: 13 additions & 4 deletions tests/unit/AgreementServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,16 @@ protected function setUp(): void

$agreementsData = [
'agreements' => [
['label' => 'Privacy Policy', 'content' => 'Privacy content'],
['label' => 'Terms of Service', 'content' => 'Terms content'],
[
'id' => 'privacy_policy',
'label' => 'Privacy Policy',
'content' => 'Privacy content',
],
[
'id' => 'terms_of_service',
'label' => 'Terms of Service',
'content' => 'Terms content'
],
],
];

Expand All @@ -33,13 +41,14 @@ public function testGetAgreements(): void
$agreements = $this->agreementService->getAgreements();

$this->assertCount(2, $agreements);
$this->assertArrayHasKey('id', $agreements[0]);
$this->assertArrayHasKey('label', $agreements[0]);
$this->assertArrayHasKey('content', $agreements[0]);
}

public function testFindAgreement(): void
{
$agreement = $this->agreementService->findAgreement('Privacy Policy');
$agreement = $this->agreementService->findAgreement('privacy_policy');

$this->assertNotNull($agreement);
$this->assertEquals('Privacy Policy', $agreement['label']);
Expand All @@ -48,7 +57,7 @@ public function testFindAgreement(): void

public function testFindAgreementNotFound(): void
{
$agreement = $this->agreementService->findAgreement('not existing');
$agreement = $this->agreementService->findAgreement('not_existing');

$this->assertNull($agreement);
}
Expand Down

0 comments on commit 266d6c2

Please sign in to comment.