Skip to content

Instantly share code, notes, and snippets.

@anthonysimone
Last active August 27, 2015 17:08
Show Gist options
  • Save anthonysimone/398b29cc824dd66794e7 to your computer and use it in GitHub Desktop.
Save anthonysimone/398b29cc824dd66794e7 to your computer and use it in GitHub Desktop.
Snippet to add some js or css to the Drupal admin theme via a module.
<?php
// Adds to EVERY admin page
function custom_admin_page_build(&$page) {
// Test if on an admin page
if (path_is_admin(current_path())) {
// Get paths
$path = drupal_get_path('module', 'custom_admin');
$theme_path = drupal_get_path('theme', 'themekit');
// Add js and css to customize the admin theme
$page['page_bottom']['custom_admin'] = array(
'#attached' => array(
'js' => array(
// Select2
$path . '/select2/dist/js/select2.min.js' => array('every_page' => true),
// Select2 init
$path . '/js/admin-multi-selects.js' => array('every_page' => true),
),
'css' => array(
// Select2 css
$path . '/select2/dist/css/select2.min.css' => array('every_page' => true),
// Custom css
$path . '/css/select2-custom.css' => array('every_page' => true),
),
)
);
}
}
// Add only when a specific element is present (in this case, selects)
function custom_admin_preprocess_form_element(&$variables) {
$is_admin = &drupal_static(__FUNCTION__ . 'is_admin');
$is_admin = (isset($is_admin)) ? $is_admin : path_is_admin(current_path());
if ($is_admin && isset($variables['element'])) {
$element =& $variables['element'];
if ($element['#type'] == 'select') {
// Get path to module
$path = drupal_get_path('module', 'custom_admin');
// Default options.
$js_options = array('group' => JS_THEME);
$css_options = array('group' => CSS_THEME);
// Add js and css to customize the admin theme
drupal_add_js($path . '/select2/dist/js/select2.min.js', $js_options);
drupal_add_js($path . '/js/admin-multi-selects.js', $js_options);
drupal_add_css($path . '/select2/dist/css/select2.min.css', $css_options);
drupal_add_css($path . '/css/select2-custom.css', $css_options);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment