Last updated 22 December 2010. Created on 5 January 2008.
Edited by Jim Phlew, tuxnani, Pogle, Chris Einkauf. Log in to edit this page.

At this point, you can install your module and it'll work. Let's do that, and then see where we need to improve the module.

Install

To install the module, you'll need to copy your onthisdate.module and onthisdate.info files to the right directory on your site, if they are not there already (probably sites/all/modules/onthisdate -- see the first page of this tutorial for more information).

Enable

Log in as your site administrator, and navigate to the modules administration page to get an alphabetical list of modules. In the menus: Administer » Site building » Modules, or via URL:

  • http://example.com/admin/build/modules
  • http://example.com/?q=admin/build/modules

When you scroll down, you'll see the onthisdate module listed with the description next to it, in the "Other" section. Enable the module by selecting the checkbox and save your configuration.

Configure

The purpose of this module is to display a block, but just enabling the module doesn't make the block display. You need to go to the blocks administration page (Admin >> Site building >> Blocks or paste admin/build/block) and enable it.

Enable the block by selecting a region in the drop-down list for the 'On This Date' block and save your blocks. Be sure to adjust the location (left/right) if you are using a theme that limits where blocks are displayed. After saving the block region setting, you might also want to change the configuration (by clicking on the "configure" link) so that the block is only displayed on certain pages of your site or is only visible to certain roles. One thing to be aware of is that if you are logged into your site as User 1 (the original account created when you installed Drupal), you don't necessarily have a role besides "authenticated user".

Test

