New Podcast

Posted Tuesday, January 31 at 3:28 pm
  Thomas Turnbull (tom_o_t on drupal.org) and Alan Palazzolo (zzolo on drupal.org) join Mike Anello to talk about their new book from O’Reilly Media, Mapping with Drupal. Mike’s usual co-hosts, Andrew and Ryan, were both unable to participate in the podcast, leaving Thomas and Alan subject to Mike’s long-winded (but extremely interesting by some accounts) questions.
Download Podcast 73
DrupalEasy_ep73_20120131.mp3
Syndicate content

NEWSLETTER

Stay informed on our latest news!

Syndicate content

Testimonial

The DrupalEasy workshop provided a balance between "Best Practices" for Drupal development and an elegant example of integrating content from Youtube , Flickr, etc. using the FeedAPI. The workshop provided useful insights in advanced site design and a wealth of resources to speed up development and make deployment safer.

Who are we?

DrupalEasy is the collective expertise of Ryan Price and Michael Anello, who joined forces to provide training and consulting services worldwide. Read all about them and what they can do.

What is Drupal?

Drupal is a free, super-powerful content management system for sites that require information posting and collection, including blogs, forums, videos, photos, and databases of information. We think it is the best platform available. Here's why...

Why Drupal?

More and more savvy organizations are going with Drupal for content management, and its no mystery why. It’s free, flexible, and easy to maintain for small or large volume sites. Learn more...

I need to change the number of items returned by a view, based on the theme!

A friend recently came to me with the following question:

I have a site that has a mode optimized for low-bandwidth use. Can I change the number of rows returned by a view based on the theme?

The Views 2 API has a function called hook_views_query_alter() which allows you to modify the View object before it pulls the results from the database.

In order to use views hooks, you must create a module that registers itself as using the Views API:

<?php

function drupaleasy_views_api() {
  return array(
   
'api' => 2,
   
'path' => drupal_get_path('module', 'drupaleasy') .'/includes',
  );
}
?>

Assuming your module is called drupaleasy.module. In the includes directory in your module folder, create a drupaleasy.views.inc file. The whole path would be something like sites/all/modules/drupaleasy/includes/drupaleasy.views.inc.

In this new file, create a function, and modify the view object appropriately:

<?php
function drupaleasy_views_query_alter(&$view, &$query) {
 
/* figure out which theme you're using */
 
$view->pager['items_per_page'] = 3;
}
?>

Even though my friend was using a theme, you may want to make this change based on some other context.

For another tutorial on hook_views_query_alter(), check out this post by Nick Lewis.

Trackback URL for this post:

http://drupaleasy.com/trackback/383
Your rating: None Average: 4 (1 vote)

More Quicktips


No comments

Pingback

[...] I need to change the number of items returned by a view, based on the theme! from drupaleasy.com [...]

Syndicate content