1

I am beginner to Drupal 7. I am trying to start with Basic Form handling. I created a directory my_module in sites/all/modules. Created my_module.info file using notepad with the following contents

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

Then I created my_module.module file with the following contents

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


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;
}

But when I goes to module list and tries to enable the newly created module it is not being listed. Kindly help me to resolve this problem. I have gone through several techniques shown in different forums like deleting multiple copies of the files,providing permissions etc.But all in vain.

4
  • You are using the wrong version of hook_menu() for Drupal 7. That's the Drupal 6 one. Commented Dec 4, 2014 at 10:36
  • To display module, in module list mainly the .info file is responsible. There in no error in info file code check once again in other section of your module list(admin section). you will get it. After enable the module type your site url/my_module/form. you will able to see the form.
    – DRUPWAY
    Commented Dec 4, 2014 at 10:41
  • Have you created the my_module directory under sites/all/modules. Commented Dec 4, 2014 at 11:01
  • Yes that is the best place to put custom module. Otherwise you can create a custom folder inside your modules folder.
    – DRUPWAY
    Commented Dec 4, 2014 at 11:39

3 Answers 3

0

You should follow these steps. This may help you to better understand that how to create custom module. If you have any query then comment below.

Link

6
  • thank u for your response. I can follow the steps upto creation of .module file. Later as the module I have created is not listing i am not able to enable it. That's the problem i am facing from 2 days.
    – Ranjini
    Commented Dec 4, 2014 at 10:40
  • No problem, at the starting point these types of issues can occurred. Follow those steps you definitely got result.
    – Nishant
    Commented Dec 4, 2014 at 10:43
  • Thanks.I installed the module_filter and gave the package field in .info file. But the package name is not listing. All the other modules downloaded are listing only problem with the custom module –
    – Ranjini
    Commented Dec 4, 2014 at 10:53
  • No need to install any module. Have you follow those steps ?
    – Nishant
    Commented Dec 4, 2014 at 10:54
  • First you need to remove all those file & folders what you had already created. Then Clear Cache from admin & browser. Then Create a folder inside sites/all/modules i.e. custom. Inside folder create two files custom.info & custom.module. Inside .info file name = custom description = Custom module core = 7.x & inside custom.module just write <?php ?> & then enable.
    – Nishant
    Commented Dec 4, 2014 at 11:02
0

You can add this to your info file:

package = My custom modules

That will tell drupal to put your module in the fieldset My custom modules. Also if you have the module module_filter installed, the packages get their own tabs. It should be easier to find your custom module in the module list this way.

5
  • Thanks.I installed the module_filter and gave the package as above. But the package name is not listing. All the other modules downloaded are listing only problem with the custom module
    – Ranjini
    Commented Dec 4, 2014 at 10:51
  • no need of download any module. Please choose a simple & easy way so you can clear your basic concepts.
    – Nishant
    Commented Dec 4, 2014 at 10:53
  • Ya,but kindly suggest me the solution why it is not listing to make it enable plz.
    – Ranjini
    Commented Dec 4, 2014 at 10:55
  • Did you clear the cache?
    – Akko
    Commented Dec 4, 2014 at 10:58
  • Yes. I tried by clearing the cache too,but the module is not listing
    – Ranjini
    Commented Dec 4, 2014 at 11:01
0

First, check the my_module.info my_module.module files. Maybe these have a wrong file permissions ? Make sure you dont have file of the same name somewhere else in the folder structure, It could stay from your old 'tests'.

Second, for displaying forms in the way you want you must use drupal_render() or render()

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_render(drupal_get_form('my_module_my_form'));
}

In D7 drupal_get_form returns a renderable array, not HTML.

Another way, you can also get the form directly from the hook:

 $items['my_module/form'] = array(
   'title' => 'My form',
   'page callback' => 'drupal_get_form',
   'page arguments' => array('my_module_my_form'),
   'access arguments' => array('access content'),
 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.