diff --git a/composer.json b/composer.json index aea477b..379893f 100644 --- a/composer.json +++ b/composer.json @@ -10,11 +10,11 @@ "repositories": [ { "type": "composer", - "url": "https://packagist.drupal-composer.org" + "url": "https://packages.drupal.org/8" } ], "require": { - "drupal/crop": "~8.1" + "drupal/crop": "1.0.0" }, "license": "GPL-2.0+" } diff --git a/image_widget_crop.install b/image_widget_crop.install index cdedbfc..9450a6a 100644 --- a/image_widget_crop.install +++ b/image_widget_crop.install @@ -4,21 +4,12 @@ * @file * Install, update and uninstall functions for the ImageWidgetCrop module. */ -use Drupal\Core\Link; /** * Implements hook_requirements(). */ function image_widget_crop_requirements($phase) { $error = []; - - $libraries_active = FALSE; - // Detect if Libraries module & cropper library is active. - if (\Drupal::moduleHandler()->moduleExists('libraries') && ($info = libraries_detect('cropper')) - ) { - $libraries_active = TRUE; - } - $config = \Drupal::config('image_widget_crop.settings'); $files = [ 'js' => $config->get('settings.library_url'), @@ -28,19 +19,15 @@ function image_widget_crop_requirements($phase) { foreach ($files as $type => $file) { $is_local = parse_url($file, PHP_URL_SCHEME) === NULL && strpos($file, '//') !== 0; // If libraries module is active check if folder is malformed. - if ($is_local && $libraries_active && !file_exists($info['library path'] . '/cropper.min.' . $type)) { - $error[] = t(":type file : Libraries module is active but an error detected with your cropper libraries configuration. To use cropper library with 'libraries' module you must have the following structure `:libraries_cropper`", [ - ':type' => strtoupper($type), - ':libraries_cropper' => '/libraries/cropper/dist/cropper.min.' . $type - ]); + if ($is_local + && \Drupal::moduleHandler()->moduleExists('libraries') + && ($info = libraries_detect('cropper')) + && (!file_exists($info['library path'] . '/dist/cropper.min.' . $type) && !file_exists($info['library path'] . '/cropper.min.' . $type))) { + $error[] = t(":type file : Libraries module is active but an error detected with your cropper libraries configuration. To use cropper library with 'libraries' module you must have the following structure `:libraries_cropper`", [ + ':type' => strtoupper($type), + ':libraries_cropper' => '/libraries/cropper/dist/cropper.min.' . $type + ]); } - elseif (!$is_local) { - $result = \Drupal::httpClient()->request('GET', $file); - if ($result->getStatusCode() != 200) { - $error[] = t(":type file : The provided CDN path file path not found.", [':type' => strtoupper($type)]); - } - } - } $requirements = []; @@ -58,7 +45,7 @@ function image_widget_crop_requirements($phase) { } else { $requirements['iwc_libraries']['severity'] = REQUIREMENT_OK; - $requirements['iwc_libraries']['description'] = t('ImageWidgetCrop libraries files are correctly configured to use :library files', [ + $requirements['iwc_libraries']['description'] = t('ImageWidgetCrop libraries files are correctly configured to use :library files', [ ':library' => !$is_local ? 'CDN' : 'Libraries API', ]); } diff --git a/image_widget_crop.module b/image_widget_crop.module index cd06620..fbfd2e3 100644 --- a/image_widget_crop.module +++ b/image_widget_crop.module @@ -105,7 +105,6 @@ function image_widget_crop_libraries_info() { 'name' => 'cropper', 'vendor url' => 'https://github.com/fengyuanchen/cropper', 'download url' => 'https://cdnjs.com/libraries/cropper', - 'library path' => 'libraries/cropper/dist', 'version arguments' => [ 'file' => 'cropper.min.js', 'pattern' => '/Cropper v(.*)/', @@ -135,23 +134,31 @@ function image_widget_crop_library_info_alter(&$libraries, $extension) { $config = \Drupal::config('image_widget_crop.settings'); $js = $config->get('settings.library_url'); $css = $config->get('settings.css_url'); - + // Explicit configuration takes priority. if (!empty($js) && !empty($css)) { - // Explicit configuration takes priority. - $is_local = parse_url($js, PHP_URL_SCHEME) === NULL && strpos($js, '//') !== 0; - $libraries['cropper']['js'][$js] = [ - 'type' => $is_local ? 'file' : 'external', - 'minified' => TRUE, - ]; - $is_local = parse_url($css, PHP_URL_SCHEME) === NULL && strpos($css, '//') !== 0; - $libraries['cropper']['css']['component'][$css] = [ - 'type' => $is_local ? 'file' : 'external', - 'minified' => TRUE, - ]; + $files = ['js' => $js, 'css' => $css]; + foreach ($files as $type => $file_path) { + // Evaluate if the path are an local or external. + $is_local = parse_url($file_path, PHP_URL_SCHEME) === NULL && strpos($file_path, '//') !== 0; + // In that location $file_path are placed on root of module not in drupal root. + $data = ($is_local && substr($file_path, 0, 1) !== '/') ? '/' . $file_path : $file_path; + if ($type === 'js') { + $libraries['cropper'][$type][$data] = [ + 'type' => $is_local ? 'file' : 'external', + 'minified' => TRUE, + ]; + } else { + $libraries['cropper'][$type]['component'][$data] = [ + 'type' => $is_local ? 'file' : 'external', + 'minified' => TRUE, + ]; + } + } } // If Libraries exist and cropper library is available via Libraries API. elseif (\Drupal::moduleHandler()->moduleExists('libraries') - && ($info = libraries_detect('cropper')) && $info['installed']) { + && ($info = libraries_detect('cropper')) + && $info['installed']) { $libraries['cropper']['version'] = $info['version']; foreach ($info['files'] as $type => $files) { // Fetch all possible entry. @@ -169,6 +176,7 @@ function image_widget_crop_library_info_alter(&$libraries, $extension) { } else { $libraries['cropper'][$type][$data] = $option; + $libraries['cropper'][$type][$data] = $option; } } } diff --git a/src/Form/CropWidgetForm.php b/src/Form/CropWidgetForm.php index e8be0df..00f1125 100644 --- a/src/Form/CropWidgetForm.php +++ b/src/Form/CropWidgetForm.php @@ -104,8 +104,8 @@ class CropWidgetForm extends ConfigFormBase { // Indicate which files are used when custom urls are not set. if (\Drupal::moduleHandler()->moduleExists('libraries') && ($info = libraries_detect('cropper')) && $info['installed']) { - $form['library']['library_url']['#attributes']['placeholder'] = $info['library path'] . '/' . key($info['files']['js']); - $form['library']['css_url']['#attributes']['placeholder'] = $info['library path'] . '/' . key($info['files']['css']); + $form['library']['library_url']['#attributes']['placeholder'] = $info['library path'] . '/dist/' . key($info['files']['js']); + $form['library']['css_url']['#attributes']['placeholder'] = $info['library path'] . '/dist/' . key($info['files']['css']); } else { $form['library']['library_url']['#attributes']['placeholder'] = $cdn_js;