Commit b3363bd9 authored by nikunjkotecha's avatar nikunjkotecha Committed by nikunj
Browse files

Issue #2895666 by nikunjkotecha, dsdeiz, mparker17, Jelle_S: Allow jQuery...

Issue #2895666 by nikunjkotecha, dsdeiz, mparker17, Jelle_S: Allow jQuery Validation library to be installed outside of module folder
parent e54fdd90
......@@ -7,18 +7,60 @@ form elements. If an HTML5 attribute exists it is used as is.
## clientside_validation_jquery
This modules adds the [jQuery Validation Plugin](http://jqueryvalidation.org/),
to install (download the latest version)[http://jqueryvalidation.org/files/jquery-validation-1.14.0.zip]
and extract the zip file to the js/lib of this sub module.
This modules adds the [jQuery Validation Plugin](http://jqueryvalidation.org/).
If you don't download the library, it will be hot linked to the CDN version.
If you use this module as-is and you do not download the library, it will be
automatically included by linking to the CDN version.
If you would prefer to download the library, you can either install it manually
or using Composer.
To install the library manually:
1. Download the jQuery Validation library from http://jqueryvalidation.org/ .
jQuery Validation 1.17.0 or higher is recommended.
2. If it does not already exist, create a folder named `libraries` at the web
root of your Drupal site. Then, create a folder named `jqueryvalidate`
inside of the `libraries` folder (i.e.: `/libraries/jqueryvalidate`).
3. Extract the ZIP you downloaded in step 1 inside of the
`/libraries/jqueryvalidate` folder (i.e.: so that the `jquery.validate.js`
file is at `/libraries/jqueryvalidate/dist/jquery.validate.js`).
To install the library using Composer:
1. Add the proper repository to your `composer.json` file to be able to require
the JS library:
```
{
"type": "package",
"package": {
"name": "jqueryvalidate",
"version": "1.17.0",
"type": "drupal-library",
"dist": {
"url": "https://github.com/jquery-validation/jquery-validation/releases/download/1.17.0/jquery-validation-1.17.0.zip",
"type": "zip"
}
}
}
```
It is always good to download and use the latest version here but new
versions may not work as expected since those are not tested properly.
2. Run `composer require jqueryvalidate:~1.0`
3. Install module as usual.
# Extend
If you need support for other contrib modules, you can add a CvValidator plugin
to that module and it will be picked up by the base module.
If you require custom javascript, you can implement ```clientside_validation_jquery_clientside_validation_validator_info_alter```
If you require custom javascript, you can implement `hook_clientside_validation_validator_info_alter()`
# Contribute
......@@ -27,4 +69,3 @@ See the [Drupal 8 port issue](https://www.drupal.org/node/2610804)
# Test it
On [simplytest.me](https://simplytest.me/project/clientside_validation/8.x-1.x)
jquery.validate:
js:
js/lib/dist/jquery.validate.js: {}
/libraries/jqueryvalidate/dist/jquery.validate.js: {}
dependencies:
- core/drupal
- core/jquery
jquery.validate.additional:
js:
js/lib/dist/additional-methods.js: {}
/libraries/jqueryvalidate/dist/additional-methods.js: {}
dependencies:
- clientside_validation_jquery/jquery.validate
cv.jquery.validate:
......
......@@ -46,22 +46,71 @@ function clientside_validation_jquery_clientside_validation_validator_info_alter
*/
function clientside_validation_jquery_library_info_alter(&$libraries, $extension) {
if ($extension == 'clientside_validation_jquery' && isset($libraries['jquery.validate'])) {
if (!file_exists(drupal_get_path('module', 'clientside_validation_jquery') . '/js/lib/dist/jquery.validate.js')) {
// Previously we told users to extract the /dist folder. Make sure we still
// support this.
if (file_exists(drupal_get_path('module', 'clientside_validation_jquery') . '/js/lib/jquery.validate.js')) {
unset($libraries['jquery.validate']['js']['js/lib/dist/jquery.validate.js']);
// Based on updated readme instructions, check in libraries.
if (file_exists('libraries/jqueryvalidate/dist/jquery.validate.js')) {
// We do nothing here if it is available in libraries.
return;
}
$module_path = drupal_get_path('module', 'clientside_validation_jquery');
$path_in_yml = '/libraries/jqueryvalidate/dist/';
// 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'] = [];
unset($libraries['jquery.validate.additional']['js']['js/lib/dist/additional-methods.js']);
$libraries['jquery.validate']['js']['js/lib/additional-methods.js'] = [];
}
else {
// Use CDN version if local version doesn't exist.
unset($libraries['jquery.validate']['js']['js/lib/dist/jquery.validate.js']);
$libraries['jquery.validate']['js']['//cdn.jsdelivr.net/jquery.validation/1.14.0/jquery.validate.min.js'] = ['type' => 'external'];
unset($libraries['jquery.validate.additional']['js']['js/lib/dist/additional-methods.js']);
$libraries['jquery.validate.additional']['js']['//cdn.jsdelivr.net/jquery.validation/1.14.0/additional-methods.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']['js'][$path . 'additional-methods.js'] = [];
}
// Nothing left to be processed now, we simply return.
return;
}
}
// Use JS from CDN as we didn't find it in module or libraries.
// @TODO: Make version number configurable here.
$cdn_url = '//cdn.jsdelivr.net/npm/jquery-validation@1.17.0/dist/';
// 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_requirements().
*/
function clientside_validation_jquery_requirements($phase) {
$requirements = [];
if ($phase == 'runtime') {
$library_exists = file_exists('libraries/jqueryvalidate/dist/jquery.validate.js');
$requirements['clientside_validation_jquery_library'] = [
'title' => t('Clientside Validation jQuery library'),
'value' => $library_exists ? t('Downloaded in Library') : t('Downloaded in module OR using from CDN'),
'description' => $library_exists ? '' : t('Check README in module folder for instructions on how to use it through libraries (Drupal way).'),
'severity' => $library_exists ? REQUIREMENT_OK : REQUIREMENT_WARNING,
];
}
return $requirements;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment