function LocaleHooks::jsAlter

Implements hook_js_alter().

File

core/modules/locale/src/Hook/LocaleHooks.php, line 220

Class

LocaleHooks
Hook implementations for locale.

Namespace

Drupal\locale\Hook

Code

public function jsAlter(&$javascript, AttachedAssetsInterface $assets, LanguageInterface $language) {
    $files = [];
    foreach ($javascript as $item) {
        if (isset($item['type']) && $item['type'] == 'file') {
            // Ignore the JS translation placeholder file.
            if ($item['data'] === 'core/modules/locale/locale.translation.js') {
                continue;
            }
            $files[] = $item['data'];
        }
    }
    // Replace the placeholder file with the actual JS translation file.
    $placeholder_file = 'core/modules/locale/locale.translation.js';
    if (isset($javascript[$placeholder_file])) {
        if ($translation_file = locale_js_translate($files, $language)) {
            $js_translation_asset =& $javascript[$placeholder_file];
            $js_translation_asset['data'] = $translation_file;
            // @todo Remove this when https://www.drupal.org/node/1945262 lands.
            // Decrease the weight so that the translation file is loaded first.
            $js_translation_asset['weight'] = $javascript['core/misc/drupal.js']['weight'] - 0.001;
        }
        else {
            // If no translation file exists, then remove the placeholder JS asset.
            unset($javascript[$placeholder_file]);
        }
    }
}

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