diff --git src/Form/TurnstileAdminSettingsForm.php src/Form/TurnstileAdminSettingsForm.php index 21ebcef..6e5ac83 100644 --- src/Form/TurnstileAdminSettingsForm.php +++ src/Form/TurnstileAdminSettingsForm.php @@ -2,15 +2,44 @@ namespace Drupal\turnstile\Form; +use Drupal\Core\Cache\CacheTagsInvalidatorInterface; +use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Form\ConfigFormBase; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Url; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Configure Turnstile settings for this site. */ class TurnstileAdminSettingsForm extends ConfigFormBase { + /** + * The cache tags invalidator service. + * + * @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface + */ + protected $cacheTagsInvalidator; + + /** + * {@inheritdoc} + */ + public function __construct(ConfigFactoryInterface $config_factory, CacheTagsInvalidatorInterface $cache_tags_invalidator) { + parent::__construct($config_factory); + + $this->cacheTagsInvalidator = $cache_tags_invalidator; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('config.factory'), + $container->get('cache_tags.invalidator'), + ); + } + /** * {@inheritdoc} */ @@ -184,6 +213,8 @@ class TurnstileAdminSettingsForm extends ConfigFormBase { * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { + $this->cacheTagsInvalidator->invalidateTags(['library_info']); + $config = $this->config('turnstile.settings'); $config ->set('site_key', $form_state->getValue('turnstile_site_key')) diff --git src/Turnstile/Turnstile.php src/Turnstile/Turnstile.php index 4940e48..e2a6524 100644 --- src/Turnstile/Turnstile.php +++ src/Turnstile/Turnstile.php @@ -106,6 +106,7 @@ class Turnstile { public function getWidget($validation_function) { // Add the Turnstile Ajax Commands library. $widget['form']['#attached']['library'][] = 'turnstile/turnstile.ajax'; + $widget['form']['#cache']['tags'][] = 'library_info'; // Captcha requires TRUE to be returned in solution. $widget['solution'] = TRUE; diff --git turnstile.libraries.yml turnstile.libraries.yml index e7d8aa6..6825242 100644 --- turnstile.libraries.yml +++ turnstile.libraries.yml @@ -6,4 +6,5 @@ turnstile.ajax: turnstile.ajax.js: {} dependencies: - core/jquery - - core/drupal.ajax \ No newline at end of file + - core/drupal.ajax + - turnstile/turnstile diff --git turnstile.module turnstile.module index eca4c47..e68990e 100644 --- turnstile.module +++ turnstile.module @@ -6,7 +6,6 @@ */ use Drupal\Core\Routing\RouteMatchInterface; -use Drupal\Core\Url; use Drupal\turnstile\Turnstile\Drupal8Post; use Drupal\turnstile\Turnstile\Turnstile; @@ -43,25 +42,6 @@ function turnstile_captcha($op, $captcha_type = '') { $turnstile = new Turnstile($turnstile_site_key, $turnstile_secret_key, $attributes); $captcha = $turnstile->getWidget('turnstile_captcha_validation'); - - $turnstile_src = $config->get('turnstile_src'); - $captcha['form']['turnstile_widget']['#attached'] = [ - 'html_head' => [ - [ - [ - '#tag' => 'script', - '#attributes' => [ - 'src' => Url::fromUri($turnstile_src, [ - 'absolute' => TRUE, - ])->toString(), - 'async' => TRUE, - 'defer' => TRUE, - ], - ], - 'turnstile_api', - ], - ], - ]; } else { // Fallback to Math captcha as Turnstile is not configured. @@ -75,6 +55,18 @@ function turnstile_captcha($op, $captcha_type = '') { } } +/** + * Implements hook_library_info_build(). + */ +function turnstile_library_info_build() { + $config = \Drupal::config('turnstile.settings'); + $turnstile_src = $config->get('turnstile_src'); + + $libraries['turnstile']['js'][$turnstile_src] = []; + + return $libraries; +} + /** * CAPTCHA Callback; Validates the Turnstile code. */