function Ckeditor5Hooks::jsAlter

Same name and namespace in other branches
  1. 11.x core/modules/ckeditor5/src/Hook/Ckeditor5Hooks.php \Drupal\ckeditor5\Hook\Ckeditor5Hooks::jsAlter()

Implements hook_js_alter().

Attributes

#[Hook('js_alter')]

File

core/modules/ckeditor5/src/Hook/Ckeditor5Hooks.php, line 344

Class

Ckeditor5Hooks
Hook implementations for ckeditor5.

Namespace

Drupal\ckeditor5\Hook

Code

public function jsAlter(&$javascript, AttachedAssetsInterface $assets, LanguageInterface $language) : void {
  $placeholder_file = 'core/assets/vendor/ckeditor5/translation.js';
  // When the locale module isn't installed there are no translations.
  if (!$this->moduleHandler
    ->moduleExists('locale')) {
    unset($javascript[$placeholder_file]);
    return;
  }
  $translations_library = 'core/ckeditor5.translations';
  if (in_array($translations_library, $this->libraryDependencyResolver
    ->getLibrariesWithDependencies($assets->getLibraries()), TRUE) || in_array($translations_library, $this->libraryDependencyResolver
    ->getLibrariesWithDependencies($assets->getAlreadyLoadedLibraries()), TRUE)) {
    // This file is used to get a weight that will make it possible to
    // aggregate all translation files in a single aggregate.
    $ckeditor_dll_file = 'core/assets/vendor/ckeditor5/ckeditor5-dll/ckeditor5-dll.js';
    // Use the placeholder file weight to set all the translations files
    // weights so they can be aggregated together as expected. Account for
    // requests where the library is not loaded such as when during an AJAX
    // request when it was already loaded via the main request. In these cases
    // it is unlikely that multiple JavaScript aggregates will be created
    // anyway since AJAX requests generally result in very few libraries being
    // loaded.
    $default_weight = $javascript[$placeholder_file]['weight'] ?? 0;
    if (isset($javascript[$ckeditor_dll_file])) {
      $default_weight = $javascript[$ckeditor_dll_file]['weight'];
    }
    $ckeditor5_language = $this->languageMapper
      ->getMapping($language->getId());
    // Remove all CKEditor 5 translations files that are not in the current
    // language.
    foreach ($javascript as $index => &$item) {
      // This is not a CKEditor 5 translation file, skip it.
      if (empty($item['ckeditor5_langcode'])) {
        continue;
      }
      // This file is the correct translation for this page.
      if ($item['ckeditor5_langcode'] === $ckeditor5_language) {
        // Set the weight for the translation file to be able to have the
        // translation files aggregated.
        $item['weight'] = $default_weight;
      }
      else {
        // Remove files that don't match the language requested.
        unset($javascript[$index]);
      }
    }
  }
  // The placeholder file is not a real file, remove it from the list.
  unset($javascript[$placeholder_file]);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.