Commit 0bd84ed1 authored by Christian Bläul's avatar Christian Bläul
Browse files

Issue #3278683 by Gogowitsch, SocialNicheGuru, lucashodge: Use composer to set library paths

parent 8719f80a
Loading
Loading
Loading
Loading
+36 −0
Original line number Diff line number Diff line
@@ -21,3 +21,39 @@ function handsontable_yml_webform_requirements() {
    ],
  ];
}

/**
 * Implements hook_library_info_alter().
 *
 * Allows adding Handsontable as a package to the "repositories"
 * section of the root composer.json.
 *
 * @see
 * https://www.drupal.org/project/handsontable_yml_webform/issues/3278683#comment-14601528
 */
function handsontable_yml_webform_library_info_alter(&$libraries, $extension) {
  if ($extension == 'handsontable_yml_webform' && isset($libraries['handsontable'])) {

    if (Drupal::hasService('library.libraries_directory_file_finder')) {
      /** @var \Drupal\Core\Asset\LibrariesDirectoryFileFinder $library_file_finder */
      $library_file_finder = Drupal::service('library.libraries_directory_file_finder');
      $library_path = $library_file_finder->find('handsontable');
      if (!$library_path) {
        return;
      }
      $js = '/' . $library_path . '/handsontable/dist/handsontable.full.js';
      $css = '/' . $library_path . '/handsontable/dist/handsontable.full.css';
      if (!file_exists(Drupal::root() . $js) || !file_exists(Drupal::root() . $css)) {
        return;
      }
      $libraries['handsontable']['js'] = [
        $js => [],
        'js/handsontable.config.js' => [],
      ];
      $libraries['handsontable']['css']['component'] = [
        $css => [],
        'css/handsontable.custom.css' => [],
      ];
    }
  }
}
+11 −0
Original line number Diff line number Diff line
@@ -26,6 +26,17 @@ class Handsontable extends FormElement {
   * @return string if library is missing
   */
  public static function getLibInstructionsIfNeeded($folder) {
    if (\Drupal::hasService('library.libraries_directory_file_finder')) {
      /** @var \Drupal\Core\Asset\LibrariesDirectoryFileFinder $library_file_finder */
      $library_file_finder = \Drupal::service('library.libraries_directory_file_finder');
      if ($library_path = $library_file_finder->find('handsontable')) {
        $js = $library_path . '/handsontable/dist/handsontable.full.js';
        if (file_exists(\Drupal::root() . '/' . $js)) {

          return '';
        }
      }
    }
    if (!file_exists("$folder/js/handsontable.full.js") || !file_exists("$folder/css/handsontable.full.css")) {
      $msg = t('Please download the Handsontable source zip from %s. Then place <ul><li>the file %s in the folder %s and <li>the file %s in the folder %s.</li></ul>');
      return '<b>' . t('Error') . '</b><br><br>' .