Community Documentation

Testing and troubleshooting the module

Last updated May 9, 2011. Created by jn2 on March 24, 2011.
Edited by kim-day. Log in to edit this page.

It's time to enable and fully test your module!

Enable the module

Go to Modules, or http://example.com/admin/modules, and scroll down to the bottom of the list in the 'Other' category. You should see the module 'Current posts.' Click the checkbox to enable Current posts, and save your configuration. Now you should see a link to Help beside the module name. Click it to see the help text you entered in current_posts_help.

Enable the block

Next, navigate to Structure > Blocks, or http://example.com/admin/structure/block. Scroll down to the bottom of the list. Among the disabled blocks, you should find the name, 'Current posts'. Set its location for one of the page regions and save. Navigate to another page like your homepage to see your block. Congratulations! You have written a working module.

Troubleshooting

If you get a "white screen" or a PHP error when you enable this module, it probably means you have a syntax error in your .module file. Be sure all your punctuation is correct, semi-colons, commas, etc. all in the right places, and that you have all the hook names and module short names spelled correctly. (In the case of a white screen, you may be able to find out what the PHP error was by looking in your Apache error log. Or you can try changing PHP's error reporting level.)

If you cannot find and fix the syntax error, nothing on your site will display, because Drupal will try to load your module on every page request. The easiest way to get your site working again is to delete the module's folder or move it out of the site, in which case Drupal will figure out that it shouldn't load this module after all, and your site should work again.

Clear caches

Drupal caches a lot of data, and if you are not seeing changes appear, that could be why. In this phase of the module, the caches shouldn't be an issue, but they will be as we proceed. To get all the troubleshooting instructions in one place, we'll give you the instructions here that you'll need later.

To clear the caches, go to Configuration > Performance or http://example.com/admin/config/development/performance, and click the Clear all caches button.

Comments

Help please

I have gong through everything exactly as stated in the tutorial..

Nothing is showing up on the page its like its not finding anything in the database..

Have I done something wrong?

This is the code ..

"
<?php
/**
* Implements hook_help.
*
* Displays help and module information.
*
* @param path
* Which path of the site we're using to display help
* @param arg
* Array that holds the current path as returned from arg() function
*/
function current_posts_help($path, $arg) {
switch ($path) {
case "admin/help#current_posts":
return '

'. t("Displays links to nodes created on this date") .'

';
break;
}
}

/**
* Implements hook_block_info().
*/
function current_posts_block_info() {
$blocks['current_posts'] = array(
'info' => t('Current posts'), //The name that will appear in the block list.
'cache' => DRUPAL_CACHE_PER_ROLE, //Default
);
return $blocks;

}

/**
* Custom content function.
*
* Set beginning and end dates, retrieve posts from database
* saved in that time period.
*
* @return
* A result set of the targeted posts.
*/
function current_posts_contents(){
//Get today's date.
$today = getdate();
//Calculate the date 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();

//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.
->execute();
return $query;

/**
* Implements hook_block_view().
*
* Prepares the contents of the block.
*/
function current_posts_block_view($delta = '') {
switch($delta){
case 'current_posts':
$block['subject'] = t('Current posts');
if(user_access('access content')){
//Use our custom function to retrieve data.
$result = current_posts_contents();
//Array to contain items for the block to render.
$items = array();
//Iterate over the resultset and format as links.
foreach ($result as $node){
$items[] = array(
'data' => l($node->title, 'node/' . $node->nid),
);
}

if (empty($items)) { //No content in the last week.
$block['content'] = t('No posts available.');
}
else {
//Pass data through theme function.
$block['content'] = theme('item_list', array(
'items' => $items));
}
}
}
return $block;
}

}

Hello

I've tried changing themes since I was using my own custom one. Still no good. Maybe if I knew how to debug this any idea, so I can go on to the next part....

Thanks
Rick

Ok got it to work

It was my code.

If I was you I would put the whole code into one. That way people will not get confused with the <?php tags and {} ...

thanks

Great tutorial - No trouble encountered

Thank you very much for this tutorial very carefully written. Nothing missing, nothing confusing.
Bit after bit, light comes.
Look forward to reading more.

Help Tutorial

Thanks for this tutorial. Easy to follow instructions. I had some syntax errors but was able to figure them out by backtracking. Keep up the good work.

Nice tutorial

Really nice and clean tutorial.
Very easy to follow and with complete explanations.
Thank you for sharing.

Best regards

Thank you very much for this

Thank you very much for this tutorial! Everything clear.
Respect!

Not getting the required output.

Hello,
Friends i am new to Drupal.
I have coded the way the tutorial say, bt i didnt get the required output.

Anand S. Bahuguna
Software Engineer
OSSCube

I am not successful

Enable the block

Next, navigate to Structure > Blocks, or http://example.com/admin/structure/block. Scroll down to the bottom of the list. Among the disabled blocks, you should find the name, 'Current posts'. Set its location for one of the page regions and save. Navigate to another page like your homepage to see your block. Congratulations! You have written a working module.

I didn't see my 'Current Posts' among the disabled blocks as you wrote above

mimosa

Page status

No known problems

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.
nobody click here