Problem/Motivation

Extracted from #2631732-13: Clean up libraries validation code:

[T]he Cropper library registration in hook_libraries_info expects the files in the library root (e.g. libraries/cropper/cropper.min.js). This does not match the structure of the packaged files, which places the script & style sheets in the /dist/ sub-folder. As this also effects backward compatibility for users that placed the files in the expected path, I have not yet addressed this issue.

Directory struckture should match Cropper library.

Proposed resolution

Change the hook_libraries_info implementation to include the dist directory.

Current implementation:

<?php
/**
 * Implements hook_libraries_info().
 */
function image_widget_crop_libraries_info() {
  $libraries = [
    'cropper' => [
      'name' => 'cropper',
      'vendor url' => 'https://github.com/fengyuanchen/cropper',
      'download url' => 'https://cdnjs.com/libraries/cropper',
      'version arguments' => [
        'file' => 'cropper.min.js',
        'pattern' => '/Cropper v(.*)/',
        'lines' => 2,
      ],
      'files' => [
        'js' => [
          'cropper.min.js' => [],
        ],
        'css' => [
          'cropper.min.css' => [],
        ],
      ],
    ],
  ];
  return $libraries;
}
?>

Compatibility

This change is not backwards compatible and requires existing installations to adjust to the new structure by moving the files. Maybe this can be handled by an update hook?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

ckaotik created an issue. See original summary.

ckaotik’s picture

Issue summary: View changes
woprrr’s picture

Yes is now my focus to update that :). I purpose a patch fast to solve it thank a lot @ckaotik

ckaotik’s picture

Great to hear! I didn't want to push you, just wanted to make sure that part is not forgotten.
Keep it up :thumbsup:

MXT’s picture

Category: Task » Feature request
Priority: Normal » Major

I'm changing priority of this feature request because using Composer to download Cropper library the code comes within /dist, so nothing works with actual DEV.

Please, consider this ASAP because Composer workflow to manage D8 websites is largely diffuse now.

Thank you very much!

woprrr’s picture

Category: Feature request » Bug report

I change that into bug because that is verry prior issue to purpose an new release of module ! I fix that Today can you stand ready to test it fastly ?

woprrr’s picture

@MXT for using composer to downloading cropper can you purpose a patch ?

This issue is the last blocker before release. @ckaotik have you a small moment to view this with me ? :)

I ve begin tp create composer.json file:

{
  "name": "drupal/image_widget_crop",
  "description": "Provides an interface for using the features of the Crop API.",
  "type": "drupal-module",
  "homepage": "https://www.drupal.org/project/image_widget_crop",
  "support": {
    "issues": "https://www.drupal.org/project/issues/image_widget_crop",
    "source": "https://www.drupal.org/project/image_widget_crop"
  },
  "repositories": [
    {
      "type": "composer",
      "url": "https://packagist.drupal-composer.org"
    }
  ],
  "authors": [
    {
      "name": "Alexandre Mallet",
      "homepage": "https://www.drupal.org/u/woprrr"
    }
  ],
  "require": {
    "drupal/crop": "~8.1"
  }
  "license": "GPL-2.0+"
}
ckaotik’s picture

Hey @woprrr,

I'm not that firm on Composer, either. I have no idea how Composer handles installation of a JS library. The documentation states:

There are two ways of downloading a package: source and dist. For stable versions Composer will use the dist by default. The source is a version control repository.

This might mean that the user gets a dist directory, or doesn't. Also, if we included fengyuanchen/cropper as a dependency, we might run into trouble as the library requires component/jquery when Drupal uses drupal/jquery. We don't want multiple jQuery installations in one Drupal *shudder*

You can always compare against other modules' composer.json, e.g. from Address (which needs a PHP library). They provide far less information in theirs.

On the other hand, https://drupal.org/project/dropzonejs is in a similar situation as we are (i.e. the need for a JS library), and doesn't use Composer to manage that library. Instead, they provide download instructions in their README.md and have no composer.json at all.

Composer really is more for PHP libraries. Personally, I prefer the "old-fashioned" way followed by dropzonejs, as we are less likely to break things ;) That probably means we'd have to simply ignore composer with regards to the library, and adjust the hook_libraries_info files to something like $libraries['cropper']['files']['js']['dist/cropper.min.js' = [] for both files.
Then the question remains, on how to upgrade existing installs - or whether just to provide information on the changed location on drupal.org's release notes.

ckaotik’s picture

One afterthought: If you still wish to use Composer for the JS library, you might want to take a look at this issue on StackOverflow :)

woprrr’s picture

That patch prevent all cases with update_n for old user of libraries. FileSystem component in Symfony basis are powerfull for manipulate files / folders.

