'Autoload JS help',
'description' => 'Provide some insight to the user about how automatic loading for JS files is done.',
'page callback' => 'autoload_js_page',
'access arguments' => array('administer themes'),
);
return $items;
}
/**
* Implements hook_help().
*/
function autoload_js_help($section = 'admin/help#autoload_js', $arg = NULL) {
}
/**
* Callback function for admin/config/system/load-js
* Provide some insight to the user about how suggestion for JS files are made
*/
function autoload_js_page() {
$suggestions = autoload_js_suggestions();
$render = array();
$render['top'] = array (
'#markup' => '
Autload JS provides a set of suggestions for javascript files that will be automatically loaded based on a dynamic conditions.
Available conditions:
- "all" - For all pages
- "front" - For front page
- "pagetitle--[title]" - A page with title "My blog" would have the suggestion "pagetitle--my-blog"
- "pagepath--[path]" - A page with the URL of "path/to/blog" would have the suggestion "pagepath--path-to-blog". Aliased and non-aliased version of the path are both available.
- "pagepath--arg0--[pathcomponent]" - A suggestion based on the first component of the url path.
A page with the URL of "/blog/jan/1" or a page with the URL of "/blog/feb/1" would have the suggestion "pagepath--arg0--blog".
- "nodetype--[type]" - When viewing a node page of type "page", the suggestion would be "nodetype--page"
- "nodeid--[id]" - Viewing the page of node 27 would get "nodeid--27"
- "nodetitle--[title]" - A node page with title "This is my node" would have the suggestion "nodetitle--this-is-my-node"
How suggestions work:
Suggestions work in two ways:
1. The module will attempt to add a matching js file in your theme\'s js sub-folder. For example, for the suggestion "nodetype--blog", "path/to/theme/js/nodetype--blog.js" will be loaded
(if it exists).
2. If a folder exists in your theme\'s js sub-folder with a name that matches the suggestion, ALL the files in that folder will be loaded. For example:
"path/to/theme/js/nodetype--blog/file1.js"
"path/to/theme/js/nodetype--blog/file2.js"
"path/to/theme/js/nodetype--blog/anyotherfiles.js"
etc...
'
);
/*foreach ($suggestions as $sug) {
$render[] = array (
'#type' => 'container',
'#attributes' => array(),
'contents' => array('#markup' => $sug),
);
}*/
return $render;
}
function autoload_js_preprocess_html(&$vars) {
// path_to_theme() doesn't necessarily return the path of the active theme ( http://drupal.org/node/1022942 )
global $theme_key;
$path_to_theme = drupal_get_path('theme', $theme_key);
$path_to_js = $path_to_theme . '/js';
if(!is_dir($path_to_js)) {
// Don't waste anymore time if our primary directory does not exist
return;
}
$suggestions = autoload_js_suggestions($vars);
// Go through all of our suggestions and try to load them
foreach ($suggestions as $suggestion) {
// Load the file with the same name
$filename = "$path_to_js/$suggestion.js";
if(file_exists($filename)) {
drupal_add_js($filename);
}
// Or, if there is a sub-folder with that name, load all of the JS files inside of it
if(is_dir("$path_to_js/$suggestion")) {
$dir = scandir("$path_to_js/$suggestion");
foreach ($dir as $file) {
// Complete path to the file
$filename = "$path_to_js/$suggestion/$file";
if(is_dir($filename)) {
// Skip this file if it is a folder - we're not handling any recursive files
continue;
}
// Make sure the filename ends in .js before attempting to load it - miscellaneous files like .txt files,
// system files like .DS_Store, etc may also be in a folder
if(pathinfo($filename, PATHINFO_EXTENSION) != 'js') {
continue;
}
// We have a .js file, load it!
drupal_add_js($filename);
}
}
}
}
/**
* Generate all the suggestion for javascript files to laod
*/
function autoload_js_suggestions(&$vars = null) {
$suggestions = array();
// Add a suggestion for all pages
$suggestions[] = 'all';
// Add a suggestion for the front page
if(drupal_is_front_page()) {
$suggestions[] = 'front';
}
// If we are viewing a node page
if ($node = menu_get_object()) {
// html template based on page node type
$suggestions[] = 'nodetype--'.$node->type;
// ..based on page node id
$suggestions[] = 'nodeid--'.$node->nid;
// ..based on page node title
$safe_title = autoload_js_safename($node->title);
$suggestions[] = 'nodetitle--'.$safe_title;
}
// ..based on page title
$safe_title = autoload_js_safename(drupal_get_title());
$suggestions[] = 'pagetitle--'.$safe_title;
// ..based on page path
$safe_path = autoload_js_url_to_file($_GET['q']);
$safe_path_alias = autoload_js_url_to_file($_GET['q'], true);
$suggestions[] = 'pagepath--'.$safe_path;
$suggestions[] = 'pagepath--'.$safe_path_alias;
// ..based on pages first path arg (arg(0))
$safe_path_arg0 = autoload_js_url_to_file($_GET['q'], false, 0);
$safe_path_arg0_alias = autoload_js_url_to_file($_GET['q'], true, 0);
$suggestions[] = 'pagepath--arg0--'.$safe_path_arg0;
$suggestions[] = 'pagepath--arg0--'.$safe_path_arg0_alias;
return $suggestions;
}
/**
* Convert a relative URL path to a filename safe version
*/
function autoload_js_url_to_file($path, $use_alias = false, $arg = null) {
if($use_alias) {
$path = drupal_get_path_alias($path);
}
else {
$path = drupal_get_normal_path($path);
}
if($arg !== null) {
// Return only a portion of the path
// Replace slashes with dashes and strip unsafe characters
return drupal_html_class(str_ireplace('/','-',arg($arg,$path)));
}
// Replace slashes with dashes and strip unsafe characters
return drupal_html_class(str_ireplace('/','-',$path));
}
/**
* Convert a string to a safe filename
*/
function autoload_js_safename($string) {
// Replace slashes with dashes and strip unsafe characters
return drupal_html_class(str_ireplace('/','-',$string));
}