View source
<?php
use Drupal\Core\Asset\AttachedAssetsInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
define('MINIFYJS_CACHE_CID', 'minifyjs:all_files');
function minifyjs_form_system_performance_settings_alter(array &$form, FormStateInterface $form_state, $form_id) {
if (\Drupal::currentUser()
->hasPermission('administer minifyjs')) {
$files = \Drupal::service('minifyjs')
->loadAllFiles();
$files_minified = 0;
foreach ($files as $file) {
if (!empty($file->minified_uri)) {
$files_minified++;
}
}
$form['bandwidth_optimization']['use_minified_javascript'] = [
'#type' => 'checkbox',
'#title' => t('Use Minified JavaScript files.'),
'#disabled' => $files_minified ? FALSE : TRUE,
'#description' => t('Toggle minified javascript files on or off. There are @minified minified files out of @total total files available. <a href=":href">Click here to manage your javascript files</a>.', [
'@minified' => $files_minified,
'@total' => count($files),
':href' => Url::fromUri('base:/admin/config/development/performance/js')
->toString(),
]),
'#default_value' => \Drupal::config('minifyjs.config')
->get('use_minified'),
];
$form['#submit'][] = 'minifyjs_system_performance_settings_submit';
}
}
function minifyjs_system_performance_settings_submit(array &$form, FormStateInterface $form_state) {
\Drupal::service('config.factory')
->getEditable('minifyjs.config')
->set('use_minified', $form_state
->getValue('use_minified_javascript'))
->save();
}
function minifyjs_js_alter(array &$scripts, AttachedAssetsInterface $assets) {
$do_replace = FALSE;
if (\Drupal::config('minifyjs.config')
->get('use_minified')) {
if (\Drupal::config('minifyjs.config')
->get('disable_admin')) {
if (!\Drupal::service('router.admin_context')
->isAdminRoute()) {
$do_replace = TRUE;
}
}
else {
$do_replace = TRUE;
}
}
if ($do_replace) {
$files = \Drupal::service('minifyjs')
->loadAllFiles();
$minified_files = [];
foreach ($files as $file) {
if (!empty($file->minified_uri)) {
$minified_files[$file->uri] = $file;
}
}
foreach ($scripts as $path => $file) {
if (isset($minified_files[$path])) {
$scripts[$path]['data'] = $minified_files[$path]->minified_uri;
minifyjs_update_scripts($scripts, $path, $minified_files[$path]->minified_uri);
}
}
}
}
function minifyjs_update_scripts(array &$scripts, $existing, $new) {
$keys = array_keys($scripts);
$index = array_search($existing, $keys, TRUE);
if ($index !== FALSE) {
$keys[$index] = $new;
$scripts = array_combine($keys, $scripts);
}
}
function minifyjs_batch_minify_file_operation($fid, array &$context) {
$files = \Drupal::service('minifyjs')
->loadAllFiles();
$file = $files[$fid];
$context['message'] = t('Minifying file - %file', [
'%file' => $file->uri,
]);
$result = \Drupal::service('minifyjs')
->minifyFile($fid);
if ($result === TRUE) {
isset($context['results']['success']) ? $context['results']['success']++ : ($context['results']['success'] = 1);
$context['results']['operation'] = 'minify';
}
else {
$context['results']['errors'][] = $result;
}
}
function minifyjs_batch_remove_minified_file_operation($fid, array &$context) {
$files = \Drupal::service('minifyjs')
->loadAllFiles();
$file = $files[$fid];
$context['message'] = t('Restoring un-minified file - %file', [
'%file' => $file->uri,
]);
$result = \Drupal::service('minifyjs')
->removeMinifiedFile($fid);
if ($result === TRUE) {
isset($context['results']['success']) ? $context['results']['success']++ : ($context['results']['success'] = 1);
$context['results']['operation'] = 'restore';
}
else {
$context['results']['errors'][] = $result;
}
}
function minifyjs_batch_finished($success, array $results, array $operations) {
if (isset($results['success'])) {
if ($results['operation'] == 'minify') {
\Drupal::messenger()
->addMessage(t('%count files were minified.', [
'%count' => $results['success'],
]));
}
if ($results['operation'] == 'restore') {
\Drupal::messenger()
->addMessage(t('%count files were restored.', [
'%count' => $results['success'],
]));
}
}
if (isset($results['errors'])) {
if (count($results['errors'])) {
foreach ($results['errors'] as $error) {
\Drupal::messenger()
->addMessage($error, 'error');
}
}
}
\Drupal::cache()
->delete(MINIFYJS_CACHE_CID);
}