I ve process an helper in update_n because two cases possible with libraries for location of files and i prefer an approach more solid and detest code duplication :P

Can you test it and tell me if all is okay for u ? For me all works fine in all cases

I ve just an problem to stop Update_n when exception fired (in extrem cases if we have an files with name modified i think we can't prevent all exotic cases in this update_n because that introduce an too complex logic).

Berdir’s picture

No, this is not OK.

You can't move files, that's not your job in an update function. If it has to change, then you need to document it instead.

Many people today deploy to containers and other read-only file systems. They use tools to download assets and so on.

This seems like a problematic change for a stable update. Maybe there's a way to support both?

woprrr’s picture

@berdir i see your opinion, and I agree that. It better to inform users using libraries to move her files manually. You think is better to add a notice in report page ?

If you take abstraction of .install all work for you when you have /libraries/cropper/dist/* ?

I Re-patch fast when I have your opinion about report notice :)

Thank a lot for you fast feedback berdir I am glad to see you in iwc issues :)

woprrr’s picture

I ve re-patch, but can you confirm me if these start of solution feel like for you ? I add an requirment to verify and prevent user to change her configuration.

woprrr’s picture

I purpose that :) is more precise & clear.

Berdir’s picture

Status: Needs review » Needs work
  1. +++ b/composer.json
    @@ -0,0 +1,20 @@
    +  "require": {
    +    "drupal/crop": "~8.1"
    +  },
    

    Specifying a version is problematic, see https://www.drupal.org/node/2817789#comment-11730919.

    People using the official d.o packages have version numbers like 1.0.0, not 8.1.0. So this won't install for them. Just put a * there or try specifying boh versions, not sure how well that would work.

  2. +++ b/image_widget_crop.install
    @@ -0,0 +1,66 @@
    +  if (\Drupal::moduleHandler()->moduleExists('libraries') && ($info = libraries_detect('cropper'))
    +  ) {
    

    ") {" should be on the same line as the if

  3. +++ b/image_widget_crop.install
    @@ -0,0 +1,66 @@
    +    elseif (!$is_local) {
    +      $result = \Drupal::httpClient()->request('GET', $file);
    +      if ($result->getStatusCode() != 200) {
    +        $error[] = t("<b>:type</b> file : The provided CDN path file path not found.", [':type' => strtoupper($type)]);
    +      }
    +    }
    

    I'm not sure if doing this request is worth it. If people put a wrong path there, then they should see a 404 entry in their logs. No strong opinion, fine if you'd prefer to have this. Maybe only do a HEAD request though, to avoid downloading the file?

  4. +++ b/image_widget_crop.install
    @@ -0,0 +1,66 @@
    +    $requirements['iwc_libraries']['description'] = t('ImageWidgetCrop libraries files are correctly configured to use <b>:library</b> files', [
    

    I think <strong> should be used instead of <b> these days?

  5. +++ b/image_widget_crop.module
    @@ -105,6 +105,7 @@ function image_widget_crop_libraries_info() {
           'download url' => 'https://cdnjs.com/libraries/cropper',
    +      'library path' => 'libraries/cropper/dist',
           'version arguments' => [
    

    why specify this? according to http://www.drupalcontrib.org/api/drupal/contributions!libraries!librarie..., this shouldn't be used, as other locations (like profiles/someting/libraries/cropper) wouldn't work anymore?

    I think you want to specify dist/some_file.. instead later in the code, for the actual files?

That said, I still think you should consider doing a check for the file with and without dist/ for backwards compatible. People don't expect that things change in a stable module update. At the very least, you should put a big warning into the release notes.

woprrr’s picture

Thank a lot again @berdir for your full feedback :) I re-patch with your feedback, yes you right, I think I have too much more complex in the Previous patch to try to cover too many cases as possible. Nothing beats a nice line in the description of the release;). About backward compatibility part, I totally agree with you !! so I leave the options you have to be able to function with the old structure.

For the first point : Yes you right !!! I was still on the old version on packagist-drupal, thanks to you I am now aware

2. Yes !! fixed
3. Yes you right too in this case it's not really revelant to test it and increase complexity, i delete this unused part.
4. I ve found the two cases on core, but i can switch easy to Fixed.
5. Yes ! wrong way for me i ve read more doccumentation about libraries and patch it ! I see to maintain backward compatibility with old / new possible path.

  • woprrr committed 4114d49 on 8.x-1.x
    Issue #2793455 by woprrr, ckaotik, Berdir: Libraries registration does...
  • woprrr committed cb93e8f on 8.x-1.x
    Issue #2793455 by woprrr, ckaotik, Berdir: Libraries registration does...
woprrr’s picture

Status: Needs review » Fixed
woprrr’s picture

Status: Fixed » Closed (fixed)