To see the block, navigate to another page (it's hard to see them on the Blocks page itself); if you chose to have the block visible only on certain pages, be sure to navigate to one of those pages. Note that our example module is configured to display only posts from the day exactly one week ago, and, if there are no results, to display the message "Sorry No Content".

For testing purposes, you may want to create some content (or edit some existing content) and adjust the "Authored on:" date to be a week ago, if your site lacks content from that particular date.

Troubleshoot

If you get a "white screen" or a PHP error on your screen when you enable this module (or after editing the file while it is enabled), it probably means you have a syntax error in your .module file. (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.) 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, in which case Drupal will figure out that it shouldn't load this module after all, and your site should be working again.

Should you find that Drupal does not appear to recognize a specific hook you have implemented (for example you have function moduleName_block(...){} defined but there is no listing for your module in http://example.com/admin/build/block) double check your definition statement. Misspelling the moduleName can cause this.

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

wanneng’s picture

Hi,
I can configure the block "On this date" on rightside, but i can not see it on rightside. what is wrong?
thanks
wanneng

MindStalker’s picture

I had this problem too initially, thing is the block only shows content created withing a 24 hour day exactly 1 week ago. In reality its a bad starting code because few rarely have that if they are just starting out programming.
What I did was create a few forum post then I edited the code so $end_date = time();

(time() produces the current time in unix timestamp)
This way you get a weeks range.

You can also edit the code so where it returns nothing

if ($block_content == '') {
// no content from a week ago, return nothing.
return;
}

To instead return something

if ($block_content == '') {
// no content from a week ago
$block['subject'] = 'On This Date';
$block['content'] = 'Sorry No Content';
return;
}

Author you really should make these changes because this can be very confusing to someone who just installed drupal and wants to learn modules and is surprised when nothing causes the block to not be rendered at all. This caused me several hours of frustration personally.

gottsman’s picture

I was able to make the code above work correctly after I returned the $block array.

iddqd’s picture

You can also just take the "- 7" out of:

$today['mon'], ($today['mday'] - 7), $today['year']);

for a really quick change, then post something while testing. It should then show up since this makes the query look for today instead of a week ago. I have no idea as to how that seemed like a good idea for a result to quickly see after a tutorial. lol.

bits’s picture

Slight correction in the code below:

if ($block_content == '') {
// no content from a week ago
$block['subject'] = 'On This Date';
$block['content'] = 'Sorry No Content';
return;
}

it must return $block

IbnKhaldun’s picture

Many text editors include tabs to indent lines. You must replace them by spaces in order to make your code runs.
I had that difficultie. I replaced the hidden tabs by spaces; then my code works.

More: the example has some warnings: like ",$" and ".$" replace by ", $" and ". $" (don't use my doublequotes)
NOTE Put a space in between of the "." operator and the ",". And between the "." and the "$" variable indicator.

regards ^_^ç

tzoscott’s picture

I also hit the problem of 'nothing visible'.

Given that new users might overlook or forget such a caveat to "fake" content", and that they're practicing on a test site with very little content, and probably they'll have to remember to change article dates more than once. When PHP code has errors, on most modern webserver installs you will not get any PHP errors.

So it seems to make more sense to accommodate user habits, and just made the date range wider:

Change $start_time to be something bigger than -7 days... here I did 28 days:
(ignore the start

    ...
    // Look 28 days back
    $start_time = mktime(0, 0, 0,$today['mon'],
                               ($today['mday'] - 28), $today['year']);

    // 28 days
    $end_time = $start_time + 2419200;
    ...

... then add the 'Sorry, no content found...' if block['content'] == '', as someone already suggested.

This will negate the chance of the user thinking the code's not working, because they'll always get content.

Other than that, fine article! Thank you for your documentation.

invisible_swordsman’s picture

That thing was the worse one I ever saw. That can't be named 'manual'. Someone here said that this is more a 'receipt',some kind of copy and paste,but that it gives not understanding. I disagree. I think it is worse. Just try to do EXACTLY what it tells you to do, and you end up with nothing. But I agree that it doesn't make it easy to understand; the few comments are better then all the 'thing' together. Someone here with authority should just delete this 'thing' for good. The problem with those kind of useless things is that it not just take you time that you will never get back, but takes the space; at this exactly space we could have a tutorial about how to create modules, instead of this 'thing'

aikiken’s picture

Would be for you to take that passion and invest it in creating a rewrite and offering to the site authors. Make it clear, I dare you... :)

maartenkuilman’s picture

    // -1 month
    $start_time = mktime(0, 0, 0, ($today['mon'] - 1), 
                               $today['mday'], $today['year']);
manop’s picture

Well, although this might not be one of the best manuals on Drupal.org, it simply gives me enough knowledge understanding module creation method. The block module works for me :).

swelljoe’s picture

The first Drupal user is special. It's not an administrator (by default), but it has full administrator privileges. I'm using the first user for testing and development on my devel Drupal instance.

When I first followed this tutorial, the resulting module was configured to only display for administrators (so I could experiment). Thus I saw nothing, though everything else, such as configuring blocks and such, seemed to be working. A bit of error logging revealed that nothing in _block was ever running, and that tickled a vague memory that first user was somehow not automatically an administrator roled user. Changing the new block to display for all authenticated users made it show up for me for testing.

Just a heads up. I imagine others could be in the same boat, and be confused by nothing showing up.

solidad’s picture

I am in the admin/build/block area and don't actually see the "onthisdate" block at all to be able to place it in a region. I enabled the module and it it enabled fine, but I don't see it as an available block...a bit confused..

jccgsunyi’s picture

did u changed the name of the file before? try rename it

tomato’s picture

Why I can't find the block I set it left region but when I head to another page ,I can see nothing .where is the block? Please tell me why.

kaytortuga’s picture

Try moving the block to the region you want it, scroll down, click Save, THEN configure it. Otherwise, it won't save the position, even you do configure it the way you want it.

jkinsting’s picture

In the section entitled 'Install' the reference to 'the right directory on your site' as being...
sites/all/modules/onthissite

...should be...
sites/all/modules/onthisdate

Pogle’s picture

DIY

jkinsting’s picture

This is more of a comment on the actual Drupal UI than it is of this tutorial page, but....

In the 'Configure' section, regarding the line that reads...
"After saving the block region setting, you might also want to change the configuration ... so that the block is ... only visible to certain roles."

If you look at the corresponding Drupal UI, you will see a section entitled 'Show block for specific roles:', and in here are checkbox lines, one each for the roles of your configuration.

The problem I have with this UI itself relates to that last line of explanation, the one that reads:
"If you select no roles, the block will be visible to all users."

Specifically, this means that SELECTING ALL CHECKBOXES will be functionally equivalent to SELECTING NO CHECKBOXES.
A very awkward and confusing UI indeed.

Chris Einkauf’s picture

As I did on the previous book page, I edited this page to change the behavior for no results. Instead of completely omitting the block if there are no results, it now displays the message "Sorry No Results".

simonbcfa’s picture

Is anybody getting the code actually printed out when they go to save the configuration??

Do I need to turn on a setting or something??

chkumar’s picture

Hi,

I am not able to see the module listed in Administer » Site building » Modules, I have followed all the steps as per the procedure but no result.

Kumar

chkumar’s picture

Ok, I have found what is the problem ? I did a mistake I have saved the file as onthisdate.module.php which should have been saved as onthisdate.module file without .php extension.

I request the moderator to highlight this in the tutorial.

Prnb’s picture

I had the same problem as you. Thanks for posting this!
As chkumar mentioned this should be highlighted in the tutorial. The onthisdate.module file should NOT have a .php extension!

CORRECT: onthisdate.module

WRONG: onthisdate.module.php

harizon9’s picture

In the .info file

;$Id$
name = "On this date"
description = "This is the first module in the introduction"
core = 6.x

Manu The Great’s picture

Hi,

under Configure you wrote pate in stead of paste

Emmanuel Delay

rickyd1’s picture

I was receiving this error:
This version is incompatible with the version of Drupal core.

Initially, I was creating these files in Dreamweaver, and my module was not showing up. From experience in creating themes, I should have known not to use DW for editing the .info file. So, I created the file using TextEdit. It works for my themes. However, I was getting the incompatible error. On a hunch, I recreated the file with TextMate, and it worked for me.

Just thought I would share.

Rickyd

daniellarsennz’s picture

Module installs OK.
Can't see new block in Blocks list.
How can I debug to prove that onthisdate_block() is being called?

rondog469’s picture

I cant see it in the block list either. I can see it in the list of modules however and it is enabled. What gives?

Urme’s picture

I had the same problem, the misstake I did was putting all code in the onthisdate.info, instead of onthisdate.module :)

