clientside_validation_jquery.module in Clientside Validation 8.2
Hook implementations for the Clientside Validation jQuery module.
File
clientside_validation_jquery/clientside_validation_jquery.moduleView source
<?php
/**
* @file
* Hook implementations for the Clientside Validation jQuery module.
*/
use Drupal\Core\Asset\AttachedAssetsInterface;
use Drupal\Core\Cache\Cache;
/**
* Implements hook_js_alter().
*/
function clientside_validation_jquery_js_alter(&$javascript, AttachedAssetsInterface $assets) {
if (\Drupal::moduleHandler()
->moduleExists('ckeditor')) {
$library_discovery = \Drupal::service('library.discovery');
$ckeditor = $library_discovery
->getLibraryByName('ckeditor', 'drupal.ckeditor');
foreach ($ckeditor['js'] as $ckeditor_file) {
if (strpos($ckeditor_file['data'], 'ckeditor.js') !== FALSE) {
break;
}
}
$cv_jquery = $library_discovery
->getLibraryByName('clientside_validation_jquery', 'cv.jquery.ckeditor');
foreach ($cv_jquery['js'] as $cv_jquery_file) {
if (strpos($cv_jquery_file['data'], 'cv.jquery.ckeditor.js') !== FALSE) {
break;
}
}
// Add our scripts after ckeditor.
if (isset($javascript[$ckeditor_file['data']]) && isset($javascript[$cv_jquery_file['data']])) {
$javascript[$cv_jquery_file['data']]['weight'] = $javascript[$ckeditor_file['data']]['weight'] + 0.001;
}
}
}
/**
* Implements hook_clientside_validation_validator_info_alter().
*/
function clientside_validation_jquery_clientside_validation_validator_info_alter(&$validators) {
foreach ($validators as &$validator) {
$validator['attachments']['library'][] = 'clientside_validation_jquery/cv.jquery.validate';
if (\Drupal::moduleHandler()
->moduleExists('ckeditor')) {
$validator['attachments']['library'][] = 'clientside_validation_jquery/cv.jquery.ckeditor';
}
if (\Drupal::moduleHandler()
->moduleExists('inline_form_errors')) {
$validator['attachments']['library'][] = 'clientside_validation_jquery/cv.jquery.ife';
}
}
}
/**
* Implements hook_library_info_alter().
*/
function clientside_validation_jquery_library_info_alter(&$libraries, $extension) {
if ($extension == 'clientside_validation_jquery' && isset($libraries['jquery.validate'])) {
$module_path = drupal_get_path('module', 'clientside_validation_jquery');
$path_in_yml = '/libraries/jquery-validation/dist/';
// Load settings from config.
$config = \Drupal::config('clientside_validation_jquery.settings');
// Check for library or js in module only if use_cdn flag is set to false.
if (empty($config
->get('use_cdn'))) {
// Based on updated readme instructions, check in libraries.
if (file_exists('libraries/jquery-validation/dist/jquery.validate.js')) {
// We do nothing here if it is available in libraries.
return;
}
// Previously we told users to extract the /dist folder.
// Make sure we still support this.
// Check direct extraction and extraction of just dist dir both.
$paths_to_check = [
'/js/lib/',
'/js/lib/dist/',
];
foreach ($paths_to_check as $path) {
if (file_exists($module_path . $path . 'jquery.validate.js')) {
unset($libraries['jquery.validate']['js'][$path_in_yml . 'jquery.validate.js']);
$libraries['jquery.validate']['js']['js/lib/jquery.validate.js'] = [];
// Add additional methods js only if required.
if (isset($libraries['jquery.validate.additional']['js'][$path_in_yml . 'additional-methods.js'])) {
unset($libraries['jquery.validate.additional']['js'][$path_in_yml . 'additional-methods.js']);
$libraries['jquery.validate']['js'][$path . 'additional-methods.js'] = [];
}
// Nothing left to be processed now, we simply return.
return;
}
}
}
// Use JS from CDN.
// Fallback to CDN if not available in libraries or module.
// Also use this by default if config says use CDN.
$cdn_url = $config
->get('cdn_base_url');
// For CDN we use the min versions as Drupal is not going to compress them.
unset($libraries['jquery.validate']['js'][$path_in_yml . 'jquery.validate.js']);
$libraries['jquery.validate']['js'][$cdn_url . 'jquery.validate.min.js'] = [
'type' => 'external',
];
// Add additional methods js only if required.
if (isset($libraries['jquery.validate.additional']['js'][$path_in_yml . 'additional-methods.js'])) {
unset($libraries['jquery.validate.additional']['js'][$path_in_yml . 'additional-methods.js']);
$libraries['jquery.validate.additional']['js'][$cdn_url . 'additional-methods.min.js'] = [
'type' => 'external',
];
}
}
}
/**
* Implements hook_page_attachments().
*
* Adds clientside_validation_jquery config to settings.
*
* @see contextual_preprocess()
*/
function clientside_validation_jquery_page_attachments(array &$page) {
$config = \Drupal::config('clientside_validation_jquery.settings');
$page['#attached']['drupalSettings']['clientside_validation_jquery']['validate_all_ajax_forms'] = (int) $config
->get('validate_all_ajax_forms');
$page['#attached']['drupalSettings']['clientside_validation_jquery']['force_validate_on_blur'] = (bool) $config
->get('force_validate_on_blur');
$page['#attached']['drupalSettings']['clientside_validation_jquery']['messages'] = [
'required' => t('This field is required.'),
'remote' => t('Please fix this field.'),
'email' => t('Please enter a valid email address.'),
'url' => t('Please enter a valid URL.'),
'date' => t('Please enter a valid date.'),
'dateISO' => t('Please enter a valid date (ISO).'),
'number' => t('Please enter a valid number.'),
'digits' => t('Please enter only digits.'),
'equalTo' => t('Please enter the same value again.'),
'maxlength' => t('Please enter no more than {0} characters.'),
'minlength' => t('Please enter at least {0} characters.'),
'rangelength' => t('Please enter a value between {0} and {1} characters long.'),
'range' => t('Please enter a value between {0} and {1}.'),
'max' => t('Please enter a value less than or equal to {0}.'),
'min' => t('Please enter a value greater than or equal to {0}.'),
'step' => t('Please enter a multiple of {0}.'),
];
if (empty($page['#cache']['#tags'])) {
$page['#cache']['#tags'] = [];
}
$page['#cache']['#tags'] = Cache::mergeTags($page['#cache']['#tags'], $config
->getCacheTags());
}