Cropper library in libraries/cropper/dist does not load for local

Created on 10 August 2019, about 5 years ago
Updated 8 February 2024, 8 months ago

From the README.md file of Image Widget Crop module:

* Local library:
1. Download the latest version of [Cropper].
2. Copy the dist folder into:
- /libraries/cropper/dist
3. Enable the libraries module.

After doing the above for Cropper Library version v3.1.3 (I've tried the same for v3.1.6, and latest v4.0.0, with same results), this is how the directory looks like:

/libraries/cropper/dist
 |_ cropper.css
 |_ cropper.js
 |_ cropper.common.js
 |_ cropper.esm.js
 |_ cropper.min.css
 |_ cropper.min.js

My settings at admin/config/media/crop-widget ('Custom Cropper library' and 'Custom Cropper CSS file') are empty. The note there says:
Set the URL or local path for the file, or leave empty to use the installed library (if present) or a CDN fallback. You can retrieve the library file from Cropper CDN.

Even though the Cropper library is present at /libraries/cropper/dist, on my site, I see that the default CDN is still being used, not my local libraries. I have to copy (not cut) all files from /libraries/cropper/dist to /libraries/cropper (which means I need to have those files at both locations), for local files to be picked up. There might be some issue with the code.

I have discovered the following:

  • In image_widget_crop.module, function image_widget_crop_library_info_alter:
      // If Libraries exist and cropper library is available via Libraries API.
      elseif (\Drupal::moduleHandler()->moduleExists('libraries')
        && ($info = libraries_detect('cropper'))
        && $info['installed']) {
    

    $info['installed'] returns False, so local libraries are not used, this should have been True.

  • In libraries_detect('cropper') in libraries.module (currently version 8.x-3.0-alpha1), I found that it tries to locate the library files only in directory /libraries/cropper, it doesn't find it there, and gives out an error in a json, with $info['installed'] set to False. This is how the $info array looks like after the libraries_detect('cropper') call:
    Array(
        [name] => cropper
        [vendor url] => https://github.com/fengyuanchen/cropper
        [download url] => https://cdnjs.com/libraries/cropper
        [version arguments] => Array(
                [file] => cropper.min.js
                [pattern] => /Cropper v(.*)/
                [lines] => 2
            )
        [files] => Array(
                [js] => Array(
                        [cropper.min.js] => Array()
                    )
                [css] => Array(
                        [cropper.min.css] => Array()
                    )
            )
        [module] => image_widget_crop
        [machine name] => cropper
        [path] => 
        [library path] => libraries/cropper
        [version callback] => libraries_get_version
        [dependencies] => Array()
        [variants] => Array()
        [versions] => Array()
        [integration files] => Array()
        [callbacks] => Array(
                [info] => Array(
                        [0] => libraries_prepare_files
                    )
                [pre-detect] => Array()
                [post-detect] => Array(
                        [0] => libraries_detect_dependencies
                    )
                [pre-load] => Array()
                [post-load] => Array()
            )
        [installed] => 
        [version] => 
        [error] => not detected
        [error message] => Drupal\Core\StringTranslation\TranslatableMarkup Object(
                [translatedMarkup:protected] => 
                [options:protected] => Array()
                [stringTranslation:protected] => 
                [string:protected] => The version of the %library library could not be detected.
                [arguments:protected] => Array(
                        [%library] => cropper
                    )
            )
    )
    

    Note that installed, version are empty, and the error message. This leads to the code thinking the library is not installed locally, and hence uses the default CDN.

    If I copy the cropper.min.css and cropper.min.js from /libraries/cropper/dist to /libraries/cropper, the above looks like:

    Array(
        [name] => cropper
        [vendor url] => https://github.com/fengyuanchen/cropper
        [download url] => https://cdnjs.com/libraries/cropper
        [version arguments] => Array(
                [file] => cropper.min.js
                [pattern] => /Cropper v(.*)/
                [lines] => 2
            )
        [files] => Array(
                [js] => Array(
                        [cropper.min.js] => Array()
                    )
                [css] => Array(
                        [cropper.min.css] => Array()
                    )
            )
        [module] => image_widget_crop
        [machine name] => cropper
        [path] => 
        [library path] => libraries/cropper
        [version callback] => libraries_get_version
        [dependencies] => Array()
        [variants] => Array()
        [versions] => Array()
        [integration files] => Array()
        [callbacks] => Array(
                [info] => Array(
                        [0] => libraries_prepare_files
                    )
                [pre-detect] => Array()
                [post-detect] => Array(
                        [0] => libraries_detect_dependencies
                    )
                [pre-load] => Array()
                [post-load] => Array()
            )
        [installed] => 1
        [version] => 3.1.3
    )

    And now I see that the cropper.min.js and cropper.min.css are correctly loaded from my local.

    I use composer to install the Cropper library from https://github.com/fengyuanchen/cropper, so it comes with dist directory. I do not want to manually copy files from dist to its parent directory. But I do not know how to patch this.

🐛 Bug report
Status

Closed: works as designed

Version

2.2

Component

Code

Created by

🇮🇳India fishfin

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024