From 266d6c2436fa1a3be51338968e24287b9f147f80 Mon Sep 17 00:00:00 2001 From: Robert van Lienden Date: Mon, 30 Dec 2024 22:01:26 +0100 Subject: [PATCH] Implement ID for checkbox fields --- README.md | 3 ++- src/Form/AgreementFormType.php | 2 +- src/Service/AgreementHandlingService.php | 16 ---------------- src/Service/AgreementService.php | 4 ++-- src/UserAgreementsBundle.php | 4 ++++ tests/unit/AgreementServiceTest.php | 17 +++++++++++++---- 6 files changed, 22 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index e41847b..47e562a 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/Form/AgreementFormType.php b/src/Form/AgreementFormType.php index 88fdc4a..7e1e108 100644 --- a/src/Form/AgreementFormType.php +++ b/src/Form/AgreementFormType.php @@ -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('%s', $url, $agreement['label']), diff --git a/src/Service/AgreementHandlingService.php b/src/Service/AgreementHandlingService.php index 91d8c08..d70ed77 100644 --- a/src/Service/AgreementHandlingService.php +++ b/src/Service/AgreementHandlingService.php @@ -29,7 +29,6 @@ public function handleAgreementsFromFormType(array $agreements, UserInterface $u ); } - $agreements = $this->normalizeAgreementsLabels($agreements); $this->createUserAgreements($agreements, $user); if ($flushEntities) { @@ -37,21 +36,6 @@ public function handleAgreementsFromFormType(array $agreements, UserInterface $u } } - 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) { diff --git a/src/Service/AgreementService.php b/src/Service/AgreementService.php index 7ed2976..2dca80e 100644 --- a/src/Service/AgreementService.php +++ b/src/Service/AgreementService.php @@ -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; } diff --git a/src/UserAgreementsBundle.php b/src/UserAgreementsBundle.php index e2f6c27..5a41cec 100644 --- a/src/UserAgreementsBundle.php +++ b/src/UserAgreementsBundle.php @@ -27,6 +27,10 @@ public function configure(DefinitionConfigurator $definition): void ->arrayNode('agreements') ->arrayPrototype() ->children() + ->scalarNode('id') + ->isRequired() + ->cannotBeEmpty() + ->end() ->scalarNode('label') ->isRequired() ->cannotBeEmpty() diff --git a/tests/unit/AgreementServiceTest.php b/tests/unit/AgreementServiceTest.php index 44f8f1a..a909066 100644 --- a/tests/unit/AgreementServiceTest.php +++ b/tests/unit/AgreementServiceTest.php @@ -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' + ], ], ]; @@ -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']); @@ -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); }