Problem: I am requesting complete code for a "helloworld" module in Drupal 6 (D 7 optional, if you can do that also, fine).

Specification:
* The module shall be a standard Drupal module.
* When the user navigates to [ http://mydrupalsiteexample.com/helloworld ] the module shall output "helloworld" *and nothing else*
* The content of "View Source" in the browser shall be *completely blank* with no theme, no blocks, no css, no comments, no nothing; just a white screen with "helloworld"
* The module shall be a member of the HelloWorldTest package
* The module shall be coded with the fewest lines possible to accomplish this output
* The module shall be commented
* The module shall explain if any "tricks" were used that are not part of the standard Drupal API
* The module shall be released to the public domain

Offer:
Name your price. If you do it for free I will offer a trade or a donation to your Amazon.com account or something similar.

Disclaimer:
This is not a joke. This is a simple request in hopes to see the different ways people would accomplish this in Drupal. It is also a starting point to test out what kind of services can be purchased here in comparison to elsewhere. Depending on the result, there may be future requests for more advanced services. Thank you.

Comments

marcvangend’s picture

That's a minor adjustment of something I did before on http://drupal.org/node/354910#comment-1186793.

Module code (save in folder 'helloworld' as 'helloworld.module'):

<?php
; $Id$

/* implementation of hook_menu() */
function helloworld_menu(){
  $items = array();

  $items['helloworld'] = array(
    'title' => t('Hello world'),
    'page callback' => 'helloworld_page',
    'page arguments' => array(1),
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );

  return $items;
}

/* generate output and stop executing other Drupal code */
function helloworld_page($name) {
  print "helloworld";
  die();
}

Info file (save in folder 'helloworld' as 'helloworld.info'):

; $Id$
name = Hello World
description = Simplest module ever.
core = 6.x
package = HelloWorldTest

I don't need to be paid, please donate whatever it's worth to you to the Drupal Association: http://association.drupal.org/civicrm/contribute/transact

dreftymac’s picture

1) that link you gave me to association.drupal.org does not seem to work, the result is ...

    Sorry. A non-recoverable error has occurred.
    We could not find contribution details for your request. Please try your request again.
   Return to home page.

2) Thanks for your reply, I must admit I never considered simply exiting from php. Do you
know of a way to accomplish this *without* simply shutting down php entirely?

su8lime’s picture

Good job on the helloworld module.. that is exactly what I would have done as well.

Based on your request of outputting a page with no theme applied to it... this is the clean way to do it.

Some web applications need an output page referenced by a url that doesnt return any theme data similar to this and this is always how we did it.

This is the link to the Donate button on a.d.o
http://association.drupal.org/civicrm/contribute/transact?reset=1&id=8

It would be nice to turn this into a module in the library maybe?

Best regards,

_Dan

syedz’s picture

I feel like an idiot for asking this, but how do you test to see this module work? Noob here.
Thanks,

Zee

marcvangend’s picture

1. create a folder 'helloworld' in sites/all/modules
2. create the helloworld.module file
3. create the helloworld.info file
4. visit yoursite.com/admin/build/modules on your site and enable the module
5. visit yoursite.com/helloworld

syedz’s picture

Thanks got it to work. But why isn't the menu appearing in the site configuration? Unless I'm doing something completely wrong.
Thanks,

Zee

marcvangend’s picture

It doesn't appear in the site navigation. This module is meant to be the simplest module possible. Therefore it does not publish a visible menu item. The hook_menu uses 'type' => MENU_CALLBACK, which means that is just links a path to a function, nothing more.

kbahey’s picture

Here is an improved version.

The info file looks like this:

; $Id$
name = helloworld
description = Hello World module
package = HelloWorldTest
core = 6.x

Some extra items in hook_menu() are removed, since the default is adequate. Also proper HTTP headers are sent.

There are pros and cons when exiting early. Pros are less load on the server, but cons include cleanup and housekeeping tasks are skipped. For example, hook_exit() does not fire, so you don't get statistics, session updating and other things.

So, it is better to do something like this:

// $Id$

/*
 * Implementation of hook_menu()
 */
function helloworld_menu(){
  $items = array();

  $items['helloworld'] = array(
    'title'            => t('Hello world'),
    'page callback'    => 'helloworld_output',
    'access arguments' => array('access content'),
  );

  return $items;
}

/*
 * Display output
 */
function helloworld_output() {
  header('Content-type: text/plain; charset=UTF-8');
  header('Content-Disposition: inline');
  return 'helloworld';
}

Then in your theme directory create a file called page-helloworld.tpl.php with this in it:

print $content;

That is it! That theme file will be called for that path, and it has no HTML in it, so only what is returned from the function is displayed!

Here is where you can donate to the Drupal project please.

--
Drupal performance tuning and optimization, hosting, development, and consulting: 2bits.com, Inc. and Twitter at: @2bits
Personal blog: Ba

marcvangend’s picture

Thanks Khalid for your more sophisticated version, good point about the headers and hook_exit.

I think it's good to mention that the print-and-exit method is usually used when you want Drupal to return something like XML or json. This method is also used in the Drupal core, for instance in system_date_time_lookup, however it's nicer to run it through drupal_json(), like user_autocomplete does. (By the way, note that drupal_json does not explicitly exit... I wonder what happens when a menu callback does not return anything, but doesn't exit either.)