I am Drupal Newbie but an experienced programmer (alas Microsoft-oriented - mostly ASP Classic).

I copied a module example that does nothing more than display "hello World" back to the screen.

Now what must sound like a stupid question; how do I invoke it. In other words, how do I create a menu item or have a page invoke this module so it will do its thing and display "hello World"? I have placed the module contents in /drupal/sites/all/modules/helloworld and enabled it in Drupal. But I don't know how one gets Drupal to actually invoke the module.

Once I have this module working it is my intention to expand it to do something useful.

Thanks in advance for any assistance.

Comments

nevets’s picture

Does it implement hook_menu(), if so there is probably a path to use.

bg1’s picture

Thanks for the response.

We are following example given at http://drupal.org/node/470070.

The author wrote: Some extra items in hook_menu() are removed, since the default is adequate.

Below are the small files we are using. The bigger question, however, is how does one connect a module to anything. Until now, the modules I run have to do with defining content that gets put in the database and then these are retrieved via a view or page or whatever. How does one invoke a module otherwise?

Thanks again.

Bill

***** Info file is as follows:

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

***** Module file is as follows:

// $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';
}

***** theme directory has file: page-helloworld.tpl.php with this in it:

print $content;
nevets’s picture

So if your site URL was http://www.example.com you would visit http://www.example.com/helloworld.

bg1’s picture

When I do that I get the following error message:

Page not found
warning: Cannot modify header information - headers already sent by (output started at /opt/lampp/htdocs/drupal/sites/all/modules/helloworld/helloworld.module:23) in /opt/lampp/htdocs/drupal/includes/common.inc on line 148.
The requested page could not be found.

Here is exactly what I did:

I created files to match the above:
helloworld.module in folder: htdocs/drupal/sites/all/modules/helloworld
helloworld.info in same folder

I don't know if it is needed, but I changed the owner for the helloworld folder from root to 1080 and also gave all permissions.

drwxrwxrwx 2 1080 1080 4096 2009-12-13 10:54 helloworld

I also change the owner and group for both files to 1080, but I left the permissions the way they were because I saw the modules in other folders also had those permissions.

-rw-r--r-- 1 1080 1080 74 2009-12-13 10:53 helloworld.info
-rw-r--r-- 1 1080 1080 450 2009-12-13 10:53 helloworld.module

I enabled the module in drupal (and saved the configuration).

I could not set any permissions in Drupal re. this module because when I go to Permissions under User Admin there is nothing referring to this module.
When I go to the administration by module page, the module is not listed.

when I try to run the module: localhost/drupal/helloworld I get the following:

Page not found
warning: Cannot modify header information - headers already sent by (output started at /opt/lampp/htdocs/drupal/sites/all/modules/helloworld/helloworld.module:23) in /opt/lampp/htdocs/drupal/includes/common.inc on line 148.
The requested page could not be found.

I did find that the person trying to implement this also reported that they initially had problems getting it to work, They reported that they got the message: "Access Denied page instead".

A little later they reported: "Access was probably denied because I had not mentioned any permissions for the module (using hook_perm()) and neither was I specifying any permissions in the hook_menu using 'access arguments'."

Any further suggestions?

Thanks in advance.

Bill

P.S. I am trying slightly different code:

This would be the code of helloworld.module:

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

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

  return $items;
}

function helloworld_page($name) {
  $output = '<p>'.t('Hello world!').'</p>';
  $output.= '<p>'.t('Hello').' '.check_plain($name).'!</p>';
  return $output;
}

Create another file called helloworld.info and put the following lines in it:
; $Id$
name = Hello World
description = Simplest module ever.
core = 6.x

Put those two files in a folder called 'helloworld'

jaypan’s picture

The hook_menu() function you showed us the first time is different from the one you showed us the second time. The path in the second one is helloworld/%. This means you need to go to a URL and add something after the hello/world.

Also you should try clearing your cache if you have made changes in the meantime. You can do this at admin/settings/performance.

