I have a carousel built using Views (7.x-3.x) that displays 10 items. In this carousel I would like to place 1 item (inclusive of the 10) that displays Twitter content.

What I would like to do is run the function that uses the Twitter API before the View is built/executed. If it succeeds then the View should display 9 items from it's own query (10 items total.) If it fails, then the View would display 10 items from it's own query.

I have successfully implemented the View and the Twitter function running on the theme layer though in order to have a 'fall back' by altering the number of items displayed I would need to do this before the View is built/executed.

Can anyone point me in the right direction for this?

I believe I am looking at the right function "hook_views_query_alter" but I'm not sure how to set "items_per_page"


4 Answers 4


You might be interested in a similiar kind of question I answered here:

How do you customize the title of an RSS feed created by Views RSS?

I would just set the view size to 10 always. And then use array_unshift() as I did in that reply to "prepend" the new item Object to the front of the view results array, if your Twitter call return its 1 item of content. Or else you just let the original 10 come from Views.


The hook you're looking for is probably pre_render. There you can execute your function and replace the first row of the view with whatever you want.

  • You probably should use the proper hook name: hook_views_pre_render().
    – rooby
    Commented Feb 24, 2014 at 13:20

Not a full answer to your question, but I typically set items_per_page in hook_views_pre_build, like

function mymodule_views_pre_build (&$view)
    if ($_GET["perpage"]) {
        $perpage = check_plain($_GET["perpage"]);

        if (is_numeric($perpage) && (int) $perpage > 1) {
            $view->pager["use_pager"] = TRUE;
            $view->pager["items_per_page"] = (int) $perpage;
        } else if ($perpage == "all") {
            $view->pager["use_pager"] = FALSE;
            $view->pager["items_per_page"] = 0;

This is from a Drupal 6 Views 2 site, but I am pretty sure this still works with Drupal 7 and Views 3.

  • I implemented that hook into my module and included -- $view->pager["items_per_page"] = (int) 2; This did not change the display items to 2... Commented Mar 28, 2012 at 15:06
  • Are you sure the hook is running? Views 3 may be different. You may need to tweak the code a bit.
    – mpdonadio
    Commented Mar 28, 2012 at 17:50
  • if I do a print_r($view->pager); immediately after I get Array ( [items_per_page] => 2 ). Perhaps it is a D6 vs. D7 thing... Commented Mar 29, 2012 at 20:18

I first used hook_views_query_alter(). While the code below does work I needed a solution that was a better fit my entire situation.


The better solution was to call the Twitter API during the hook_views_pre_render() phase. If good data was returned then I appended this data to $views for use later.

function mymod_views_pre_render(&$view) {
  if ($view->name == 'carousel') {
    $view->mymod_carousel = array();

    $data = twitter(); // custom function that requests data from Twitter API
    if (!empty($data)) {
        $count = count($view->result); // we need to remove the last node in the views result so we always have the same number of items
        if ($count) {
            $view->mymod_carousel['twitter'] = $data;

Then during hook_views_post_render() I test for this new data and if it is present then I basically result $output using hook_theme() from the jCarousel module.

function mymod_views_post_render(&$view, &$output, &$cache) {
  if ($view->name == 'carousel') {
    $result = $view->result;
    $items = array();
    $count = 0;
    foreach($result as $node) { // loop through the results of the view and add our Twitter data at a specific location
        if ($count == 2) {
            if (!empty($view->mymod_carousel['twitter'])) {
                $data = $view->mymod_carousel['twitter'];
                $items[] = theme('mymod_carousel_twitter', array('data' => $data));
        if (!empty($node)) {
            $items[] = theme('mymod_carousel_item', array('node' => $node));

    $options = $view->style_options;
    $output = theme('jcarousel', array('items' => $items, 'options' => $options));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.