I am developing a custom module by name 'form' in drupal 7.
I have placed my module folder in sites\all\modules\form.
And i have 2 files in 'form' folder. 'form.info' and 'form.module'.
'form.info'
name = Form
description = Form creation.
core = 7.x
And 'form.module' contains
<?php
function form_menu()
{
$items['form/examples'] = array
(
'title' => 'Form API Examples',
'description' => 'Examples of using the Form API',
'page callback' => 'drupal_get_form',
'page arguments' => array('form_simple_form'),
'access callback' => TRUE
);
return $items;
}
function form_simple_form($form, &$form_submit)
{
$form['color'] = array
(
'#title' => t('Favorite Color'),
'#type' => 'textfield',
'#required' => TRUE,
'#description' => t('What is the favorite color?'),
);
$form['submit'] = array
(
'#type' => 'submit',
'#value' => 'Submit',
)
return $form;
}
I am getting only blank white page when i click on the link.
Thanks in advance.
I works for me try this code for mymodule.module
<?php
// $Id$
function form_menu()
{
$items['form/examples'] = array
(
'title' => 'Form API Examples',
'description' => 'Examples of using the Form API',
'page callback' => 'drupal_get_form',
'page arguments' => array('form_simple_form'),
'access callback' => TRUE
);
return $items;
}
function form_simple_form($form, &$form_submit)
{
$form['color'] = array
(
'#title' => t('Favorite Color'),
'#type' => 'textfield',
'#required' => TRUE,
'#description' => t('What is the favorite color?'),
);
$form['submit'] = array
(
'#type' => 'submit',
'#value' => 'Submit',
);
return $form;
}
When you create mymodule.info file
name = Form
description = Form creation.
core = 7.x
package = Form
Add package to it so you have your module in his own box. It is easier to find it betwen all of those modules.
Rename your module to form1, as it is conflicting with Drupal internal form module, located in /includes.
You forgot a semicolon here:
$form['submit'] = array
(
'#type' => 'submit',
'#value' => 'Submit',
); //<- semicolon missing
Related
I am trying to create some settings for a module and am following the guide on Drupal.org. I am also comparing my work to existing modules.
The config menu appears in the correct place with the correct fields but when I hit save no input is saved. (I have run cache clear and registry rebuild.)
Would anyone know what I am doing wrong here? I cannot see how my stuff differs.
On my .admin.inc file I have set the form as such:
function contact_page_settings() {
$form = array();
$config = contact_page_default_settings();
$form['#tree'] = TRUE;
$form['contact_page_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Top Section'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'top-title' => array(
'#type' => 'textfield',
'#title' => t('Top title'),
'#default_value' => !empty($config['top-title']) ? $config['top-title'] : '',
),
'top-left' => array(
'#type' => 'text_format',
'#title' => t('Top left'),
'#default_value' => !empty($config['top-left']) ? $config['top-left'] : '',
),
'top-right' => array(
'#type' => 'text_format',
'#title' => t('Top right'),
'#default_value' => !empty($config['top-right']) ? $config['top-right'] : '',
),
);
return system_settings_form($form);
}
On my .module file I have:
function contact_page_menu() {
$items = array();
$items['admin/settings/contact-page'] = array(
'title' => 'Contact Page content',
'page callback' => 'drupal_get_form',
'access arguments' => array('administer site configuration'),
'page arguments' => array('contact_page_settings'),
'type' => MENU_NORMAL_ITEM,
'file' => '/admin/contact_page.admin.inc',
);
return $items;
}
function contact_page_default_settings() {
$defaults = array(
'top-tile' => 'Top title',
'top-left' => 'Top left',
'top-right' => 'Top right',
);
$config = variable_get('contact_page_settings', array());
return array_merge($defaults, $config);
}
OK, so I found out the values were saving - just they were not appearing in my text areas when I reloaded the config page.
The problem was that I was using text_format fields which save data as an array. I needed to get the value property of that array as my default value.
So, I just added ['value'] to the default_value ternary expression:
'#default_value' => !empty($config['top-right']['value']) ? $config['top-right']['value'] : '',
I am having three fields: First name, Last name and Year of birth. In code I am easily inserting into database when I press submit button, so I want to update these fields when I want to click on Edit button. Please anyone help me to code this update to put the inserted data in a database or please post code.
<?php
function my_module_menu() {
$items = array();
$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'),
'description' => 'My form',
'type' => MENU_CALLBACK,
);
$items['my_module/edit'] = array(
'title' => t('Edit Name'),
'page callback' => 'drupal_get_form',
'page arguments' => array('my_module_edit'),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
function my_module_my_form($form_state) {
$form['first_name'] = array(
'#type' => 'textfield',
'#title' => t('First name'),
'#required' => TRUE, // Added
);
$form['last_name'] = array(
'#type' => 'textfield',
'#title' => t('Last name'),
'#required' => TRUE, // Added
);
$form['year_of_birth'] = array(
'#type' => 'textfield',
'#title' => ('Year of birth'),
'#description' => 'Format is "YYYY"',
);
// Adds a simple submit button that refreshes the form and clears its contents -- this is the default behavior for forms.
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
function my_module_edit($form, &$form_submit){
$form['Edit'] = array(
'#type' => 'Edit',
'#value' => 'Edit',
);
return $form;
}
function my_module_my_form_submit($form, &$form_state){
$firstName = $form_state['values']['fname'];
$lastName=$form_state['value']['lname'];
$yearofbirth = $form_state['values']['yearOfbirth'];
$query ="INSERT INTO `slideshow`.`mymoduledb`(`first_name`,`last_name`,`year_of_birth`) VALUES ('{$firstName}','{$lastName}','{$yearofbirth}')";
//$result=db_query($query);
if ($success = db_query($query)) {
// Tell the user that the employee has been saved.
drupal_set_message(t(' has been saved.'));
}else{ // If there's an error, $success will evaluate to FALSE, and the following code will execute.
drupal_set_message(t('There was an error saving your data. Please try again.'));
}
}
}
Inorder to edit form field you can reuse the form and set the default value field '#default_value'
storing the form submitted data fetched from database using the respective uniques ids.Refer the link for sample code.
https://www.drupal.org/node/2069383
I am a newbie to drupal. I am working to create a custom module in the name "course". I have read about the list of hooks to create a custom module. So, what's my problem is that i cant able to see the edit form for the module configure but i can do add management and its working fine.
I've used Existing contact modules as a reference.
Below are my code:
In course.module
function course_menu() {
$items['admin/structure/course'] = array(
'title' => 'Academy\'s courses',
'description' => 'Create a system contact form and set up categories for the form to use.',
'page callback' => 'course_list',
'access arguments' => array('administer contact forms'),
'file' => 'course.admin.inc',
);
$items['admin/structure/course/add'] = array(
'title' => 'Add Courses',
'page callback' => 'drupal_get_form',
'page arguments' => array('course_edit_form'),
'access arguments' => array('administer contact forms'),
'type' => MENU_LOCAL_ACTION,
'weight' => 1,
'file' => 'course.admin.inc',
);
$items['admin/structure/course/edit/%course'] = array(
'title' => 'Edit Courses',
'page callback' => 'drupal_get_form',
'page arguments' => array('course_edit_form',4),
'access arguments' => array('administer contact forms'),
'file' => 'course.admin.inc',
);
return $items;
}
and in course.admin.inc:
function course_edit_form($form, &$form_state, array $course = array()) {
// If this is a new course, add the default values.
$course += array(
'name' => '',
'course_id' => NULL,
);
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Course Name'),
'#maxlength' => 255,
'#default_value' => $course['name'],
'#description' => t("Example: 'Available Course Names'."),
'#required' => TRUE,
);
$form['course_id'] = array(
'#type' => 'value',
'#value' => $course['course_id'],
);
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
return $form;
}
function course_edit_form_submit($form, &$form_state) {
if (empty($form_state['values']['course_id'])) {
db_insert('courses') // Table name no longer needs {}
->fields(array(
'name' => $form_state['values']['name'],
'course_url' => $form_state['values']['name']
))
->execute();
//drupal_write_record('courses', $form_state['values']);
}
else {
db_insert('courses') // Table name no longer needs {}
->fields(array(
'name' => $form_state['values']['name'],
'course_url' => $form_state['values']['name']
))
->condition('course_id', $form_state['values']['course_id'])
->execute();
//drupal_write_record('courses', $form_state['values'], array('course_id'));
}
drupal_set_message(t('Course %name has been saved.', array('%name' => $form_state['values']['name'])));
watchdog('contact', 'Course %name has been saved.', array('%course' => $form_state['values']['name']), WATCHDOG_NOTICE, l(t('Edit'), 'admin/structure/course/edit/' . $form_state['values']['course_id']));
$form_state['redirect'] = 'admin/structure/course';
}
I can't able to find the exact problem, why add is working but not edit & delete? Help me on this.
Below image tells you what exactly i am asking for(Red mark).
If anything additional needed in this please let me know.
At A guess I'd say the edit route is not matching because of an issue with the course load function (not listed). Try dumping the result of that method. Is the course entity defined in your module? If so, then the convention is to prefix the variable name with the module name, and implement the appropriately names hook.
There's a better description of how this should work here: https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_menu/7
Jump to the "Auto-Loader Wildcards" section.
Try to clear cache /admin/config/development/performance
I'd like to make a drupal page with a form. Something like the following, which doesn't render:
function score_table_menu() {
$items['table'] = array(
'title' => t('name'),
'page arguments' => array('table_page'),
'page arguments' => array('table_form'),
'description' => t('score table'),
'type' => MENU_CALLBACK,
);
return $items;
}
function table_page(){
$output .= t('Complicated Hello');
$header = stuff;
$rows = stuff;
$output .= theme_table($header, $rows);
return $output;
}
function table_form(){
$stuff_array = array (values);
$form['choice']= array(
'#type' => 'select',
'#title' => t('Select Stuff'),
'#options' => $stuff_array,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
return $form;
}
function table_form_validate(){}
function table_form_submit(){
drupal_set_message(t('Submitted'));
}
Is it possible to have half the module render a page and the other half a form? I've written out two page arguments. I don't see any difference between page argument and callback. Also might need to use drupal_get_form() to render the form on the page.
page callback
is the function that you want this URI to trigger.
page arguments
are any arguments that you want to sent to that function when the callback is called.
Your menu item should look like this:
$items['table'] = array(
'title' => t('name'),
'page callback' => 'table_page',
'description' => t('score table'),
'type' => MENU_CALLBACK,
);
Or this:
$items['table'] = array(
'title' => t('name'),
'page callback' => 'drupal_get_form',
'page arguments' => array('table_form'),
'description' => t('score table'),
'type' => MENU_CALLBACK,
);
In your table_form, you can use other form types to display markup,
$form['table'] = array(
'#type' => 'markup',
'#markup' => theme_table($header, $rows),
);
Take a look in the API for a better understanding of hook_menu: http://api.drupal.org/api/drupal/developer%21hooks%21core.php/function/hook_menu/6
I am setting up a configuration page for a new module and I am having trouble getting my textfield to remember data I previously entered.
I am setting #default_value by calling variable_get but the value never gets reset in the textfield. It is however correctly stored as a variable and visible on the variable editor page.
Below is a code fragment:
$form = array();
$form['username'] = array(
'#type' => 'textfield',
'#title' => t('Username'),
'#default_value' => variable_get('username', '' ),
'#description' => t('supply the username'),
'#maxlength' => 30,
'#required' => TRUE,
'#weight' => -1,
);
return system_settings_form($form);
How can I fix this?
Updated code
The submit function is as follows:
function mymodule_settings_submit($form, &$form_state){
drupal_set_message('form submit responding : ' . $form_state['values']['username'] );
variable_set( 'username', $form_state['values']['username'] );
}
Updated code #2
The full code:
function my_module_init(){
}
function my_module_menu(){
$items = array();
$items['admin/settings/helper'] = array(
'title' => 'helper',
'page callback' => 'drupal_get_form',
'page arguments' => array('helper_setup'),
'access arguments' => array('access administration pages'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
function helper_setup(){
$node_types = node_get_types('names');
$form = array();
$form['username'] = array(
'#type' => 'textfield',
'#title' => t('Username'),
'#default_value' => variable_get('username', '' ),
'#description' => t('supply the username'),
'#maxlength' => 30,
'#required' => TRUE,
'#weight' => -1,
);
$form['#submit'][] = 'my_module_settings_submit';
return system_settings_form($form);
}
function my_module_validate($form, &$form_state){
die('validation area responding!');
}
function my_module_settings_submit($form, &$form_state){
drupal_set_message('form submit responding : ' . $form_state['values']['username'] );
variable_set( 'username', $form_state['values']['username'] );
}
in your form submit function you should insert variable_set('variableName' , 'value') variables is not inserted automatically..
UPDATE:
$form['username'] = array(
'#type' => 'textfield',
'#title' => t('Username'),
'#default_value' => variable_get('username', '' ),
'#description' => t('supply the username'),
'#maxlength' => 30,
'#required' => TRUE,
'#weight' => -1,
);
$form['#submit'][] = '_myform_submit_function' ;
return system_settings_form($form);
now lets build your submit function
function _myform_submit_function($form,&$form_state ) {
variable_set('username', $form_state['values']['username']) ;
drupal_set_message('form submit responding : ' . $form_state['values']['username'] );
}
now clear the cache and tell me if its working or not