10 Comments

Found in Tutorials

June 20, 2010

5 Quick and Dirty Drupal Tips

Post to Twitter

Drupal is an incredibly rich platform for content management, but its not always a walk in the park for front-end developers who need a quick solution. At the risk of insulting hardcore Drupal developers everywhere, here are some quick tips and useful snippets to help save you hours of development time and assist in keeping the hair-pulling-out to a minimum (hopefully).

1. Limit Blocks of Code Based on Authentication Status and Role
2. Place Block Regions In Any Template File
3. Print Views Programmatically
4. Create a Homepage Views Sandbar
5. Deploy a Views “Items Per Page” Filter

1. Limit Blocks of Code Based on Authentication Status and Role

The following snippet can be used to provide alternate content for unauthenticated users and vice versa:

<?php
global $user;
if ($user->uid) {
<!-- content for logged in user-->
} else {
<!-- content for unauthenticated user-->
}
?>

Restricting content by role is just as easy using the machine readable role name in the following snippet:

<?php
  global $user;
  if (in_array('YOUR_ROLE_HERE', array_values($user->roles))) {
<!-- content for specific role-->
  }
?>

Be sure to include the call to ‘global $user;’ when deploying these snippets, lest you be caught with your pants down.

2. Place Block Regions In Any Template File

By default, block regions are not accessible by normal means in views or node templates. There are two ways to access a block region outside of page.tpl.php. After you declare a region in your .info file, place the following snippet where you’d like the block to display:

<?php
	print theme('blocks', 'YOUR_REGION');
?>

The more acceptable way is to create a variable in your template.php,

<?php
function YOURTHEME_preprocess_views_view__index(&$vars, $hook) {
	  $vars['YOUR_REGION'] = theme('blocks', 'YOUR_REGION');
}
?>

then print the variable as you normally would in your template:

<?php
print $YOUR_REGION;
?>

3. Print Views Programmatically

The following snippet of code allows you to place a view right into your .tpl file. There are numerous pros and cons to this approach. Lucky for you, this is Quick and Dirty, so I’ll just give you the pros.

<?php
  $view_args = array();
  $display_id = 'default';
  $view = views_get_view('YOUR_VIEW_NAME');
       if (!empty($view)) {
        print $view->execute_display($display_id , $view_args);
  }
?>

The greatest advantage to this is the ability to quickly and easily use variables of the current page as dynamic arguments. Views 2 may give you access to some wonderful filters, but you may want to refine a view based solely on the variables of the current node (a related posts view that pulls a set of nodes with similar taxonomy terms, for example). I’ve also found it quite useful when deploying tabbed jQuery widgets (a good example is the “Popular” sidebar widget I built on http://governingpeople.com/).

Adding an argument to the view is as easy as updating this line:

 $view_args = array($place_variable_here);

It’s important to note that you must define your argument in the view for this to work correctly. Also, if you have multiple arguments and only want, say, the second argument to be programmatic, change the line to this:

 $view_args[1] = $place_variable_here;

You can also alter the display id to choose between your different view displays:

 $display_id = 'default';

I highly recommend NOT using a Page view. This WILL override the title of the entire page. Default has always worked fine for me.

4. Create a Homepage Views Sandbar

This is a great way to break a view apart for

IMPORTANT: This snippet is built for an Unformatted List view.

You’ll need to go ahead and create a template for you view named views-view-unformatted–YOURVIEWNAME.tpl.php (replacing with the correct machine readable of your view, respectively). If you already have one of these created using Views 2′s lovely “Theme Information” tab, you probably have a document that looks something like this:

<?php
// $Id: views-view-unformatted.tpl.php,v 1.6 2008/10/01 20:52:11 merlinofchaos Exp $
/**
 * @file views-view-unformatted.tpl.php
 * Default simple view template to display a list of rows.
 *
 * @ingroup views_templates
 */
?>
<?php if (!empty($title)): ?>
  <h3><?php print $title; ?></h3>
<?php endif; ?>
<?php foreach ($rows as $id => $row): ?>
  <div class="<?php print $classes[$id]; ?>">
    <?php print $row; ?>
  </div>
<?php endforeach; ?>

Now, we’re going to take that lovely default template and gut it a bit. For the sake of simplicity, I’m removing everything that we dont need, including the title (you may choose to keep this and style the template any way you wish).

The first snippet of code accesses the view’s pager and creates two variables based on the Items Per Page. Right below this, we’ll make the split with a very simple for loop for each of the two sections:

<?php
$whole = $view->pager['items_per_page'];
$half = ceil($whole /2);

for( $i=0; $i<$half; $i++ ) {
	print $rows[$i];
}
?>

SANDBAR CONTENT GOES HERE

<?php
for( $i=$half; $i<$whole; $i++ ) {
	if ($rows[$i]) {
		print $rows[$i];
	}
}
?>

It may not be pretty, but it works wonders.

5. Deploy a Views “Items Per Page” Filter

I’m still surprised this option isn’t built in. Maybe I’m missing something. I’ll show you the simplest way to execute a pager with a set of links, but you can use this simple approach to build a dropdown with multiple choices with relative ease.

Open up your views-view.tpl template for the specific view you’ll be working with. It’s important that the view you created has its “Items to Display” option set equal to the highest option you will provide. If you want to have a maximum choice of 48, set you view’s limit to 48. If one of your choices exceeds the maximum, it just won’t show them. You’ll be able to set a default setting in the template.

<?php
$itemcount = $_GET['itemcount'];
if ($itemcount = 0) {
	$view->set_items_per_page(12);
} else {
	$view->set_items_per_page($itemcount);
}
?>

Place this snippet at the top of your views-view tpl an you’re already halfway there. Quite simply, attaching a variable to the end of the url (i.e. http://www.yoursite.com/yourview?itemcount=24) will set the new count before executing the view. If no variable is present, it is set to default to 12. Here is a sample of an “items per page” div you can place anywhere within your view:

<h3>Items per page:</h3>
<ul>
<li><a href="your-view-name?itemcount=6">6</a></li>
<li><a href="your-view-name?itemcount=12">12</a></li>
<li><a href="your-view-name?itemcount=24">24</a></li>
</ul>

Until Views ship with a better alternative, this is a quick and painless way to implement an items per page filter.