Same name and namespace in other branches
  1. 9 core/modules/ckeditor5/ckeditor5.module \ckeditor5_library_info_alter()

Implements hook_library_info_alter().

File

core/modules/ckeditor5/ckeditor5.module, line 412

Code

function ckeditor5_library_info_alter(&$libraries, $extension) {
  if ($extension === 'filter') {
    $libraries['drupal.filter.admin']['dependencies'][] = 'ckeditor5/internal.drupal.ckeditor5.filter.admin';
  }
  $moduleHandler = \Drupal::moduleHandler();
  if ($extension === 'ckeditor5') {

    // Add paths to stylesheets specified by a theme's ckeditor5-stylesheets
    // config property.
    $css = _ckeditor5_theme_css();
    $libraries['internal.drupal.ckeditor5.stylesheets'] = [
      'css' => [
        'theme' => array_fill_keys(array_values($css), []),
      ],
    ];
  }
  if ($extension === 'core') {

    // CSS rule to resolve the conflict with z-index between CKEditor 5 and jQuery UI.
    $libraries['drupal.dialog']['css']['component']['modules/ckeditor5/css/ckeditor5.dialog.fix.css'] = [];

    // Fix the CKEditor 5 focus management in dialogs. Modify the library
    // declaration to ensure this file is always loaded after
    // drupal.dialog.jquery-ui.js.
    $libraries['drupal.dialog']['js']['modules/ckeditor5/js/ckeditor5.dialog.fix.js'] = [];
  }

  // Only add translation processing if the locale module is enabled.
  if (!$moduleHandler
    ->moduleExists('locale')) {
    return;
  }

  // All possibles CKEditor 5 languages that can be used by Drupal.
  $ckeditor_langcodes = array_values(_ckeditor5_get_langcode_mapping());
  if ($extension === 'core') {

    // Generate libraries for each of the CKEditor 5 translation files so that
    // the correct translation file can be attached depending on the current
    // language. This makes sure that caching caches the appropriate language.
    // Only create libraries for languages that have a mapping to Drupal.
    foreach ($ckeditor_langcodes as $langcode) {
      $libraries['ckeditor5.translations.' . $langcode] = [
        'remote' => $libraries['ckeditor5']['remote'],
        'version' => $libraries['ckeditor5']['version'],
        'license' => $libraries['ckeditor5']['license'],
        'dependencies' => [
          'core/ckeditor5',
          'core/ckeditor5.translations',
        ],
      ];
    }
  }

  // Copied from \Drupal\Core\Asset\LibraryDiscoveryParser::buildByExtension().
  if ($extension === 'core') {
    $path = 'core';
  }
  else {
    if ($moduleHandler
      ->moduleExists($extension)) {
      $extension_type = 'module';
    }
    else {
      $extension_type = 'theme';
    }
    $path = \Drupal::getContainer()
      ->get('extension.path.resolver')
      ->getPath($extension_type, $extension);
  }
  foreach ($libraries as &$library) {

    // The way to know if a library has a translation is to depend on the
    // special "core/ckeditor5.translations" library.
    if (empty($library['js']) || empty($library['dependencies']) || !in_array('core/ckeditor5.translations', $library['dependencies'])) {
      continue;
    }
    foreach ($library['js'] as $file => $options) {

      // Only look for translations on libraries defined with a relative path.
      if (!empty($options['type']) && $options['type'] === 'external') {
        continue;
      }

      // Path relative to the current extension folder.
      $dirname = dirname($file);

      // Path of the folder in the filesystem relative to the Drupal root.
      $dir = $path . '/' . $dirname;

      // Exclude protocol-free URI.
      if (str_starts_with($dirname, '//')) {
        continue;
      }

      // CKEditor 5 plugins are most likely added through composer and
      // installed in the module exposing it. Suppose the file path is
      // relative to the module and not in the /libraries/ folder.
      // Collect translations based on filename, and add all existing
      // translations files to the plugin library. Unnecessary translations
      // will be filtered in ckeditor5_js_alter() hook.
      $files = scandir("{$dir}/translations");
      foreach ($files as $file) {
        if (str_ends_with($file, '.js')) {
          $langcode = basename($file, '.js');

          // Only add languages that Drupal can understands.
          if (in_array($langcode, $ckeditor_langcodes)) {
            $library['js']["{$dirname}/translations/{$langcode}.js"] = [
              // Used in ckeditor5_js_alter() to filter unwanted translations.
              'ckeditor5_langcode' => $langcode,
              'minified' => TRUE,
              'preprocess' => TRUE,
            ];
          }
        }
      }
    }
  }
}