Ben Marshall: Load JS & CSS Conditionally in Drupal Support Service 7

Published on February 6, 2019

Annertech: Annertech: Web Agency of the Year

Annertech: Web Agency of the Year My fingers are trembling typing this. I can’t believe it. This morning everyone in Annertech land is thinking “did that really just happen?” It appears it did, we are the web agency of the year! Last night, to top off the other three awards we won – best arts […]

Flickr: Registration Desk – Tuesday – DrupalCon Dublin 2016

comprock posted a photo: The Drupal community is one of the largest open source communities in the world. We’re developers, designers, strategists, coordinators, editors, translators, and more. Each year, we meet at DrupalCamps, meetups, and other events in more than 200 countries. But once a year, our community comes together in a European city for […]

Flickr: Sprint Lounge – Tuesday – DrupalCon Dublin 2016

comprock posted a photo: The Drupal community is one of the largest open source communities in the world. We’re developers, designers, strategists, coordinators, editors, translators, and more. Each year, we meet at DrupalCamps, meetups, and other events in more than 200 countries. But once a year, our community comes together in a European city for […]

Flickr: Tuesday – DrupalCon Dublin 2016

comprock posted a photo: The Drupal community is one of the largest open source communities in the world. We’re developers, designers, strategists, coordinators, editors, translators, and more. Each year, we meet at DrupalCamps, meetups, and other events in more than 200 countries. But once a year, our community comes together in a European city for […]

Flickr: David & Paul – Scout masters – Wednesday – DrupalCon Dublin 2016

comprock posted a photo: The Drupal community is one of the largest open source communities in the world. We’re developers, designers, strategists, coordinators, editors, translators, and more. Each year, we meet at DrupalCamps, meetups, and other events in more than 200 countries. But once a year, our community comes together in a European city for […]

Flickr: Jam interview – Wednesday – DrupalCon Dublin 2016

comprock posted a photo: The Drupal community is one of the largest open source communities in the world. We’re developers, designers, strategists, coordinators, editors, translators, and more. Each year, we meet at DrupalCamps, meetups, and other events in more than 200 countries. But once a year, our community comes together in a European city for […]

Flickr: Sprinters wanted – Wednesday – DrupalCon Dublin 2016

comprock posted a photo: The Drupal community is one of the largest open source communities in the world. We’re developers, designers, strategists, coordinators, editors, translators, and more. Each year, we meet at DrupalCamps, meetups, and other events in more than 200 countries. But once a year, our community comes together in a European city for […]

Flickr: Sprint Lounge – Wednesday – DrupalCon Dublin 2016

comprock posted a photo: The Drupal community is one of the largest open source communities in the world. We’re developers, designers, strategists, coordinators, editors, translators, and more. Each year, we meet at DrupalCamps, meetups, and other events in more than 200 countries. But once a year, our community comes together in a European city for […]

Leopathu: Dynamic Block Weight in Drupal 8

In such a time, i want to place blocks in sidebar region with the dynamic weight. It means the blocks should render in different position for each page request. I have searched and tried lots of method but unfortunately i can’t find proper method to do that. So i have decided to do that with […]

Agiledrop.com Blog: AGILEDROP: Drupal‘s path from 4.0 to 8.0

Last time we guided you through early beginnings of Drupal. We explained how all started and how first versions of Drupal were made. This time we will look how this open-source content-management framework evolved from its fourth to its latest, eight version. Drupal 4.0 Drupal’s fourth version was released on 15. 6. 2002. It became […]

Flickr: Wednesday – DrupalCon Dublin 2016

comprock posted a photo: The Drupal community is one of the largest open source communities in the world. We’re developers, designers, strategists, coordinators, editors, translators, and more. Each year, we meet at DrupalCamps, meetups, and other events in more than 200 countries. But once a year, our community comes together in a European city for […]

OStatic: Web Publishing and Development: Free Tools Abound

Are you involved in DevOps and web development, or are you aiming to be? If so, you’re probably very aware of many of the tools from the open standards and open source arenas that can make your work easier. Still, these are always spreading out at a fast clip and there are some applications and […]

This post was originally published on May 22, 2013 and last updated March 8, 2018 thanks to some helpful input by Steve Elkins.
Drupal Support Service 7 is a haus at combining CSS & JS files. This can help boost page performance & optimization easily, but if not used right, can do the complete opposite. In this post, we’ll go over how to load JS & CSS files based on conditionals like URL, module, node, views and more.
Before we dive in, get somewhat familiar with the drupal_add_js and drupal_add_css functions. We’ll use these to load the actual JS and CSS files.

