<\/script>)!"; // Regular expression to extract js paths and filenames from $scripts variable preg_match_all($pattern, $scripts, $matches); // Create an array $matches with pieces of $pattern found in $scripts $scripts_js_files = $matches[2]; // We only need paths and filenames stored in $matches[2] $filename = md5(implode('', $scripts_js_files)) . '.js'; // Generate a unique filename for any set of js files $jspath = file_create_path('js'); // similar approach to drupal_build_css_cache (common.inc) file_check_directory($jspath, FILE_CREATE_DIRECTORY); $jsfile = $jspath .'/'. $filename; if (!file_exists($jsfile) && variable_get('aggregate_js', 0)) { // Check if aggregated file exists, if not create it $contents = ''; // Start aggegating the files foreach ($scripts_js_files as $scripts_js_file) { $contents .= file_get_contents(preg_replace('/^\//', '', $scripts_js_file)); // Gead the js file and add it to $contents $contents .= "\n\n;\n\n"; // Add a semicolon to the end of each file to make sure every line has an end } file_save_data($contents, $jsfile, FILE_EXISTS_REPLACE); // Create the js file. } $scripts = preg_replace($pattern, "",$scripts); // remove aggregated js files from $scripts $newscripts = ''; // Create a link to aggregated file and append cleaned $scripts $newscripts .= $scripts; return $newscripts; } /** * Delete all cached JS files. */ function javascript_aggregator_clear_js_cache() { $success = file_scan_directory(file_create_path('js'), '.*', array('.', '..', 'CVS'), 'file_delete', TRUE); if ($success) { drupal_set_message(t('Javascript cache cleared.'), $type = 'status'); } else { drupal_set_message(t('Javascript cache could not be cleared.'), $type = 'error'); } drupal_goto('admin/settings/performance'); } function javascript_aggregator_form_alter($form_id, &$form) { if ($form_id == 'system_performance_settings') { $directory = file_directory_path(); $is_writable = is_dir($directory) && is_writable($directory) && (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PUBLIC); $form['javascript_aggregation'] = array( '#type' => 'fieldset', '#title' => t('Javascript Aggregation'), '#description' => t('

The Javascript Aggregator Module aggregates javascript files into a single cached file. This will help to reduce the number of requests made to your webserver on every pageload, reducing server load and average page loading time.

To enable this feature, make sure you have set up your files directory correctly and your download method is not set to private.

Click here to clear the javascript cache.

NOTICE: If you clear the cache while Javascript Aggregation is enabled, your files/js directory will NOT be empty because a new file is instantly created.

'), '#weight' => 0 ); $form['javascript_aggregation']['aggregate_js'] = array( '#type' => 'radios', '#title' => t('Aggregate JavaScript files'), '#default_value' => variable_get('aggregate_js', 0), '#disabled' => !$is_writable, '#options' => array(t('Disabled'), t('Enabled')), '#description' => t("This option can interfere with module development. It is recommended to only turn this on when your site is complete."), ); } } function javascript_aggregator_menu($may_cache) { $items = array(); if (!$may_cache) { global $user; if ($user->uid == 1) { // TODO: make this a setting or something $access = TRUE; } else { $access = FALSE; } $items[] = array( 'path' => 'clearjscache', 'callback' => 'javascript_aggregator_clear_js_cache', 'access' => $access, 'type' => MENU_CALLBACK ); } return $items; }