3

In a current Drupal 7 install i realize that all our CSS files are being declared in html.tpl.php using a variable $styles instead of through the templates .info file. I tried using Xdebug but all i managed to get is the info the variable holds. The same is also being done to javascript files which should really be at the footer instead of the header.

I have checked my themes template.php but i have not found a declaration of it anywhere.

2

$styles is set in the $variables array during template_process_html. It is a result of calling drupal_get_css, and you can add to, remove, or change the order of this array by implementing drupal_add_css, adding a css file to your theme info file, or using hook_css_alter. The reason you do not see it in your theme's template.php is because it is a higher level function that Drupal does, the page is assembled using a majority of the application, core and contrib modules informing or responding to requests.

https://api.drupal.org/api/drupal/includes%21common.inc/function/drupal_get_css/7.x

As you can see, drupal_get_css allows for altering, which is where you would implement hook_css_alter to adjust anything in the array of CSS files before they are added to the page.

See: hook_css_alter

The same applies for the $scripts variable. You can add javascript with drupal_add_js, library hooks, or by adding it to your theme info file.

For example, if you want to change every JS file in drupal to load in the footer, instead of the head, you can do this with hook_js_alter:

/**
 * Implements hook_js_alter().
 * @param $items
 */
function mytheme_js_alter(&$items) {
  foreach ($items as $key => $script) {
    $items[$key]['scope'] = 'footer';
  }
}

Lets say on certain node types, you also want to remove scripts from loading to the page because they are not necessary for the page to function:

/**
 * Implements hook_js_alter().
 * @param $items
 */
function mytheme_js_alter(&$items) {
  foreach ($items as $key => $script) {
    $items[$key]['scope'] = 'footer';
  }

  if ($node = menu_get_object('node')) {
    if ($node->type == 'campaign') {
      // unset all the JS we won't need to reduce page weight
      unset($items['sites/all/themes/mytheme/js/jquery.waypoints.min.js']);
      unset($items['sites/all/themes/mytheme/js/jquery.fitvids.js']);
      unset($items['sites/all/themes/mytheme/js/isotope.min.js']);
      unset($items['sites/all/themes/mytheme/js/imagesloaded.min.js']);
      unset($items['sites/all/libraries/ckeditor/plugins/codesnippet/lib/highlight/highlight.pack.js']);
      unset($items['sites/all/modules/contrib/wysiwyg_codesnippet/wysiwyg_codesnippet.js']);
      unset($items['sites/all/modules/contrib/addthis/addthis.js']);
      unset($items['http://s7.addthis.com/js/300/addthis_widget.js']);
    }

    // we only need addthis to load on an article
    if ($node->type != 'article') {
      unset($items['sites/all/modules/contrib/addthis/addthis.js']);
      unset($items['http://s7.addthis.com/js/300/addthis_widget.js']);
    }
  }
}

See hook_js_alter

| improve this answer | |
  • Will this affect js that was also added through theme.info or only js being added by drupal_add_js() ? – blu Sep 23 '16 at 16:40
  • It will also affect that. – Kevin Sep 23 '16 at 16:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.