Skip to content

Instantly share code, notes, and snippets.

@hannesl
Created October 15, 2012 13:01
Show Gist options
  • Select an option

  • Save hannesl/3892343 to your computer and use it in GitHub Desktop.

Select an option

Save hannesl/3892343 to your computer and use it in GitHub Desktop.
Override a stylesheet added by a Drupal 7 theme
/**
* Implements hook_element_info_alter().
*/
function mymodule_element_info_alter(&$type) {
// Rather than implementing hook_css_alter(), we'll add a custom pre render
// function for the styles elemement. This is to allow us to override
// seven_css_alter(), which always runs after any module's implementation.
if (isset($type['styles']['#pre_render'])) {
array_unshift($type['styles']['#pre_render'], 'mymodule_pre_render_styles');
}
}
/**
* Pre render the styles element.
*/
function mymodule_pre_render_styles($elements) {
$css = &$elements['#items'];
// Installs the jquery.ui themeroller theme to the theme.
if (isset($css['misc/ui/jquery.ui.theme.css'])) {
$css['misc/ui/jquery.ui.theme.css']['data'] = drupal_get_path('module', 'mymodule') . '/css/mymodule-jquery-ui-theme.css';
}
if (isset($css['misc/ui/jquery.ui.dialog.css'])) {
unset($css['misc/ui/jquery.ui.dialog.css']);
}
if (isset($css['misc/ui/jquery.ui.tabs.css'])) {
unset($css['misc/ui/jquery.ui.tabs.css']);
}
return $elements;
}
@hannesl
Copy link
Author

hannesl commented Oct 15, 2012

A Drupal 7 theme can implement hook_css_alter() to insert custom css files in place of those added by modules. Seven in core does this to add its own jQuery UI styles for example. But sometimes you just need to override the override...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment