Installing, enabling and testing the module

Last modified: August 24, 2009 - 23:30

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/onthissite -- 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 pate 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.

why can I not see the block "On this date" on rightside?

wanneng - November 25, 2008 - 15:47

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

Re: why can I not see the block "On this date" on rightside?

MindStalker - December 9, 2008 - 03:55

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.

Need to return $block

gottsman - December 29, 2008 - 07:38

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

You can also just take the "-

iddqd - March 19, 2009 - 14:54

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.

return $block

bits - April 2, 2009 - 03:43

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

couple of tweaks

tzoscott - January 5, 2009 - 21:48

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

<?php
   
...
   
// 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.

The worse "thing"

invisible_swordsman - March 1, 2009 - 18:21

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'

The best "thing"

aikiken - March 4, 2009 - 18:05

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... :)

Aikiken,
No Wife, No Horse, No Mustache.

Well, although this might not

manop - March 12, 2009 - 15:21

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 :).

It was not apparent to me that "administrator" isn't me

swelljoe - April 7, 2009 - 07:02

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.

I don't see the block to add to the region

solidad - May 4, 2009 - 19:52

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..

did u changed the name of the file before?

jccgsunyi - June 25, 2009 - 04:21

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

Why I can't find the block I

tomato - May 6, 2009 - 08:00

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.

花谢花飞花满天,红綃香断有谁怜

Install section - incorrect pathname

jkinsting - August 19, 2009 - 18:35

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

Configure section - strange UI for the 'roles' seclection

jkinsting - August 19, 2009 - 19:00

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.

Just edited this page

Chris Einkauf - August 24, 2009 - 23:32

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".

output error

smonbcfa - September 29, 2009 - 04:56

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??

 
 

Drupal is a registered trademark of Dries Buytaert.