0
votes

I've tried the simplest examples of using hook_menu() posted here and on other Drupal forms and noting seems to work. My code, in: /sites/themes/mytheme/mymodule.module, is as follows:

<?php
function helloworld_menu() {
    $items = array();
    $items['hello'] = array(
        'title' => 'Hello world!',
        'type' => MENU_CALLBACK,
        'page callback' => 'helloworld_page',
        'access callback' => TRUE,
    );
    return $items;
}

function helloworld_page() {
    return 'Hello world !';
}

When I navigate to www.mydomain.com/hello I get a 404 error. I've tried enabling and disabling the module along with clearing the cache numerous times with no luck still. Here is some additional information about my environment:

  • Running Drupal Commerce version 7.22
  • I've enabled clean URLS and pathauto module

The end goal I'm trying to achieve is adding products to the cart with a link. I already have that part working so that I can pass product ID's into a function and add them to cart. I would be replacing helloworld_page() with my function and then changing $items['hello'] to $items['cart/add/%/%'], having two wildcards (product ID and quantity).

1

1 Answers

2
votes

For a hook declaration like hook_menu, the function name should be like <your_module_name_here>_menu

This is where you are going wrong.

Your module name is mymodule.module so your hook_menu should be called, mymodule_menu

<?php
/**
 * Implements hook_menu().
 */
function mymodule_menu() {
    $items = array();
    $items['hello'] = array(
        'title' => 'Hello world!',
        'type' => MENU_CALLBACK,
        'page callback' => 'helloworld_page',
        'access callback' => TRUE,
    );
    return $items;
}

function helloworld_page() {
    return 'Hello world !';
}

Please correct the function name and clear your cache and try again.

ALso i noticed you put the module in an unconventional location.

Please move it to /sites/all/modules/custom/mymodule folder ( both mymodule.info and mymodule.module files ).