3
votes

I have installed drupal 7 and have been trying to create a custom form. The below code which am trying has been taken from http://drupal.org/node/717722 and I have not made any changes except for .info file.

here is the my_module.info

name = My module
description = Module for form api tutorial
core = 7.x

Below is the my_module.module

<?php

/**
* This function defines the URL to the page created etc.
* See http&#58;//api.drupal.org/api/function/hook_menu/6
*/
function my_module_menu() {
  $items = array();
  $items['my_module/form'] = array(
    'title' => t('My form'),
    'page callback' => 'my_module_form',
    'access arguments' => array('access content'),
    'description' => t('My form'),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
* This function gets called in the browser address bar for:
* "http://yourhost/my_module/form" or
* "http://yourhost/?q=my_module/form". It will generate
* a page with this form on it.
*/
function my_module_form() {

  // This form calls the form builder function via the
  // drupal_get_form() function which takes the name of this form builder
  // function as an argument. It returns the results to display the form.
  return drupal_get_form('my_module_my_form');

}

/**
* This function is called the "form builder". It builds the form.
* Notice, it takes one argument, the $form_state
*/
function my_module_my_form($form_state) {

// This is the first form element. It's a textfield with a label, "Name"
  $form['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Name'),
  );
  return $form;
}

?>

I have placed these two files in a *my_module* folder and placed it in sites/all/modules After that, I enabled the module from the modules page without any errors or warnings.

Now, when I try to access this for using the url, localhost/d7/?q=my_module/form

I get a "Page not found " error..!! Why..?? What am I missing..?

Its not only for this module but also for this examples for developers module http://drupal.org/project/examples. It shows the same error.

2
Did you clear the cache at admin/config/development/performance?nmc
Thank you so much...!! That's where I was going wrong....:)akshaynhegde

2 Answers

0
votes

You should write:

$items['my_module']

Where my_module is module name.
And you need to create page-my_module_my_form.tpl.php file at

sites/all/theme/your_theme/template/page-my_module_my_form.tpl.php

and in this file add code like this:

<?php

if (isset($form['submission_info']) || isset($form['navigation'])) {
    print drupal_render($form['navigation']);
    print drupal_render($form['submission_info']);
}

print drupal_render($form['submitted']);

?>

<?php print drupal_render_children($form); ?>

and try to run with

localhost/d7/my_module

I hope this will be useful to you

0
votes

I know this is late, but I do believe that you need to have the $form variable passed into your form, like so :
function my_module_my_form($form_state, $form)...
That way you actually have a form variable to house your form data.