hook_init – runs on every page
/**
* Implements hook_init()
*
* @link https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_init/7.x
*/
function HOOK_init() {

// Using the equivalent of Apache’s $_SERVER[‘REQUEST_URI’] variable to load based on URL
// @link https://api.drupal.org/api/drupal/includes!bootstrap.inc/function/request_uri/7
if (request_url() === ‘your-url-path’) {
drupal_add_js( /* parameters */ );
drupal_add_css( /* parameters */ );
}
}
Using hook_init is one of the simplest methods to load specific JS and CSS files (don’t forget to replace HOOK with the theme or module machine name).
Be careful, this method get’s ran on every page, so it’s best to use this method only when you actually need to check every page for your conditional. A good example, loading module CSS and JS files. A bad example, loading node-specific CSS and JS files. We’ll go over that next.
There’s also a similar preprocess function, template_preprocess_page you could use, but it too get’s ran on every page and is essentially the same as hook_init.
template_preprocess_node – runs on node pages
/**
* Implements template_preprocess_node()
*
* @link https://api.drupal.org/api/drupal/modules%21node%21node.module/function/template_preprocess_node/7.x
*/
function TEMPLATE_preprocess_node(&$vars) {
// Add JS & CSS by node type
if( $vars[‘type’] == ‘your-node-type’) {
drupal_add_js( /* parameters */ );
drupal_add_css( /* parameters */ );
}

// Add JS & CSS to the front page
if ($vars[‘is_front’]) {
drupal_add_js( /* parameters */ );
drupal_add_css( /* parameters */ );
}

// Given an internal Drupal Support Service path, load based on node alias.
if (drupal_get_path_alias(“node/{$vars[‘#node’]->nid}”) == ‘your-node-id’) {
drupal_add_js( /* parameters */ );
drupal_add_css( /* parameters */ );
}
}
Using template_preprocess_node is perfect when loading JS and CSS files based on nodes (don’t forget to replace TEMPLATE with the theme machine name). Since it only get’s run on nodes, it’s great to use when you want to load CSS and JS files on specific node types, front pages, node URLs, etc.
template_preprocess_views_view – runs every view load
/**
* Implements template_preprocess_views_view()
*
* @link https://api.drupal.org/api/views/theme%21theme.inc/function/template_preprocess_views_view/7.x-3.x
*/
function TEMPLATE_preprocess_views_view(&$vars) {
// Get the current view info
$view = $vars[‘view’];

// Add JS/CSS based on view name
if ($view->name == ‘view_name’) {
drupal_add_js( /* parameters */ );
drupal_add_css( /* parameters */ );
}

// Add JS/CSS based on current view display
if ($view->current_display == ‘current_display_name’) {
drupal_add_js( /* parameters */ );
drupal_add_css( /* parameters */ );
}
}
Using template_preprocess_node is useful when loading JS and CSS files when a particular view is being used (don’t forget to replace TEMPLATE with the theme machine name).

Helpful Methods for Conditionals
Here’s a few helpful Drupal Support Service methods you can use for your conditionals. Have one you use often? Let me know in the comments below.

request_uri – Returns the equivalent of Apache’s $_SERVER[‘REQUEST_URI’] variable.
drupal_get_path_alias – Given an internal Drupal Support Service path, return the alias set by the administrator.

Looking like a foreign language to you?
Not a developer or just lost looking at the code snipplets above? Shoot me a question in the comments below, or give these ‘plug-and-play’ modules a try for a GUI alternative:

CSS Injector: http://drupal.org/project/css_injector
JS Injector: http://drupal.org/project/js_injector
Code per Views Display: https://www.drupal.org/project/cpv
Context: https://www.drupal.org/project/context

The post Load JS & CSS Conditionally in Drupal Support Service 7 appeared first on Ben Marshall.
Source: New feed

REQUEST FOR PROPOSAL

Need a quick project proposal?

Submit the RFP form below and we will send you a project proposal in 48 hours. If you like what you see, we can schedule a call to discuss the project in greater detail.

Step 1 of 2

  • Contact Information

* Subject to reasonable use. Small fixes and updates must be requested one at a time and take no more than 30 minutes. Only mission-critical tasks are addressed on weekends.

Shopping Cart
There are no products in the cart!
Continue Shopping
0