Learn Drupal with tons of focused Drupal tutorials at Build a Module.com

newsletter icon

twitter icon

rss icon

6.x

greggles's picture

Creating Local Copies of External JS for fun and performance: Drupal Starter Module

Often you need to load external JavaScript resources into a Drupal page. Unfortunately those can be from external servers that are slow. Even if the servers are fast, it is great to be able to take advantage of Drupal's JavaScript aggregation to make it a single HTTP request for the JavaScript file instead of multiple. The attached starter module is useful to create a local copy of external JavaScript files. This is a task that is really site specific. There are visibility rules and lists of modules to maintain. So, rather than creating a contributed module for it I'm presenting it here as a tutorial. You can also download the code for Drupal 6 as a .tgz. There are three functions in this starter module.

List out your files in customlocaljs_files

This function is just a piece of configuration to let other functions know which files they are dealing with.
<?php
/**
* A helper function to list all your external js files.
*/
function customlocaljs_files() {
  return array(
   
'example1' => 'http://example.com/number-magic.js',
   
'exampleb' => 'http://example.com/fun-with-advertising.js',
  );
}
?>

Download and Add the JavaScript in customlocaljs_init

This function shouldn't need any tweaks unless you want to modify exactly which pages on the site the JavaScript should be added to.
<?php
function customlocaljs_init() {
 
// If you want you could also do this in a node_api or wherever it makes sense.
  // Only do this in your init if that's where you need to add the js.
 
$directory = file_directory_path() .'/customlocaljs';
  foreach (
customlocaljs_files() as $key => $url) {
   
// First make sure that the file is available locally.
   
$file_destination = $directory .'/'. basename($url);
    if (!
file_exists($file_destination)) {
     
$result = drupal_http_request($url);
      if (
$result->code == 200) {
       
// Check that the files directory is writable.
       
if (file_check_directory($directory, FILE_CREATE_DIRECTORY)) {
         
file_save_data($result->data, $directory . '/' . basename($url), FILE_EXISTS_REPLACE);
        }
      }
    }
   
// This actually adds the file to the page.
   
drupal_add_js($directory .'/'. basename($url));
    }
  }
}
?>

Delete the files every 24 hours: customlocaljs_cron

The final purpose is to delete the files every 24 hours. This is important since the code that downloads files only goes to get them if they aren't available.
<?php
function customlocaljs_cron() {
 
// Redownload the files every day.
 
if (time() - variable_get('customlocaljs_last_cache', 0) >= 60*60*24) {

   
// You have to create this file locally.
   
foreach (customlocaljs_files() as $key => $url) {
     
file_delete(file_directory_path() .'/customlocaljs' . '/' . basename($url));
    }

   
// Clear aggregated JS files.
   
if (variable_get('preprocess_js', 0)) {
     
drupal_clear_js_cache();
    }

   
variable_set('customlocaljs_last_cache', $_SERVER['REQUEST_TIME']);
  }
}
?>
And that's it! Once you've done this you should of course make sure that you are running cron and then bask in the glory that is locally aggregated javascript files.

The Drupal Community e-book

$0.00
Written by: 
Ezra Barnett Gildesgame and Greg James Knaddison

Some people use Drupal for a long time before having direct interaction with this community. These folks might be unaware that the community is out there, or of the benefits of having a good relationship with it.

In this chapter, we'll introduce the community and give some suggestions for how to interact with it (and get support) to get the best results for you and your website(s). First, let's start by sharing some basic information about the community. Then we'll talk about finding support. We'll end with some suggestions of ways that you (yes, you!) can say thanks to the Drupal community for the help and free software.

Summary

  • A glance at the community
  • Using Drupal.org and Groups.Drupal.org to Get Answers
  • Finding the Best Contributed Modules
  • Drupal Community Values
  • Be Thankful
  • Drinking from the fire hose

Improving User Experience with Recommendations e-book

$0.00
Written by: 
Ezra Barnett Gildesgame and Greg James Knaddison

If you've ever seen a list such as “Popular News Stores,” “Customers who bought this item also bought” or “Other stories by this author,” then you're familiar with the idea of related and recommended content, and the value these recommendations can add for website visitors. They also add value for site owners, since suggestions (and especially good suggestions) of additional content make people more likely to stay on your site for longer, view more pages, and buy more products.


Monetizing Your Site e-book

$0.00
Written by: 
Ezra Barnett Gildesgame and Greg James Knaddison

Whether you're looking for a new way to generate income or just trying to cover the costs of operating a website, you've probably wondered how your site will make money with its valuable content. In this chapter, we'll explore several different ways of generating income using external services like Google AdSense and Amazon.com's affiliate program, as well as self-contained systems for selling ads and charging to view and post content.

Contents

  • Context-Sensitive ads with Google AdSense
  • Building a self-contained ad selling system
  • Online classifieds: Paying to post
  • Subscription only Content
  • Summary

Introduction to Drupal e-book (free)

$0.00
Written by: 
Ezra Barnett Gildesgame and Greg James Knaddison

Drupal is an open source content management software tool used by individuals, news and publishing companies, world government organizations, major record companies, and nonprofit organizations that allow people to build websites, publish content such as text, video and pictures and communicate with one another.

This free PDF can be downloaded instantly after checkout.


Creating sessions and a schedule with COD

COD, the Conference Organizing Distribution for Drupal, is an easy and streamlined way to build powerful event websites. The recent release of COD Alpha 2 comes with a Views-powered schedule and many other great features.

In this quick screencast we'll build a single day, two-track conference using the COD Session feature.

Length: 
4 minutes

Smart Landing Pages with Panels e-book

$5.00
Written by: 
Ezra Barnett Gildesgame and Greg James Knaddison

Panels makes it relatively easy to integrate your Drupal site's content into dynamic landing pages with customized layouts.

This PDF can be downloaded instantly after purchase. Download the Features file that provides essential assets.


Mastering Views e-book

$5.00
Written by: 
Ezra Barnett Gildesgame and Greg James Knaddison

The Views module addresses a common need for people with a website full of content: displaying that content without having programming knowhow.

Views is one of the most innovative and important Contributed modules, empowering site administrators to easily create complex displays of content, and has changed the way developers build their modules.

This PDF can be downloaded instantly after purchase.


greggles's picture

Tutorial: Calculating Distances and Importing GeoPostcodes Data into the Drupal Location Module

For a recent project we needed to do a fair bit of work with locations, maps, and distances:

  • Associating retail stores with latitude/longitude
  • taking users and finding the nearest store based on the entry of a postal code
  • showing nodes in a nodereference based on distance.

Solving the last two of these things ended up being interesting enough that I wanted to share them with you.


greggles's picture

Drupal Tutorials

When you're learning Drupal it can be really helpful to read how other people have accomplished the same thing that you're trying to do.

Drupal Tutorial and recipes handbook

The Drupal.org Tutorials handbook is a great resource. It contains hundreds of pages showing how to do all sorts of different things.

Learning from Drupal Case Studies

Another great tool is the Case Studies where people explain how they built a specific site.

Similarly, the recipes area is full of great tips for how to make Drupal achieve certain features.


Syndicate content