Community Documentation

Adapting the query

Last updated November 11, 2011. Created by jn2 on April 6, 2011.
Edited by ressa, kariem. Log in to edit this page.

Main topic described: Database API

The new page function will do almost the same work as current_posts_block_view, which derives its data from our custom function, current_posts_contents. Now the benefit of writing a separate function for our database query becomes apparent. All we need to do is edit it to take an argument and adjust some of the query code, and it will be ready to go for the page.

Here's the edited code:

<?php
function current_posts_contents($display){   //$display argument is new.
  //Get today's date.
 
$today = getdate();
 
//Calculate midnight a week ago.
 
$start_time = mktime(0, 0, 0,$today['mon'],($today['mday'] - 7), $today['year']);
 
//Get all posts from one week ago to the present.
 
$end_time = time();
 
 
$max_num = variable_get('current_posts_max', 3);
 
 
//Use Database API to retrieve current posts.
 
$query = db_select('node', 'n')
    ->
fields('n', array('nid', 'title', 'created'))
    ->
condition('status', 1) //Published.
   
->condition('created', array($start_time, $end_time), 'BETWEEN')
    ->
orderBy('created', 'DESC'); //Most recent first. Query paused here.

  
if ($display == 'block'){
 
// Restrict the range if called with 'block' argument.
   
$query->range(0, $max_num);
  }
//Now proceeds to execute().
  //If called by page, query proceeds directly to execute().
 
 
return $query->execute();
}
?>

First of all, we add the $display argument to the function. Then, in the query statements, we pause building the query by adding a semi-colon after the orderBy method. We then get the function's argument value and decide how to continue. If the argument is from the block, we pick up the query object and restrict the range, then proceed to execute. If it's from the page, we exclude the range() method and proceed straight to execute, thus including all the available posts.

Editing current_posts_block_view

To make this code work as it did in the current_posts_block_view function before the change, we must add one word to one line of code:

<?php
$result
= current_posts_contents('block');
?>

Adding the word 'block' as an argument to the function call is all it takes.

Comments

How to fetch content type using above query.

Hi all i am new in drupal. I have created one module(Current post),here i want to display only content type(only for address book content type node).how to write above current_posts_contents() method to display result?can any body tell me?

Just add a condition

Hi,
You can just add a condition,
->condition('type','TYPE_YOU_WANT_TO_DISPLAY')
:)

Kristougher

Always blank page after editing this

At this point the module should display page from http://www.mysite/current_posts......right?
But I,ve always a blank page
Cache cleared and module reinstalled ...
Moreover, drupal displays this error message:Warning: call_user_func_array() expects parameter 1 to be a valid callback, function '_current_posts_page' not found or invalid function name in menu_execute_active_handler() (line 516 of /Applications/MAMP/htdocs/mysite/includes/menu.inc).
What could be the issue?
I'm on D7.15

Page callback not found

It seems that there is something wrong with the page callback function.

Have you changed the name of _current_posts_page ?
If no, look for irregularities in or before that function.

The blank page is often due to an error in the template, but right now I think you should focus on the menu callbacks.

Kristougher

Missing _current_posts_page

senzaesclusiva,

The missing function is defined in the next page of this section, Theming the page

Page status

Needs technical review

Log in to edit this page

About this page

Drupal version
Drupal 7.x
Audience
Programmers

Develop for Drupal

Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.