joshisumitnet’s picture

Dear Friends,

I am not able to see onthisdate module name in admin/build/block page.
Why?

Please help me out.

I had used Netbeans, then after reading one of friend's suggestion(here is he) that because of CRLF, I had used TextMate. Still not seeing the module name on admin/build/block page.

I am able to see it on Modules page and it shows that it is installed successfully. But now showing on admin/build/block page.

Please help me out.

Thanks in advanced.
Sumit Joshi

davisjan’s picture

I'm debugging the same problem right now. If you want to do simple printf-type debugging, you can use print or print_r and the result will show up at the very top of the page above the header. For example:

function residence_views_block($op = 'list', $delta = 0, $edit = array()) {
  // The $op parameter determines what piece of information is being requested.
  print('HERE');
  switch ($op) {
    case 'list':
      $blocks[0] = array(
...
davisjan’s picture

I found that changing $delta from an integer to a string caused the blocks to show up. Be sure to replace 0/1 with strings everywhere you are comparing to $delta.

function residence_views_block($op = 'list', $delta = 'my', $edit = array()) {
  // The $op parameter determines what piece of information is being requested.
  switch ($op) {
    case 'list':
      $blocks['my'] = array(
        'info'       => t('My Residence'),
...

I was inspired by this comment.

akobashikawa’s picture

To try this example, I follow a baby step approach, putting a file and testing, writing a function and testing, etc.

I think this way is more convenient to learn.

I put .info and .module in modules/ then test if I can view.
I write hook_perm then test it.
I write hook_help then test it.
I write hook_block with trivial echo word display then test it.
I enhance hook_block step by step until desired result.

hmahajan’s picture

This version is not compatible with Drupal 7.x and should be replaced.

----------------------------------------
Contents of my onthisdate.info :
---------------------------------------
; $Id$
name = "On that date"
description = "Shows nodes from last week for on that date"
core = 7.x
version = "1.0.1"

--------------------------------------------
Contents of my onthisdate.module
---------------------------------------------
<?php
/**
* Display help and module information
* @param path which path of the site we're displaying help
* @param arg array that holds the current path as would be returned from arg() function
* @return help text for the path
*/
function onthisdate_help($path, $arg) {
$output = ''; //declare your output variable
switch ($path) {
case "admin/help#onthisdate":
$output = '

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

';
break;
}
return $output;
} // function onthisdate_help

/**
* Valid permissions for this module
* @return array An array of valid permissions for the onthisdate module
*/
function onthisdate_perm() {
return array('access onthisdate content');
} // function onthisdate_perm()

/**
* Generate HTML for the onthisdate block
* @param op the operation from the URL
* @param delta offset
* @returns block HTML
*/
function onthisdate_block($op='list', $delta=0) {

if ($op == "list") {
// Generate listing of blocks from this module, for the admin/block page
$block = array();
$block[0]["info"] = t('On This Date');
return $block;
}//if ($op == "list")
else if ($op == 'view') {

// Generate our block content

// Get today's date
$today = getdate();

// calculate midnight one week ago
$start_time = mktime(0, 0, 0,
$today['mon'], ($today['mday'] - 7), $today['year']);

// we want items that occur only on the day in question, so
// calculate 1 day
$end_time = time(); // get all posts from one week ago to the present
// 60 * 60 * 24 = 86400 seconds in a day

$query = "SELECT nid, title, created FROM " .
"{node} WHERE created >= '%d' " .
" AND created <= '%d'";

$query_result = db_query($query, $start_time, $end_time);

// content variable that will be returned for display
$block_content = '';
while ($links = db_fetch_object($query_result)) {
$block_content .= l($links->title, 'node/'. $links->nid) .'
';
}//while loop

// check to see if there was any content before returning
// the block view
if ($block_content == '') {
// no content from a week ago
$block['subject'] = 'On This Date';
$block['content'] = 'Sorry No Content';
return $block;
}//if ($block_content == '')

// set up the block
$block = array();
$block['subject'] = 'On This Date';
$block['content'] = $block_content;
return $block;
}//else if ($op == 'view')

}

-------------------------------------------------------------------------------------------------------
I tried both, core=6.x as well as core =7.x. However, I still get the error message.

hmahajan’s picture

The corrected .info file is as follows:
------------------------------------------
; $Id$
name = "On this date"
description = "Shows nodes from last week for on that date"
core = 7.x
files[] = onthisdate.module

It worked for me after adding the line : files[] = onthisdate.module.

The solution was inspired from http://drupal.org/node/542202

Jonathan Baltazar’s picture

The Block API has changed in Drupal 7 so that the hook functions are specific. You have to separate the onthisdate_block() function into onthisdate_block_info() and onthisdate_block_view(): http://api.drupal.org/api/drupal/modules--block--block.api.php/7

You also need to change the database query section to comply with Drupal 7: http://api.drupal.org/api/drupal/includes--database--database.inc/functi...


function onthisdate_block_info() {
    $block = array();
    $block['info']['info'] = t('On This Date');
    return $block;
}

function onthisdate_block_view($delta='') {
    // Code from the example inside the body of "if($op == 'view')"
    // with Drupal 7 style DB calls.
}

urlstwohim3’s picture

I would say the main problem with nothing displaying for most new users is the permissions settings.
I just happened accross it and since this is just a learning experience for me I give permissions to everyone. After I have more time learning Drupal I will eventually change the settings so my site will be more secure.Go to User Management->Permissions and check all the boxes!

dineshweb’s picture

its work on my site and there is not any problam ..
you can give a right permission and it work well..
only you copy this code an dpaste on the created file and enable it and i think this will work well..

dinesh pal

Pav’s picture

This is a great tutorial many thanks! If it's not showing up just read the comments about CRLF in the earlier parts of the tutorial http://drupal.org/node/206756.

Many thanks to the creators and maintainers

aruhdom’s picture

warning: file_get_contents(sites/all/modules/onthisdate/onthisdate.info) [function.file-get-contents]: failed to open stream: Permission denied in /var/www/html/web/includes/common.inc on line 3568.

I am getting this error message.can anyone help regarding this?

Pav’s picture

If you're still stuck with this warning I would need more info to be able to help.
Go to admin -> reports and have a look for detailed explanation of the warning. That should give you some more clues hopefully.

Can you see your module?
Have you got all the relevant permission to the onthisdate folder?
If I can remember it well I was getting similar issues but the above but the module was not showing up at all. This was caused by the CR LF as described here: http://drupal.org/node/206756#comment-3337510

Pav

rotinpain’s picture

My drupal is one day old and the module just says "nothing to show".

For a quick fix to have the module show something, I just changed the end_time value :

replace :
$end_time = $start_time + 86400;

with :
$end_time = time();

Anyway, thanks for the cool tutorial !!

kulky’s picture

When I try to use the module it work but with a lot of warning. is it right?
I write under the warning:
* warning: Attempt to assign property of non-object in /membri/calcagnoprof/portale/includes/common.inc on line 3479.
* warning: Attempt to assign property of non-object in /membri/calcagnoprof/portale/includes/common.inc on line 3479.
* warning: Attempt to assign property of non-object in /membri/calcagnoprof/portale/includes/common.inc on line 3479.
* warning: Attempt to assign property of non-object in /membri/calcagnoprof/portale/includes/common.inc on line 3479.
* warning: Attempt to assign property of non-object in /membri/calcagnoprof/portale/includes/common.inc on line 3479.
* warning: Attempt to assign property of non-object in /membri/calcagnoprof/portale/includes/common.inc on line 3479.
* warning: Attempt to assign property of non-object in /membri/calcagnoprof/portale/includes/common.inc on line 3479.
* warning: Attempt to assign property of non-object in /membri/calcagnoprof/portale/includes/common.inc on line 3479.
* warning: Attempt to assign property of non-object in /membri/calcagnoprof/portale/includes/common.inc on line 3479.
* warning: Attempt to assign property of non-object in /membri/calcagnoprof/portale/includes/common.inc on line 3479.
* warning: Attempt to assign property of non-object in /membri/calcagnoprof/portale/includes/common.inc on line 3479.
* user warning: Duplicate entry '0' for key 'tmd' query: INSERT INTO avdrupal_blocks () VALUES () in /membri/calcagnoprof/portale/includes/common.inc on line 3528.
* warning: Attempt to assign property of non-object in /membri/calcagnoprof/portale/includes/common.inc on line 3479.
* warning: Attempt to assign property of non-object in /membri/calcagnoprof/portale/includes/common.inc on line 3479.
* warning: Attempt to assign property of non-object in /membri/calcagnoprof/portale/includes/common.inc on line 3479.
* warning: Attempt to assign property of non-object in /membri/calcagnoprof/portale/includes/common.inc on line 3479.
* warning: Attempt to assign property of non-object in /membri/calcagnoprof/portale/includes/common.inc on line 3479.
* warning: Attempt to assign property of non-object in /membri/calcagnoprof/portale/includes/common.inc on line 3479.
* warning: Attempt to assign property of non-object in /membri/calcagnoprof/portale/includes/common.inc on line 3479.
* warning: Attempt to assign property of non-object in /membri/calcagnoprof/portale/includes/common.inc on line 3479.
* warning: Attempt to assign property of non-object in /membri/calcagnoprof/portale/includes/common.inc on line 3479.
* warning: Attempt to assign property of non-object in /membri/calcagnoprof/portale/includes/common.inc on line 3479.
* warning: Attempt to assign property of non-object in /membri/calcagnoprof/portale/includes/common.inc on line 3479.
* user warning: Duplicate entry '0' for key 'tmd' query: INSERT INTO avdrupal_blocks () VALUES () in /membri/calcagnoprof/portale/includes/common.inc on line 3528.

somms’s picture

I got the same problem. It is not normal. Did you solved the problem?
If I find anything, I will post it here.

somms’s picture

I found the problem. In hook_block() you should be careful and not modify $block adding something that is not $block[0]['info'] in the case $op is "list".
In my code y have added $block['subject'] always at the end. That was the failure.

DrSeven’s picture

Hi, I've been following along with this tutorial trying to make a starting point for a module, and I keep breaking the site when ever I install the module's folder.

this is my entire .module file

/**
* Display help and module information
* @param path which path of the site we're displaying help
* @param arg array that holds the current path as would be returned from arg() function
* @return help text for the path
*/
function evaluationservices_help($path,$arg)
{
	$output = '';
	switch ($path)
	{
		case "admin/help#evaluationservices":
			$output = '

'.t('Grabs submitted evaluations').'

'; break; } return $output; } //function evaluationservices_help /** * Valid permissions for this module * @return array An array of valid permissions for the onthisdate module */ function evaluationservices_perm() { return array('access evaluationservices content', 'administer evaluationservices'); } // function evaluationservices_perm() function evaluationservices_block($op='list',$delta=0) { $block = array(); if($op == "list") { $block[0]['info'] = t('Evaluation Services'); } else if($op=='view') { $block_content=''; if($block_content=='') { $block['content']=t('All empty'); } else { $block['content']=$block_content; } } return block }

note, I don't have the "?>" in my actual file.

Amir Ati Design’s picture

Ok I've created the 100%-width block with my code. Now I need to place it in a page that contains only it (no sidebar, 60%-width page.. but of course default header, footer and template stuff). How do I do?

Thanks

prajila.v.p’s picture

hi
i created a module and it working fine. But the problem is views gives an error message(javascript error) while adding new fields or something. i used jquery .js in my module. This causes the problem.
i used
function drag_init() {
drupal_add_js(drupal_get_path('module', 'drag') . '/js/jquery.js'); in hok_init function
}
I want to enable the javascript only on my module page
ie. http:site.com/admin/user/drag

How can i do this
Pls help me...

Prajila

miwelillo’s picture

Just tried the example but I can't see the block even with the "no content" piece of code. The main problem is that, after enabling the module, I can't see it on the "Structure > Blocks" section in order to place it.

Any help would be very apreciated.