Skip to content

Commit

Permalink
Extend with recaptcha v3
Browse files Browse the repository at this point in the history
  • Loading branch information
Quehnie committed Nov 28, 2023
1 parent b01747b commit dfb6aec
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
1 change: 1 addition & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ public function getConfigTreeBuilder(): TreeBuilder
->arrayNode('dynamic_disabled_types')
->prototype('scalar')->end()->defaultValue([])
->end()
->scalarNode('recaptcha_version')->defaultValue(2)->end()
;

return $treeBuilder;
Expand Down
7 changes: 6 additions & 1 deletion DependencyInjection/SuluFormExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ public function load(array $configs, ContainerBuilder $container): void
$container->setParameter('sulu_form.media_collection_strategy', $mediaCollectionStrategy);
$container->setParameter('sulu_form.static_forms', $config['static_forms']);
$container->setParameter('sulu_form.dynamic_disabled_types', $config['dynamic_disabled_types']);
$container->setParameter('sulu_form.recaptcha.version', $config['recaptcha_version']);

// Default Media Collection Strategy
$container->setAlias(
Expand Down Expand Up @@ -223,7 +224,11 @@ public function load(array $configs, ContainerBuilder $container): void
}

if (\class_exists(\EWZ\Bundle\RecaptchaBundle\Form\Type\EWZRecaptchaType::class)) {
$loader->load('type_recaptcha.xml');
if (3 === $container->getPArameter('sulu_form.recaptcha.version')) {
$loader->load('type_recaptcha_v3.xml');
} else {
$loader->load('type_recaptcha.xml');
}
}

if (SuluKernel::CONTEXT_WEBSITE === $container->getParameter('sulu.context')) {
Expand Down
42 changes: 42 additions & 0 deletions Dynamic/Types/RecaptchaV3Type.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/*
* This file is part of Sulu.
*
* (c) Sulu GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\FormBundle\Dynamic\Types;

use Sulu\Bundle\FormBundle\Dynamic\FormFieldTypeConfiguration;
use Sulu\Bundle\FormBundle\Dynamic\FormFieldTypeInterface;
use Sulu\Bundle\FormBundle\Entity\FormField;
use Symfony\Component\Form\FormBuilderInterface;

/**
* The Recaptcha form field type.
*/
class RecaptchaV3Type implements FormFieldTypeInterface
{
use SimpleTypeTrait;

public function getConfiguration(): FormFieldTypeConfiguration
{
return new FormFieldTypeConfiguration(
'sulu_form.type.recaptcha',
__DIR__ . '/../../Resources/config/form-fields/field_recaptcha.xml',
'special'
);
}

public function build(FormBuilderInterface $builder, FormField $field, string $locale, array $options): void
{
// Use in this way the recaptcha bundle could maybe not exists.
$options['mapped'] = false;
$options['constraints'] = new \EWZ\Bundle\RecaptchaBundle\Validator\Constraints\IsTrueV3();
$builder->add($field->getKey(), \EWZ\Bundle\RecaptchaBundle\Form\Type\EWZRecaptchaV3Type::class, $options);
}
}
11 changes: 11 additions & 0 deletions Resources/config/type_recaptcha_v3.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="sulu_form.dynamic.type_recaptcha" class="Sulu\Bundle\FormBundle\Dynamic\Types\RecaptchaV3Type">
<tag name="sulu_form.dynamic.type" alias="recaptcha"/>
</service>
</services>
</container>

0 comments on commit dfb6aec

Please sign in to comment.