function _simplemenu_add_css in SimpleMenu 6
Same name and namespace in other branches
- 7 simplemenu.module \_simplemenu_add_css()
\brief Generate the CSS and add it to the page.
This function generates the dynamic CSS and then insert that to the header of the page.
The function regenerates the CSS only when the settings were modified. Otherwise, it uses the cached version.
The function has a fall back, in case the dynamic CSS cannot be created.
1 call to _simplemenu_add_css()
- simplemenu_init in ./
simplemenu.module - Implementation of hook_init().
File
- ./
simplemenu.module, line 126 - Creates a simplemenu.
Code
function _simplemenu_add_css() {
global $user;
$simplemenu_path = drupal_get_path('module', 'simplemenu');
// set $use_ctools to false if not private downloads
$use_ctools = false;
// test if the file area is private as drupal_add_css fails for generated files in this case
if (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) != FILE_DOWNLOADS_PUBLIC) {
// ctools css api can handle adding generated css with private filesystems
// check if there is a suitable version and set $use_ctools to true if so
if (module_exists('ctools') && module_invoke('ctools', 'api_version', '1.0')) {
ctools_include('css');
$use_ctools = true;
}
else {
// use the static version of the CSS if private files and no ctools
drupal_add_css($simplemenu_path . '/simplemenu.css');
return;
}
}
$css_path = file_create_path('css');
// same path as concatenated Core CSS
if (file_check_directory($css_path, FILE_CREATE_DIRECTORY)) {
$fix = variable_get('simplemenu_fix', 'scroll');
// XXX add a variable simplemenu_update which is set to TRUE whenever
// the settings get modified and false here
$output_filename = variable_get('simplemenu_css_filename', '');
if (!$output_filename || !file_exists($output_filename)) {
$tags = array(
'@MENUBAR_ZINDEX@' => simplemenu_get_zindex('simplemenu_menubar_zindex', 9999),
'@DROPDOWN_ZINDEX@' => simplemenu_get_zindex('simplemenu_dropdown_zindex', 9999),
);
switch ($fix) {
case 'top':
$tags['@FIX@'] = "position: fixed;\n top: 0;";
$tags['@STICKY_TABLE@'] = "table.sticky-header {\n top: 21px !important\n}";
break;
case 'bottom':
$tags['@FIX@'] = "position: fixed;\n bottom: 0;";
$tags['@STICKY_TABLE@'] = '';
break;
default:
// scroll
$tags['@FIX@'] = 'position: relative;';
$tags['@STICKY_TABLE@'] = '';
break;
}
$css = file_get_contents($simplemenu_path . '/simplemenu.css.tpl');
$css = strtr($css, $tags);
$css_md5 = md5($css);
$output_filename = $css_path . '/simplemenu-' . $css_md5 . '.css';
if (!file_exists($output_filename)) {
// new content, create a new file
file_put_contents($output_filename, $css);
}
else {
// this call is rather ugly, but we must make sure that the
// system cache will take the current Simplemenu CSS in account
_drupal_flush_css_js();
}
variable_set('simplemenu_css_filename', $output_filename);
}
if ($use_ctools) {
ctools_css_add_css($output_filename);
}
else {
drupal_add_css($output_filename);
}
}
else {
// in case we cannot create the dynamic CSS
$last_msg = variable_get('simplemenu_css_error', 0);
if ($last_msg != -1 && $last_msg + 3600 > time() || $user->uid == 1) {
// avoid displaying the error on each page... only once per hour.
// (unless you are the admin, in which case you probably want to know!)
variable_set('simplemenu_css_error', time());
drupal_set_message(t('Simplemenu could not create the folder @path in order to save the dynamic CSS data.', array(
'@path' => $css_path,
)), 'warning');
}
// use a default that cannot react to the dynamic changes...
drupal_add_css($simplemenu_path . '/simplemenu.css');
}
}