You are here

function simple_recaptcha_library_info_build in Simple Google reCAPTCHA 8

Implements hook_library_info_build().

File

./simple_recaptcha.module, line 74
Provides common hooks and functions for simple_google_recaptcha module.

Code

function simple_recaptcha_library_info_build() {
  $config = \Drupal::configFactory()
    ->get('simple_recaptcha.config');
  $libraries = [];

  // reCAPTCHA v3 requires site key added to URL when loading it,
  // as site key is stored in config we need to build this library dynamically.
  $url = 'https://www.google.com/recaptcha/api.js';

  // Optionally serve api.js from recaptcha.net domain (based on configuration).
  if ($config
    ->get('recaptcha_use_globally')) {
    $url = 'https://www.recaptcha.net/recaptcha/api.js';
  }

  // Base library for reCAPTCHA v2.
  $libraries['recaptcha'] = [
    'js' => [
      $url => [
        'type' => 'external',
        'minified' => TRUE,
        'attributes' => [
          'defer' => TRUE,
          'async' => TRUE,
        ],
      ],
    ],
  ];

  // Additional library for V3 if site key is provided.
  if ($config
    ->get('site_key_v3')) {
    $url .= '?render=' . $config
      ->get('site_key_v3');
    $libraries['recaptcha_v3'] = [
      'js' => [
        $url => [
          'type' => 'external',
          'minified' => TRUE,
          'attributes' => [
            'defer' => TRUE,
            'async' => TRUE,
          ],
        ],
      ],
    ];
  }
  return $libraries;
}