Posts Tagged ‘drupal’

Get the page node in hook_page_alter in Drupal 7

Tuesday, September 20th, 2011

Many times, you need to access the node in hook_page_alter(). Traversing gets ugly, so I moved it out into this little helper.

1
2
3
function _get_page_node($page) {
 return (isset($page['content']['system_main']['nodes'])) ?  $page['content']['system_main']['nodes'][array_shift(array_keys($page['content']['system_main']['nodes']))]["#node"] : false;
}

Drop this is template.php, and now you can do something like

1
2
3
4
5
6
function YOURTHEME_page_alter(&$page) {
  $node = _get_page_node($page);
  if($node && $node->type == "article") {
    // ... edit only article nodes ...
  }
}

Tags: , ,
Posted in Code | 1 Comment »

Drupal Linkshare Pixel Module Released

Monday, June 14th, 2010

I released a nice little module onto the Ubercart site today called “Linkshare Pixel.” This handly little module is will create an image tag that Linkshare requires you to put on your checkout page for tracking purposes. It takes care of building the URL out of the cart contents and store configurations, all you have to do is provide your Linkshare merchant ID.

http://www.ubercart.org/project_project/linkshare_pixel

Enjoy!

Tags: ,
Posted in Information | 1 Comment »

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();
  unset($arrCSS['all']['theme']['path/to/css-file.css']);
  $vars['styles'] = drupal_get_css($css);
  return $vars;

Tags: ,
Posted in Code | 13 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";
	  break;
      default:
        if($alias != $_GET['q']) {
	  $template_filename = 'page';
	  foreach (explode('/', $alias) as $path_part) {
	    $template_filename = $template_filename . '-' . $path_part;
	    $vars['template_files'][] = $template_filename;
	  }
        }
	break;
      }
   }
}

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.

Tags:
Posted in Code | 3 Comments »