Drupal entschlacken und nur benötigte CSS Dateien laden
Posted on : 28-01-2010 | By : DKl3in | In : Entwicklung
0
Ich beschäftige mich noch nicht lange mit Drupal, doch was mir gleich als erstes aufgefallen ist, dass jedes Modul und auch Drupal selber, eigene CSS Dateien in den Header hinzufügt, auch wenn man diese gar nicht benötigt, bzw. diese wieder mühselig überschreiben muss.
Also habe ich mich auf die Suche gemacht eine Lösung für dieses Problem zu finden, und habe sie auch gefunden.
Man muss in seiner "template.php" einfach folgende Funktionen hinzufügen: (myTheme durch den eigene Theme-Namen ersetzt)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | function myTheme_preprocess_page(&$variables) { $variables['styles'] = myTheme_get_css(); } function myTheme_get_css($css = NULL) { if($css == null) { $css = drupal_add_CSS(); } // Looping through added CSS files and unsetting files that we don't want to use foreach((array)$css as $media => $types) { foreach ((array)$types as $type => $files) { if ($type == 'module' ) { foreach((array)$files as $file => $preprocess) { if (!theme_get_setting('myTheme_module_css') && !theme_get_setting('myTheme_module_css_'. str_replace(Array('/', '.', ' '), '_', $file))) { unset($css[$media][$type][$file]); } } } } } return drupal_get_css($css); } |
Damit man nun die Möglichkeit einer Auswahl im Administrationsbereich hat, muss noch die Datei "theme-settings.php" mit folgendem Inhalt anlege:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | 0, ); $settings = array_merge($defaults, $saved_settings); /*checkbox to disable all module css*/ $form['bmu_module_css'] = array( '#type' => 'checkbox', '#title' => t('Enabled all module css'), '#default_value' => $settings['bmu_module_css'], ); /*checkboxes for enabling individual module css*/ $form['bmu_module_css_fieldset'] = array( '#type' => 'fieldset', '#title' => t('Enable individual module css'), '#collapsible' => true, '#collapsed' => true, ); /*any functions used in this file need to be defined in theme-settings.php*/ $module_css = bmu_get_module_css(); foreach ($module_css as $key => $file) { $var = 'bmu_module_css_'. str_replace(Array('/', '.', ' '), '_', $file); $form['bmu_module_css_fieldset'][$var] = array( '#type' => 'checkbox', '#title' => t($file), '#default_value' => $settings[$var], ); } return $form; } function bmu_get_module_css() { $module_css = Array(); // Scanning module folders for CSS files and saving index in database $result = db_query("SELECT * FROM {system} WHERE type = 'module' AND status = 1"); while($module = db_fetch_object($result)) { $module_path = pathinfo($module->filename, PATHINFO_DIRNAME); $css_files = file_scan_directory($module_path, '\.css$', array('.', '..', '.svn', 'CVS')); foreach((array)$css_files as $key => $file) { $module_css[] = $file->filename; } } return $module_css; } ?> |
Nun hat man im Administrationsbereich unter:
Administer -> Site building -> Themes
und dort auf "configure" neben seinem Theme klickt, die Möglichkeit alle Stylesheets die man benötigt, per Checkbox hinzuzufügen.
Es ist erstaunlich wie viele Dateien man eigentlich nicht benötigt.
Heute habe ich ein Modul gefunden mit dem man das ganze noch etwas einfacher machen kann:
Ich musste das Modul bzw die Funktion "stylestripper_preprocess_page()" nur ein kleinwenige erweitern, damit ein anderes Modul "Conditional Stylesheets" auch richtig funktioniert. (damit ist es möglich Stylesheets für den InternetExplorer hinzuzufügen)
1 2 3 4 5 6 7 | // Saving the rest of the CSS files in $variables. $variables['styles'] = drupal_get_CSS($CSS); if (!module_exists('conditional_styles')) { $variables['styles'] .= $variables['conditional_styles']; } |

