0

I'm new to drupal, and especially new to drupal form API. I see all the documentation which seems to make sense. They miss one key instruction that you need to create .module / .info files. I have created those, and copied the code from into my module Form example code drupal 6 However, I don't know how to view the module.

I know that if I can get a working example, I will be fine, but I can't. I am lost, and no matter how far I search all the guides miss the early steps, or make too much of an assumption.

test_form.info

name = Form example
description = My form example form API.
core = 6.x
package = Example form

test_form.module

function MYMODULE_menu() {
  $items['some/form'] = array(
    'title' => 'Some Form',
    'access arguments' => array('access content'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('MYMODULE_some_form'),
  );

  return $items;
}

function MYMODULE_some_form($form_state) {
  $form['item'] = array(
    '#type' => 'textfield',
    '#title' => t('Title'),
    '#required' => TRUE,
    '#default_value' => 'Default...',
  );

  return $form;
}
12
  • What you're describing there isn't part of the form API, it's part of the menu API. Which part are you actually having trouble with? Installing the module? Visiting one of the paths you've defined? What happens when you visit examples/form_example/tutorial for example? Do you get an error? A 404? Have you defined the form_example_tutorial_1 callback and placed it in your module file? Sorry for the barrage of questions but with the limited code in your question any answer would just be a wild guess
    – Clive
    Jul 24, 2013 at 15:27
  • @Clive superstar drupal helper. This is truly where I am stuck. This is all new to me, well websites arn't but using the Form API and Drupal is. Yes I get 404. I want to get the example in the link above working. My module file Is the form above, This is where I am lost following the guides for the form API. Where to put the Form API Code. Sorry for the basic understanding
    – Tom
    Jul 24, 2013 at 15:33
  • I can not find anything that says ok start with this basic example, you need to put this here, that there, and there you have a basic form working. Once I have that I can understand what I need to do to develop the forms further. :-)
    – Tom
    Jul 24, 2013 at 15:35
  • 1
    No apology necessary, we've all been there. You'll need a function in your module file with the following signature (for the first example) function form_example_tutorial_1($form, &$form_state) {. That will contain the code to define you form (remember to return $form at the end). Then it's just a matter of clearing Drupal's caches, because menu paths (or router items as they're sometimes known) are cached for performance reasons. After that it should work, in theory. If it doesn't, isolate the smallest bit of code you can that you think should work and post that here :)
    – Clive
    Jul 24, 2013 at 15:37
  • 1
    Ah, yeah that would've been good to know ;) With clean URLs off all paths go like this: http://mysite.com/index.php?q=some/path
    – Clive
    Jul 24, 2013 at 16:29

3 Answers 3

1

Rather than keep this going in the comments, here's a very basic form/menu example (un-tested but it should work).

function MYMODULE_menu() {
  $items['some/form'] = array(
    'title' => 'Some Form',
    'access arguments' => array('access content'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('MYMODULE_some_form'),
  );

  return $items;
}

function MYMODULE_some_form($form_state) {
  $form['item'] = array(
    '#type' => 'textfield',
    '#title' => t('Title'),
    '#required' => TRUE,
    '#default_value' => 'Default...',
  );

  return $form;
}

All of the above goes in a single module file. Once you install the new module (or clear the caches if it's already installed), you should see a basic form at /some/form.

2
  • Thank you for helping me through this. I will edit the question, and make it idiot proof for anyone else new to the module / form api system. All working Merci
    – Tom
    Jul 24, 2013 at 17:09
  • No worries, glad you got it sorted. If you're going to add a solution it'd be great if you could do so as an answer (answering your own questions is encouraged). There might be a delay before you can do that though...
    – Clive
    Jul 24, 2013 at 17:11
1

I learnt by studying the Examples Module. Install the examples module (https://drupal.org/project/examples), enable form api example sub module in it. Follow the code.

API Documentation: https://api.drupal.org/api/examples/form_example%21form_example.module/group/form_example/7

A simple way to follow would be - look at the hook_menu function.

Look at the code here: http://drupalcode.org/project/examples.git/blob/HEAD:/form_example/form_example.module

The following code snippet builds the page at http://baseurl.com/examples/form_example - you can now follow along.

$items['examples/form_example'] = array(
   'title' => 'Form Example',
   'page callback' => 'form_example_intro',
   'access callback' => TRUE,
   'expanded' => TRUE,
);

Another useful piece of information is the form api examples at: https://api.drupal.org/api/drupal/developer%21topics%21forms_api_reference.html/6

For Drupal 7, look at: https://api.drupal.org/api/drupal/developer%21topics%21forms_api_reference.html/7

Hope this helps.

3
  • Hi i tried the example module but when i enable it nothing loads in drupal so i had to disable it
    – Tom
    Jul 24, 2013 at 15:50
  • you mean the site does not load? Try enabling only the form example module, you dont need to enable all the modules.
    – drupalmav
    Jul 24, 2013 at 16:06
  • Yes I get a white screen. When I enable the form module. I downloaded the examples for developers drupal 6. But if I remove the module from the module folder drupal loads fine
    – Tom
    Jul 24, 2013 at 16:16
1

I was totally new to drupal, drupal modules (creating) and the form API.

Here is the basics to getting a simple form loaded with the menu link. Note the credits go to @Clive.

First of all I have a module in the module folder as follows.

formpractice - folder

--MYMODULE.info

name = My Test Form

description = A very simple Test Form to get you up and running.

core = 7.x

--MYMODULE.module (It is important that the MYMODULE_menu = the module name. So in this case at the module is called MYMODULE thats what is here replacing the hook_menu. This was my main reason for my module not loading the simple form. I had called my module something different to what the hook_menu was utilising.Then from here the form is the 2nd function, that can be further developed using the API instructions. I hope this helps any new users to getting started with the FORM API.

<?php function MYMODULE_menu() {
$items['some/form'] = array(
'title' => 'Some Form',
'access arguments' => array('access content'),
'page callback' => 'drupal_get_form',
'page arguments' => array('MYMODULE_some_form'),
);

 return $items;
}

function MYMODULE_some_form($form_state) {
 $form['item'] = array(
'#type' => 'textfield',
'#title' => t('Title'),
'#required' => TRUE,
'#default_value' => 'Default...',
 );

 return $form;
}

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.