Drupal

Drupal content management system tutorials and topics of interest.

Controlling Client-side Caching of Javascript and CSS in Drupal

While updating CSS and Javascript files on a Drupal website, I ran into a problem.  I couldn't force users to see those new versions of the CSS and Javascript files because as we all know, Javascript and CSS files are cached in web browsers.  So that left me with a problem, since the new Javascript files were needed to allow the website to operate properly.

After looking for a solution, I found that Drupal already handles this, it has a mechanism to allow you to control the client-side cache of people visiting your website.

I know what you are thinking, there's no way you can do that.  Well, you can, you are not manipulating your user's cache, you are instead just pointing them to a new URL.  Its much easier to explain in the video:

So you see, even if your CSS or Javascript files do not change contents, you can force the users of your site to grab a new copy from the server by simply passing a dummy query argument that is different from what you currently have.

The way that you can allow this functionality to happen when developing your own custom modules or themes in Drupal is to make sure that you use drupal_add_js or drupal_add_css to add your files.  That is what allows Drupal to track your files and properly append the dummy query argument to the end.

Very tricky, but very effective, and completely eliminated all the problems I had with users complaining of problems every time I did a release and had CSS and Javascript changes.

Drupal shopping cart using Ubercart

If you are looking for an easy way to implement a drupal shopping cart, using Ubercart is one of your best options.  I came to this conclusion in my article on Drupal shopping cart options.  Many out there might go for the best of breeds model, which leads you to developing your website and integrating a third party shopping cart from scratch, but why do that?  Drupal already has support for Ubercart as a shopping cart.

Drupal Twitter module - new Drupal posts update automatically to Twitter

The Drupal Twitter module is an answer to a problem that I had been looking for a solution to.  A fundamental part of a good traffic creation strategy is to ping social networking sites when you create content on your site.  As a part of that ping process, you want to have a description/title of whatever content you produce and a link back to your website to the full article.  The main purpose is so that you can drive traffic back to your website to consume your content.

How this works with the social networks is that in Twitter for example, everyone that is following you would see a tweet that you just posted to your blog.  But doing this manually every time you post to your blog or create an article would be time consuming.  Luckily, there are automatic pinging module available for Drupal called the Drupal Twitter module.

It works by saving your Twitter account username/password, then every time you post something, it will go out and automatically create a Tweet for you.  Its really simple, I actually found a tutorial to show you how to install the Drupal Twitter module.

Drupal related nodes by taxonomy term

One way to organize content in Drupal is through the use of taxonomy terms in a vocabulary.  This is not only important for organization, but also for getting visitors to your site to be presented with relevant related content once they find a valuable piece of information on your site from a search engine.  Ideally, when a vistor lands on a node that is tagged with term1 and term2, I would like a block in the sidebar to list other nodes that are tagged with either term1 or term2.  I couldn't find an easy way to do this in Drupal using a module, and the Views module is overwhelming, so I developed my own custom coded PHP block to accomplish this.

Here are my requirements for the design of this related nodes block:

  • Only display when viewing a node that has at least one taxonomy term associated with the node.
  • If the node has multiple taxonomy terms, group the terms together with an OR so I get all possible related nodes.
  • Be displayed in a list view that is themed like the rest of the site.
  • For now, display as many related pages as possible.

So go ahead and create a new block, and enter this as the block body:

 

 

<?php
  // make sure content is a node
  if ((arg(0) == 'node') && is_numeric(arg(1))) {
    // get the taxonomy terms for the current node
    $tax = taxonomy_node_get_terms(arg(1));
    // keys in the $tax array are the term ids, need to slice them out
    $tids = array_keys($tax);
    $operator = "or";
    // to find all related nodes, or all the terms together
    $result = taxonomy_select_nodes($tids, $operator);
    // fetch all of the terms with a db query
    while ($obj = db_fetch_object($result)) {
      // get info from the node
      $node = node_load(array('nid' => $obj->nid));
      // display titles and links to the node
      $items[] = l($node->title, "node/". $node->nid);
    }
    // theme the list using the standard built-in list theme
    return theme('item_list', $items);
  }
?>

And be sure to change the input type to "PHP code" before you save.

Now the block will be created, but we need to go into the block to set the block visibility so that it will only show on nodes that have some taxonomy terms associated with them.  Enter this into the Visibility section as PHP code:

<?php
if ((arg(0) == 'node') && is_numeric(arg(1))) {
  // get the taxonomy terms for the current node
  $tax = taxonomy_node_get_terms(arg(1));
  // if more than 1 term, show block
  if(count($tax) > 0) return TRUE;
  // otherwise hide block
  else return FALSE;
}
// if not a node, don't show the block
else return FALSE;
?>

Drupal shopping cart options

I have recently started looking at how to handle taking credit card payments for goods or services from a website. And since I implement sites in drupal, I geared my search towards shopping cart implementations in drupal.

So here are my requirements in searching for a shopping cart to use with drupal (or any CMS):

  • Shopping cart must be free and open source.
  • Shopping cart must support multiple payment processing companies to avoid single-sourcing.
  • The module available for Drupal must be stable and have supporting modules around it for customization.
  • Must support taking credit card payments. Other payment options are bonus.
  • Reports on transactions are necessary to track user behavior and optimize revenue. Reporting interface that supports customizations is bonus. Must at least allow exporting data out to generate custom reports.
  • Be able to calculate shipping easily. The world is used to the amazon's and buy.com's, they should expect nothing less at my site.
  • Be able to customize the look and feel of the shopping cart. It shouldn't be branded to look like an advertisement for the shopping cart company.

 

Well yeah I'm kinda picky! I don't think that a small site should have to settle for much less than the big boys!

After looking around Paypal's huge list of supported shopping carts, the one I found that was free, open source, and has great module support in drupal is Ubercart. They support payment processing through Paypal Website Payments Pro and Authorize.net, and others. This enables credit card and check processing, and many eBay shoppers are very familiar with PayPal and probably already have accounts.

And one of the things that can make or break an open source software product for me is the availability and quality of the documentation available either from the open source project itself or the community. After browsing around the documentation directly on Ubercart, it was obvious that the software was designed to be easy to use and well documented.

So I'm really excited because I've found a pure open source solution to a common ecommerce problem. I think I always had a false notion in my head that there were high barriers of entry to making a ecommerce site because of all the fees and cost that would be involved in finding a shopping cart and payment processing method, but that doesn't exist anymore thanks to Drupal and Ubercart. The only cost really involved now is the fees by the payment processor for handling the credit cards and other payment options. More on that another day...

Syndicate content