Last updated November 5, 2012. Created by bekasu on February 18, 2010.
Edited by densolis, drupalshrek, confiq, Hethrir. Log in to edit this page.
Code sample #1:
This is a very basic form which will be expanded upon in the upcoming code samples.
<?php
/**
* This function defines the URL to the page created etc.
* See <a href="http://api.drupal.org/api/function/hook_menu/6
" title="http://api.drupal.org/api/function/hook_menu/6
" rel="nofollow">http://api.drupal.org/api/function/hook_menu/6
</a> */
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;
}
?>Note: Do not include a last PHP block endings "?>" in modules, as it will "break" Drupal. See: Drupal coding standards
Remember:In order to access your page, you must type the following in the browser address bar: http://yoursite_site_url/?q=my_module/form or http://yoursite_site_url/my_module/form depending on your configuration.
If you do not do this, you will not be able to access your form!
Comments
Does not work
I have tried and tried but cannot get the My module to show up in modules.
Anyone else having the same problem?
I think you have to run
I think you have to run /update.php
Best regards,
Bill
Probably just a file
Probably just a file permissions issue. Make sure the owner:group setting for the module directory and files are assigned to the webserver process.
this ABSOLUTELY WILL SEVERLY DAMAGE DRUPAL!!!!!!
The terminal "?>" will cause many pages to cease displaying after submit-- it breaks Drupal.
For example, Administer/Modules where you need to go to activate this thing.
It should not be that easy to bust Drupal!!!
DO NOT USE A TERMINAL "?>" IN MODULES!!!
(I'm running Drupal 6.)
droopy dopey drupal dooer
I had that and for me it was
I had that and for me it was because I added the ".php" extension to my module file where it should only be module_name.module and not module_name.module.php
Where did an argument come from?
I'm taking my first crack at creating a custom form and while the above code worked quite nicely, I'm a bit confused as to where an argument came from. In the function my_module_form() the comments say that this function calls drupal_get_form and takes the name of this form builder function as the argument. Now if I'm understanding that correctly, shouldn't the argument had been my_module_form, not my_module_my_form? And if not, could someone be kind enough to explain where that argument came from.
Thanks.
The example is correct. There
The example is correct.
There are two similarly-named functions in this example, which may be confusing: my_module_form and my_module_my_form.
drupal_get_form takes as an argument the name of the form-builder function, which is the function that defines the form elements. In this example that is my_module_my_form.
Autocomplete problem?
Hi everyone,
I love this tutorial, but I'm having problems using autocomplete with this example (using 6.17, MAMP server). Whenever the user types anything in, a pop-up window appears in the browser (checked on Safari, Firefox and Camino on the Mac) stating "An error occurred". Any idea why? I'd prefer to use autocomplete rather than, say, combo boxes, as they get too unwieldy.
Here's my code (similar to the above, with adjustments for autocomplete). At the moment, it just checks through users, but I want to be able to check through node titles eventually. I have tried to follow suggested solutions for fixing this problem (e.g. http://drupal.org/node/136998) but without success.
Cheers,
G
<?php
// This function defines the URL to the page created etc.
// See http://api.drupal.org/api/function/hook_menu/6
function staffproject_menu() {
$items = array();
$items['staffproject/form'] = array(
'title' => t('My form'),
'page callback' => 'staffproject_form',
'access arguments' => array('access content'),
'description' => t('My form'),
);
$items['staffproject/autocomplete'] = array(
'page callback' => 'staffproject_autocomplete',
'access arguments' => array('access content'),
);
return $items;
}
// This function gets called in the browser address bar for:
//"http://yourhost/staffproject/form" or
//"http://yourhost/?q=staffproject/form". It will generate// a page with //this form on it.
function staffproject_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('staffproject_my_form');
}
// This function is called the "form builder". It builds the form.
// Notice, it takes one argument, the $form_state
function staffproject_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'),
'#autocomplete_path' => 'staffproject/autocomplete',
);
return $form;
}
function staffproject_autocomplete($string) {
$matches = array();
$result = db_query_range("SELECT name FROM {users} WHERE LOWER(name) LIKE LOWER('%s%%')", $string, 0, 10);
while ($user = db_fetch_object($result)) {
$matches[$user->name] = check_plain($user->name);
}
print drupal_to_js($matches);
exit();
}
Working!
Typically, I got it working an hour or so after posting this! Isn't that the way?
I replaced:
print drupal_to_js($matches);with:
drupal_json($matches);Thanks in no small part to this forum post: http://drupal.org/node/274729
Might be useful for others. If this is wrong, please let me know!
i am lost, i created a module
i am lost, i created a module with these code.
i enabled the module and it works when i type mydomain.com/my_module/form
but i can't seem to find a link on navigation to this url, do i need to do something else?
try this...
Hi tchivoan1972,
with Drupal 7, I have the same problem. I resolve in this way:
/**
* 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' => t('My form'),
'description' => t('My form'),
'page callback' => 'drupal_get_form',
'page arguments' => array('my_module_form'),
'access arguments' => array('administer site configuration'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
... hope this help you...
Aiki
page arguments compulsory with Drupal 7
With Drupal 7, the following is required indeed to tell drupal_get_form what form_id to generate.
'page arguments' => array('form_id'),www.drupalfacile.org
thanks so much it is working
thanks so much it is working very well with drupal_json($matches);
error in the line- ' $items['my_module/form'] = array( '
kindly see below post >>
error in the line- ' $items['my_module/form'] = array( '
I am getting this irking error when I am trying this module with the basic form- (the 1st example)- in line no 5
'Parse error: syntax error, unexpected T_VARIABLE in -
C:\Documents and Settings\gjatin\My Documents\Dropbox\xampp\htdocs\drupal- 6.24-v1.1 sites\all\modules\my_module\my_module.module on line 8'
When I tried subsequent .module examples, drupal gives me error on the same corresponding line in that file.
When i try solving this kind of error by googling it, all soln i get is-there might be some punctuation missing like(;,{}() etc.),
but as i see the code it looks fine, I'm anyways directly cp-pasting it into the required file!! I am getting no idea why this error is coming. I'm not from php background.
anybody pleazz help !!
Regards,
Jatin
I created a basic page in
I created a basic page in content and copied this code sample #1 in the body of basic page (I have enabled PHP Filder module), but when I save the page, I get this error. Please help me to solve this error. I'm using drupal 7.
Fatal error: Cannot redeclare my_module_menu() (previously declared in /home/a8874647/public_html/modules/php/php.module(80) : eval()'d code:9) in /home/a8874647/public_html/modules/php/php.module(80) : eval()'d code on line 18