Contact me to contract me for D7 -> D10/11 migrations.

bg1’s picture

Thanks! I think that takes care of that.

The broader question, however, is how would I create a menu item that would invoke this module?

I am assuming that the existence of a menu_hook has something to do with making this possible.

Thanks again.

Bill

nevets’s picture

hook_menu is part of a module and basically maps paths to callback functions (not modules). The key to array is the path.

bg1’s picture

Thanks for the response, but I don't know enough to follow.

When you say the menu_hook is part of a module, is that my module or some other module? If so, what is the exact name of that module?

If I wanted a menu item to invoke this module, could you please walk me through how I would make a menu item that would invoke this module? What admin function 9by name and path) do I use to create the menu item and how do I tie it to the module (please be very specific showing each entry to be made and describing what interface it is made in and the exact label of the entry on that interface.

Sorry to be so a pest, but things that sound so obvious to someone with experience are Latin to a newbie.

Once I have a module working from a menu item, I can start changing the module and rerunning it each time I make a change to see the impact.

Thanks for any assistance.

Bill

nevets’s picture

hook_menu and callback normally belong to the same module.

See Creating modules - a tutorial and more specifically Letting Drupal know about the new function

For details on the menu system see: Drupal menu system (Drupal 6.x)

jaypan’s picture

You are probably thinking of modules in a wordpress type manner, or another CMS, where only one given module may be being called at a time. But with Drupal, EVERYTHING is modules, even the core. So for any given page, there are multiple modules being called.

How Drupal works is that there are a number of hook functions. Hook functions are named according to a naming convention. When Drupal runs, it calls various hooks. When it calls these hooks, it runs through all the enabled modules on the site, and looks for a function named according to the naming conventions for the hook.

So for example, there is hook_menu(). This hook is called whenever a module is enabled, or when the cache is rebuilt (and potentially other times as well). This hook builds URLs.

As an example, lets pretend your module is named hello_world. You would have this function:

function hello_world_menu() // 'hook' is replaced with your module name. This means 'hook_menu' in your module is called 'hello_world_menu'
{
  $menu['hello_world'] = array // the URL now becomes 'hello_world'. So if your domain is example.com, the url being defined becomes example.com/hello_world
  (
    'page callback' => 'hello_world_callback_function',
    // I'm leaving the rest of the declarations out
  );
}

This function maps a URL to a function. The URL is, as explained in the code comment, 'hello_word', accessible at example.com/hello_world. The function it maps to is 'hello_world_callback_function.

So now we need to define that function:

function hello_world_callback_function()
{
  return 'Hello World';
}

So now when a user accesses example.com/hello_world, the function 'hello_world_callback_function' is called, and the return value of that function - Hello World - is printed to the screen.

Every/any module can call hook_menu, and so every/any module can define URLs and have callback functions mapped to said URL.

Contact me to contract me for D7 -> D10/11 migrations.

Korsbecker’s picture

Hi Bill

Did you get to work?

I have the same problem after trying about 10 different hello world examples i.e this one: http://www.semicolon.co.za/php/hello-world-module-in-drupal-6-part-1.htm.... They are all basically the same and follows the pattern explained by Jay Matwichuk above and all them says go to http://localhost/your_module. But for me none of them works which is extremely frustrating. I know all of you who answered mean well but I am also an experienced programmer and doing an example with only cut and paste coding should always work. None of the answers in this thread really helps which makes me think twice about using Drupal I'm sorry to say!

korsbecker

jaypan’s picture

So stop using it. No offense, but many, many other people have been able to make modules. It's not like it's not possible.

Anyways, the path you described is wrong. If you are an experienced programmer you should see that localhost/modulename isn't going to work. That skips the site itself within your localhost. On top of this, unless you happened to name a path in your module with the same name as your module, localhost/[site]/modulename will not exist either.

Contact me to contract me for D7 -> D10/11 migrations.