Skip to content
Snippets Groups Projects
Commit 03489642 authored by Oleksandr Kuzava's avatar Oleksandr Kuzava Committed by nginex
Browse files

Issue #2220547 by nginex: Drupal 8 version

parent 42ff4ee5
No related branches found
No related tags found
No related merge requests found
Conditional Styles 8.x-1.x, 2016-01-30
------------------------------------------
- #2220547 by nginex: Added Drupal 8 support
Conditional Styles 7.x-2.2, 2013-07-12
------------------------------------------
- #580440 by quicksketch: Add support for Downlevel-revealed Conditional Comments
......
......@@ -67,9 +67,12 @@ horribly-difficult-to-remember function calls to your theme's template.php file:
Blech. Who wants to do that?
This module allows you to add "conditional-stylesheets" in a much simpler
manner, by adding lines to your theme's.info file. The syntax for that is:
manner, by adding lines to your theme's.info.yml file. The syntax for that is:
stylesheets-conditional[EXPRESSION][MEDIA][] = stylesheet.css
conditional-stylesheets:
EXPRESSION:
MEDIA:
css/style.css
where
EXPRESSION can be any of the "downlevel-hidden" expressions specified in:
......@@ -77,17 +80,24 @@ manner, by adding lines to your theme's.info file. The syntax for that is:
MEDIA can be any of the normal CSS media keywords.
For example, to add a stylesheet that only targets IE 6 and below, use:
stylesheets-conditional[lt IE 7][all][] = ie6-and-below.css
conditional-stylesheets:
lt IE 7:
all:
css/ie6-and-below.css
To add a print stylesheet for IE9 only, use:
stylesheets-conditional[IE 9][print][] = ie9.css
conditional-stylesheets:
IE 9:
print:
css/ie9.css
And to add a print stylesheet for all version of IE, use:
stylesheets-conditional[IE][print][] = ie.css
conditional-stylesheets:
IE:
print:
css/ie9.css
*** IMPORTANT ***
Drupal 7 stores a cache of the data in .info files. If you modify any lines in
your theme's .info file, you MUST refresh Drupal 7's cache by simply visiting
the Appearance page at admin/appearance.
Drupal 8 stores a cache of the data in .info.yml files. If you modify any lines in
your theme's .info.yml file, you MUST rebuild Drupal 8's cache.
name: Conditional Stylesheets
type: module
description: 'Allows themes to add conditional stylesheets.'
package: Other
version: VERSION
core: 8.x
package: Other
......@@ -5,31 +5,33 @@
* Adds conditional CSS from the .info file.
*/
use \Drupal\Component\Utility\Unicode;
/**
* Implements hook_page_build().
* Implements hook_css_alter().
*/
function conditional_styles_page_build(&$page) {
function conditional_styles_css_alter(&$css, \Drupal\Core\Asset\AttachedAssetsInterface $assets) {
$theme_handler = \Drupal::service('theme_handler');
$themes = $theme_handler->listInfo();
foreach ($themes as $theme) {
$theme_path = drupal_get_path($theme->type, $theme->name);
if (isset($theme->info['stylesheets-conditional']) && $theme->status) {
foreach ($theme->info['stylesheets-conditional'] as $condition => $medias) {
foreach ($medias as $media => $stylesheets) {
foreach ($stylesheets as $stylesheet) {
$full_path = $theme_path . '/' . $stylesheet;
$page['#attached']['css'][$full_path] = array(
'group' => CSS_THEME,
'weight' => 999,
'media' => $media,
'every_page' => TRUE,
'browsers' => array(
'IE' => $condition,
'!IE' => strpos($condition, '!IE') === FALSE ? FALSE : TRUE,
),
);
}
}
$current_theme = $theme_handler->getDefault();
$path = drupal_get_path('theme', $current_theme);
$info = $theme_handler->getTheme($current_theme)->info;
if (!empty($info['conditional-stylesheets'])) {
foreach ($info['conditional-stylesheets'] as $version => $media) {
foreach ($media as $key => $file_path) {
$full_path = "{$path}/{$file_path}";
$css[$full_path] = [
'group' => CSS_THEME,
'weight' => 999,
'type' => 'file',
'preprocess' => TRUE,
'data' => $full_path,
'media' => $key,
'every_page' => TRUE,
'browsers' => [
'IE' => $version,
'!IE' => (Unicode::strpos($version, '!IE') !== FALSE),
],
];
}
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment