Is there a really basic example - simple module or tutorial like an “Hello World” module?

One that says

Hello member

where the member is the name of the member and perhaps does a little php function to get the time or something?

Just something to get me started

Comments

havran’s picture

Very simple module example (no node type but only module which generate own page type after call own path). You need create directory hello_world in your modules directory and create file hello_world.module with this code inside... And enable hello_world.module in your module administration...

/* $Id$ */

/**
 * @file
 * Very simple DRUPAL module
 */

/**
 * Implementation of hook_help().
 */
function hello_world_help($section) {
  switch ($section) {
    case 'admin/help#hello_world':
      $output = '<p>Hello world help...</p>';
      return $output;
    case 'admin/modules#description':
      return 'Hello world module description...';
  }
}

/**
 * Implementation of hook_menu().
 */
function hello_world_menu($may_cache) {
  $items = array();

  if ($may_cache) {
  }
  else {
    $items[] = array(
      'path' => 'hello', // drupal path example.com/?q=hello
      'title' => 'Hello world page...', // page title
      'callback' => 'hello_world_page', // callback function name
      'access' => TRUE, // every user can look at generated page
      'type' => MENU_CALLBACK // define type of menu item as callback
    );
  }

  return $items;
}


/**
 * Function which generate page (this generate any content - you need only your own code...)
 */
function hello_world_page() {
  return '<p>Hello world!</p>';
}

Usage - just call ?q=hello on your DRUPAL page...

--
My blog - Havran's mini-blog
My first drupal site - http://www.enigma.sk/

--
My site - Svoji.SK
My first Drupal site - http://www.enigma.sk/

nomax5’s picture

wow did you just write that for me?

Thanks you Havran it's amazing how much I can work out from examples thats fantasic.
I am very new I've just discovered I can cut n paste my php into a story module - I didn't realise.

if you're still around and I could pick your brains a little...

what I usually do with php and forms is have a little bit of processing code that also does the confirm message

eg

getweb.php
<?php
Enter your website:
<form action="process.php" method="post">
<input type="text" name="web" value="" size="100" maxlength="250">	
<input class="button" type="submit" value="Save this " />
?>
process.php
<?php
// update a table etc. 

echo "<h3> thank you for entering your website</h3>";

Click here to go back.
 
?>

The problem I have is: now I've put my code into a drupal story what or where do I point the form?
and in process.php where do I go back to?

I know my code is not very advanced I just typed it in for example purposes.

havran’s picture

If you need create some forms in your page, you need study Drupal Form API - here is Quickstart guide - http://api.drupal.org/api/4.7/file/developer/topics/forms_api.html

You only need create array with your form elements and call

  $output = drupal_get_form('test_page', $form);
  return $output;

(Look at test_page() function - it is like hello_world_page() in my example).

How to validate and submit form - examples for this are in form quickstart guide too.

--
My blog - Havran's mini-blog
My first drupal site - http://www.enigma.sk/

--
My site - Svoji.SK
My first Drupal site - http://www.enigma.sk/

yelvington’s picture

Modules are not stories, nor are they blocks.

A module is a component of the underlying software. A story is a type of node (other types are blog, page, event, etc.) A block is an independent component that can be rendered at the same time as a node, or on overview pages.

Modules can define new node types, or they can directly provide content (such as a form) and interact with input through a GET or POST.

As you noted, Drupal does you paste PHP into a document (not a module) if you have permission. Typically you are pasting into the body of a node. So you can do what you ask, by pasting PHP into the body of a simple node type (such as page or story) and selecting the PHP filter, so your code will be evaluated.

Writing a module is a much better way.

In Havran's example, www.example.com/hello calls the function hello_world_page() to fire, since it is identified as a callback function in hello_world_menu().

There are examples in the developer documentation that provide details on how to do what you want. Click the link in the left rail corresponding to the version of Drupal that you use.

You can take shortcuts and paste PHP into documents but learning the "Drupal way" of doing things pays benefits in the long term.

nomax5’s picture

I am going to do all the things you say and read the stuff

But I am doing some strange things with my forms and I’m wondering if they’re a little to strange

For instance I read all records on a table for a user and display them in a form, with a radio button on each one to select that record. The value of the radio button holds the record id so I can then present that record for edit etc.

<table width="600" border="0" cellpadding="4" cellspacing="0">
<form action="process.php" method="post">

<?php
$link = mysql_connect('localhost', $username, $password) or die('Could not connect: ' . mysql_error());
mysql_select_db($database) or die('Could not select database');
$query = "SELECT * FROM kcell WHERE kcell_mbr = '$mbrid'";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
$v_num=mysql_numrows($result);

$v=0;
while ($v < $v_num) {
?>

 	<tr>
 	<td> <?php echo $record_data_here; ?></td>
 	<td> <INPUT TYPE=RADIO NAME="rec_id" VALUE="<?php echo $record_key; ?>">Select</td>
    	</tr>

<?php
$v++;
}
mysql_close;
?>

<tr>
<td>
<INPUT class="button" type="submit" value="Edit selected Cell" />
</form>
</td>
</tr>
</table	

Do you think it will be able to handle that sort of thing?
that was the only way I could think of passing the the relevant record id to process.php

pauly-1’s picture

in a module called example, in example _form():

        $questions = array ();
        $queryResult = db_query( "select * from {question}" );
        while (      $uRef = db_fetch_object( $queryResult )) {
                $questions[$uRef->nid] = $uRef->title;
        }

        $form['pid'] = array(
                '#type' => 'select',
                '#title' => t('pid'),
                '#default_value' => $node->pid,
                '#options' => $questions,
        );
michelle’s picture

Would you be willing to write this up for the handbook? Just find a likely spot and add a page. Adding some more comments aimed at total newbies would help as well.

Thanks,

Michelle

--------------------------------------
My site: http://shellmultimedia.com

havran’s picture

I'm not very good in english language. I give this oportunity someone with better ability in english. :)

--
My blog - Havran's mini-blog
My first drupal site - http://www.enigma.sk/

--
My site - Svoji.SK
My first Drupal site - http://www.enigma.sk/

michelle’s picture

Your English seems fine to me. Besides, grammar can always be corrected later. Even if you put the code you have and some text in the best English you can manage, that's a good start.

Michelle

--------------------------------------
My site: http://shellmultimedia.com

nomax5’s picture

I've been making total newbie notes, so I can write a total beginners guide.

I was thinking of perhaps a tutorial to developing small website.
something like a DVD collection website, with a form a table and couple of pages.
step by step.

once I know what I'm doing and am confident with the code.

Also Havran, your english is fine mate.

michelle’s picture

Entry level docs are always great. By the time most people get to writing docs, they've been around enough that it's hard to remember the newbie perspective well.

Michelle

--------------------------------------
My site: http://shellmultimedia.com

dfdavis’s picture

Thanks for your simple module example. I only got it to print anything though after I changed the last function to:

<?php function hello_world_page() {
  print '<p>Hello world!</p>';//print instead of return
}?>

But what I would really like to do is display the output within the themed page. I thought that could be achieved with the following function:

<?php function hello_world_page() {
  $output = '<p>Hello world!</p>';
  print theme('page', $output);
}?>

I have tried changing print to return without any success.
Any ideas?

bartl’s picture

It can be a very frustrating experience that sometimes it works, and sometimes you can get an "access denied" page, while you didn't change anything.

Digging deeper I found out the "access" entry in the array items in the returned array from the hook_menu is wrong (at least for Drupal 6). In the end, to determine whether you are granted access, the final boolean result is stored in the "access" entry, but any value you give it here doesn't make it into the database table menu_router, which is used at runtime, and so it is simply ignored. As a result, access is not granted — or it should be inherited from a higher level. (That's why it sometimes works anyway.)

Instead, you have to use the key "access callback", which is usually used for a function name for the callback; but if the value is a number (or true or false) no function is called; instead, this value is used directly as the result.

TL;DR: Replace

      'access' => TRUE, // every user can look at generated page

with

      'access callback' => TRUE, // every user can look at generated page

and Bob is a male relative.

Ajay Pal Singh Atwal’s picture

Please do add a hello_world.info file as well if using it in Drupal 5.x, I was unable to get a listing on the module enable/disable page without it

Contencts of hello_world.info
--------snip------
name = hello_world
description = This description would be listed on module enable/disable page.
package = Core - optional
version = 1.0
--------snip------

searaig’s picture

i've tried this out a bunch of times. however, i always get an access denied error in 6.x. when trying to access http://example.com/hello

what is minimum definition of a module? in terms of functions etc. like if we had to write the module by implementing an abstract class what's the signature?

would love to have that kind of definition of a module.

geolaw’s picture

check out the creating modules tutorial - a tad more indepth than this, but in the end, produces a working module and
help you understand how it all comes together

http://drupal.org/node/206753

kbahey’s picture

Another helloworld example for Drupal 6.

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

Kanaris’s picture

Hi,

You may use next example.
It is very easy for understanding.

Hello World for Drupal

joachim’s picture

See the page example module within http://drupal.org/project/examples