yandex_smartcaptcha-1.0.2/yandex_smartcaptcha.module
yandex_smartcaptcha.module
<?php
/**
* @file
* Primary module hooks for yandex_smartcaptcha module.
*/
use Drupal\Core\Asset\AttachedAssetsInterface;
use Drupal\Core\Form\FormStateInterface;
/**
* Implements hook_help().
*/
function yandex_smartcaptcha_help($route_name) {
switch ($route_name) {
case 'help.page.yandex_smartcaptcha':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('This module provides Yandex SmartCaptcha integration.') . '</p>';
return $output;
}
}
/**
* Implements hook_library_info_alter().
*/
function yandex_smartcaptcha_library_info_alter(&$libraries, $extension) {
if ($extension === 'yandex_smartcaptcha' && isset($libraries['smartcaptcha.library'])) {
$api_url = array_keys($libraries['smartcaptcha.library']['js']);
$api_url = array_shift($api_url);
$key = $api_url . '?render=onload&onload=smartCaptchaInit';
$js = array_shift($libraries['smartcaptcha.library']['js']);
$libraries['smartcaptcha.library']['js'][$key] = $js;
}
}
/**
* Implements hook_js_alter().
*/
function yandex_smartcaptcha_js_alter(&$javascript, AttachedAssetsInterface $assets) {
$jquery_form = 'core/assets/vendor/jquery-form/jquery.form.min.js';
$smartcaptcha_element = \Drupal::service('extension.list.module')
->getPath('yandex_smartcaptcha') . '/js/smartcaptcha-element.js';
$library = \Drupal::service('library.discovery')
->getLibraryByName('yandex_smartcaptcha', 'smartcaptcha.library');
$smartcaptcha_library = $library['js'][0]['data'];
// Set correct loading order for dependent js libraries.
if (isset($javascript[$smartcaptcha_element]) && isset($javascript[$jquery_form]) && $smartcaptcha_library) {
$javascript[$smartcaptcha_element]['weight'] = $javascript[$jquery_form]['weight'] + .1;
$javascript[$smartcaptcha_library]['weight'] = $javascript[$smartcaptcha_element]['weight'] + .1;
}
}
/**
* Implements hook_form_alter().
*/
function yandex_smartcaptcha_form_alter(&$form, FormStateInterface $form_state, $form_id) {
/** @var \Drupal\yandex_smartcaptcha\YandexSmartCaptcha $smart_captcha */
$smart_captcha = \Drupal::service('yandex_smartcaptcha.service');
// Do not check if SmartCaptcha is disabled
// or if the primary submit button is not clicked.
foreach ($smart_captcha->getAttachedFormsIds() as $attached_form_id => $attached_form_settings) {
if ($form_id == $attached_form_id && !empty($attached_form_settings['enabled'])) {
$form['smartcaptcha_element'] = $smart_captcha->getCaptchaFormElement($attached_form_settings);
}
}
// Make sure the Webform module is installed.
if (\Drupal::moduleHandler()->moduleExists('webform')) {
// Show smartcaptcha only on selected pages.
if (!empty($form['#webform_id']) && !empty($form["smartcaptcha_element"])) {
$current_page = $form_state->get('current_page');
if ($form_state->get('pages') && $current_page) {
if ($current_page != $smart_captcha->getCurrentWebformDisplayPage()) {
// Hide captсha element.
unset($form['smartcaptcha_element']);
}
}
}
}
}
/**
* A hook_captcha_captcha implementation.
*/
function yandex_smartcaptcha_captcha($op, $captcha_type = '', $captcha_sid = NULL) {
switch ($op) {
case 'list':
return ['Yandex SmartCaptcha'];
case 'generate':
if ($captcha_type == 'Yandex SmartCaptcha') {
$captcha = [];
// Build the reCAPTCHA captcha form if site_key and secret_key are
// configured. Captcha requires TRUE to be returned in solution.
$captcha['solution'] = TRUE;
// As the validate callback does not depend on sid or solution, this
// captcha type can be displayed on cached pages.
$captcha['cacheable'] = TRUE;
$captcha['form']['smartcaptcha_element'] = [
'#type' => 'smartcaptcha_element',
'#smartcaptcha' => [],
];
$captcha['captcha_validate'] = 'yandex_smartcaptcha_captcha_validation';
return $captcha;
}
break;
}
}
/**
* Implements mandatory hook_captcha_validation().
*/
function yandex_smartcaptcha_captcha_validation($solution, $response, $element, $form_state) {
// The main validation was made in the
// YandexSmartCaptchaElement::validateCaptcha() handler.
// Here, we just check the token value.
return !empty($element["captcha_token"]["#value"]);
}
