Archive for the ‘Code’ Category

Add CSS File To Specific Pages In Drupal

Thursday, December 10th, 2009

I get asked a lot how to go about adding certain CSS files to only certain pages of a Drupal project, rather than every page. This is commonly desired when you are using a drastically different front page in comparison to your drill pages. To achieve this you will want to use the drupal_add_css() function provided by the Drupal API.

It’s very basic in functionality:

drupal_add_css(path_to_theme() . '/layout-fixed.css', 'theme', 'all');

Given a path to a CSS file, it will load it where ever the function is called. The second param specifies whether you are theming a “theme” or a “module” with it, and the third param reffers to the “media” attribute of the <link> tag.

I’ve seen many coders drop this call right into the page or node template they are theming. That is really less than desired, as it tightly links a css file to template file. Say you wanted to use this css file 5 times, you would have to add it to 5 files. That duplication would make it time-consuming to change if you had to alter the name of the file, for example (and that is provided you can remember where you put every call to drupal_add_css()).

A cleaner approach is to put it’s call in your template.php and use some logic to control when to use it. Something like:

if($_GET['q]=="node/2") {
drupal_add_css(path_to_theme() . '/crazy-style.css', 'theme', 'all');

Doing this will ensure that all of your dynamically added CSS’s files live in one spot, making it super easy to add or extend to.

To remove an existing CSS file per page, try something like:

 $arrCSS = drupal_add_css();
  $vars['styles'] = drupal_get_css($css);
  return $vars;

Tags: ,
Posted in Code | 13 Comments »

Remove Your Folder From SVN

Monday, December 7th, 2009

Did you ever have a folder that was nested with .svn folders and you wanted to quickly remove them and just make them a non-versioned folder? Perhaps because you are going through old repo checkouts that are too out of date to care, or you want to copy a repo-ed folder to a new project without it being externalled, or maybe because the folder got screwed up on a commit and you just want to recommit it? Try running this unsvn code.

find . -name ".svn" -exec rm -rf {} \;

Just save this code to a file (I call mine “unsvn”), run it from the parent level directory and it will remove all .svn folders in itself and all of it’s children.

Tags: ,
Posted in Code | No Comments »

Load jQuery on Any Page

Saturday, November 28th, 2009

I found this little snippet of code, simply stick it in your address bar and it will create a script element that loads jQuery from Google, so you can use jQuery calls from console.


Posted in Code | 2 Comments »

Name Your Drupal Template Files By Node Title

Monday, November 9th, 2009

Out of the box, Drupal has a really awesome cascading filename theming system. It is really easy to theme idividual content types simply by naming your file “page-contenttype.tpl.php”. But many times, I want to be able to create a bunch of “Page” content types really quickly (ex. “About Us”, “Privacy Policy”, etc) and theme it uniquely by node. Personally, I think it’s easier to be able to name your files according to the title of the node, and not by IDs, because when I come back to a project in a month, I won’t remember what “14” is referring to.

To combat that, I use this little helper hook which I drop into template.php

function theme_preprocess_page(&$vars, $hook) {
  if (module_exists('path')) {
    $alias = drupal_get_path_alias(str_replace('/edit','',$_GET['q']));
    switch($alias) {
      case "override":
        $vars['template_files'][] = "page-different-filename";
        if($alias != $_GET['q']) {
	  $template_filename = 'page';
	  foreach (explode('/', $alias) as $path_part) {
	    $template_filename = $template_filename . '-' . $path_part;
	    $vars['template_files'][] = $template_filename;

Now, by default, you can name your page template files by a hyphen-separated list phrase of the node title. So if I make a node called “About Us”, I can now theme the page by creating a template file named “page-about-us.tpl.php”. If you have a case where the default naming convention won’t work for you, simply put a case in the switch statement to case the particular page by slugged title.

Posted in Code | 3 Comments »

Set a value of 0 for unchecked checkboxes

Thursday, July 9th, 2009

Many people who aren’t too familiar with the Ruby on Rails framework may do well to learn about a convenient little trick they do with submitting check boxes.

In HTML, when you click a check box and submit it, by default, the value POSTed is “1″ – and if you do not click it, nothing is submitted. This sometimes makes keeping track of check boxes difficult.

To combat this, Rails puts a hidden field before the check box, with the same name as the check box, with a value of zero.

Click this to set my_ckbx to 1

Now when the form loads, my_chbx is defaulted to “0″. If you choose to click the check box, my_ckbx will be set to “1″, if not, it will remain “0″. No matter what, my_ckbx will POST a value.

Posted in Code | No Comments »

Introduction to JSON and PHP

Monday, May 25th, 2009

My tutorial, “Introduction to JSON and PHP”, that I wrote for has been archived. You can now read it at

Posted in Code | 1 Comment »