Custom module form problems - php

I am struggling in my attempts to programmatically display a form to display in my node-page view area. I have the following code in my "simplemodule".
function simplemodule_newcomer_form($form_state){
$form = array();
$form['simplemodule_newcomer']['name'] = array(
'#type' => 'textfield',
'#title' => t('name'),
'#description' => t(''),
'#weight' => -1,
);
$form['simplemodule_newcomer']['email'] = array(
'#title' => t('email'),
'#type' => 'textfield',
'#description' => t(''),
'#weight' => 0,
);
$form['simplemodule_newcomer']['phone'] = array(
'#title' => t('telephone No.'),
'#type' => 'textfield',
'#description' => t(''),
'#weight' => 0,
);
$form['submit_button'] = array(
'#type' => 'submit',
'#value' => 'enter',
);
return $form;
}
function simplemodule_newcomer_form_submit($form_id, &$form_state){
//dealing with submitted data
}
This code works but only from a defined link in my administration menu.
What I want to do is get the form to display and submit on a specific node in view mode. So it creates the effect that there is a form to fill when visiting the node.

You can implement hook_nodeapi() and attach your form using drupal_get_form():
function simplemodule_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
if ($node->nid == $the_nid && $op == 'view') {
$node->content['my_additional_field'] = array(
'#value' => drupal_get_form('simplemodule_newcomer_form'),
'#weight' => 10,
);
}
}
You can use the #weight key to specify where in relation to the other content on the page your form will appear. Also you'll need to clear Drupal's caches when you implement this hook to make sure it's picked up.

Surely you can use
hook_form_alter(&$form, &$form_state, $form_id) and
hook_nodeapi(&$node, $op, $teaser = NULL, $page = NULL)
http://api.drupal.org/api/drupal/modules--system--system.api.php/function/hook_form_alter/7
http://drupal.org/node/1011692

Related

Drupal 7: how do I save module settings?

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'] : '',

Drupal 6 Multistep Forms: form_state values empty on submit

I'm having trouble getting the values from a multistep form to insert into the database. All I'm getting is empty values for each field.
Here's the code:
function multistep_profile_menu() {
$items['multistep_profile'] = array(
'title' => t('Multistep Profile'),
'page callback' => 'drupal_get_form',
'page arguments' => array('multistep_profile'),
'type' => MENU_NORMAL_ITEM,
'access arguments' => array('access content'),
);
return $items;
}
function multistep_profile(&$form_state) {
// $form_state['storage']['step'] keeps track of what page we're on.
if (!isset($form_state['storage']['step'])) {
$form_state['storage']['step'] = 1;
}
//stores form data and carries it between steps
$default_value = '';
if (isset($form_state['storage']['values'][$form_state['storage']['step']])) {
$default_value = $form_state['storage']['values'][$form_state['storage']['step']];
}
switch ($form_state['storage']['step']) {
// Step 1 of Form
case 1:
$form['step_one'] = array(
'#title' => t('Step 1'),
'#type' => 'fieldset',
'#description' => t('Just getting started.'),
);
// Full Name
$form['step_one']['full_name'] = array(
'#title' => t('Full Name'),
'#type' => 'textfield',
'#description' => t('Please enter your name.'),
'#required' => TRUE,
'#default_value' => isset($default_value['full_name']) ? $default_value['full_name'] : '',
);
// College/University/Organization
$form['step_one']['college'] = array(
'#type' => 'textfield',
'#title' => t('College/University/Organization'),
'#default_value' => isset($default_value['college']) ? $default_value['college'] : '',
);
// Academic Disciplines
$form['step_one']['academic'] = array(
'#type' => 'textfield',
'#title' => t('Academic Disciplines'),
'#default_value' => isset($default_value['academic']) ? $default_value['academic'] : '',
);
break;
// Step 2 of Form
case 2:
$form['step_two'] = array(
'#title' => t('Step 2'),
'#type' => 'fieldset',
'#description' => t('Almost done.'),
);
// Job Title
$form['step_two']['job_title'] = array(
'#title' => t('Job Title'),
'#type' => 'textfield',
'#description' => t('Please enter your job title.'),
'#default_value' => isset($default_value['job_title']) ? $default_value['job_title'] : '',
);
// Department
$form['step_two']['department'] = array(
'#type' => 'textfield',
'#title' => t('Department'),
'#default_value' => isset($default_value['department']) ? $default_value['department'] : '',
);
break;
// Step 3 of Form
case 3:
$form['step_three'] = array(
'#title' => t('Step 3'),
'#type' => 'fieldset',
'#description' => t('Almost done, part 3.')
);
// Q1
$form['step_three']['q1'] = array(
'#title' => t('Why do you want to develop a globally networked course?'),
'#type' => 'textarea',
'#description' => t(''),
);
break;
//Confirmation
case 4:
$form['thanks'] = array(
'#value' => '<p>'. t('Your Profile is complete.') .'</p>',
);
break;
}
//programmatically display buttons
if ($form_state['storage']['step'] > 1) {
$form['previous'] = array(
'#type' => 'submit',
'#value' => t('<< Previous'),
);
}
if ($form_state['storage']['step'] != 4) {
$form['next'] = array(
'#type' => 'submit',
'#value' => t('Continue >>'),
);
}
else {
$form['finish'] = array(
'#type' => 'submit',
'#value' => t('Finish'),
);
}
return $form;
}
function multistep_profile_submit($form, &$form_state) {
//Save the values for the current step into the storage array.
$form_state['storage']['values'][$form_state['storage']['step']] = $form_state['values'];
//Check the button that was clicked and change the step.
if ($form_state['clicked_button']['#id'] == 'edit-previous') {
$form_state['storage']['step']--;
}
elseif ($form_state['clicked_button']['#id'] == 'edit-next') {
$form_state['storage']['step']++;
}
elseif ($form_state['clicked_button']['#id'] == 'edit-finish') {
//You should store the values from the form in the database here.
dpm($form_state);
// INSERT into DB
$full_name = $form_state['values']['full_name'];
$college = $form_state['values']['college'];
$academic = $form_state['values']['academic'];
$job_title = $form_state['values']['job_title'];
$department = $form_state['values']['department'];
$q1 = $form_state['values']['q1'];
db_query("INSERT INTO {multistep_profile} (full_name, college, academic, job_title, department, q1, created) VALUES
('%s', '%s', '%s', '%s', '%s', '%s', %d)", $full_name, $college, $academic, $job_title, $department, $q1, time());
drupal_set_message(t('Your profile has been saved.'));
unset($form_state['storage']);
//Go to this page after completing the form.
//$form_state['redirect'] = 'node';
}
}
The only value that makes it into the DB is the 'created' date timestamp.
I ran a DPM to check if the values are being carried over between form steps, and they are, I just can't figure out how to get them to insert into the database. Any help?
Looking at your submit function, you (correctly) store the values collected on the different steps under $form_state['storage']['values'][<step_number>]. This matches with the DPM output you posted, i.e. the values show up under $form_state['storage']['values'][1]..., $form_state['storage']['values'][2]..., etc.
But in your final submit processing, you try to access them under $form_state['values'][...], just as if this was a single step form. As your last step does not post any values, you will not find anything there.
So looks like you'd just need to change your final value collection logic to grab the values from the storage (sub)arrays where you have placed them before :)
E.g.:
$full_name = $form_state['values']['full_name'];
would need to be
$full_name = $form_state['storage']['values'][1]['full_name'];
and so on for the other values.

Drupal 7 FAPI- Adding form elements in validate or submit handlers

Is it possible to add additional form elements in the validate or submit functions in drupal 7 module? The following code works and image is displayed on the form:
function test1_form($form, &$form_state)
{
$form['image']=array(
'#markup'=>'<img src="sites/all/modules/create_ad/th.jpeg"><br/>', //replace with your own image path
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
}
but when I try to display image after submission in submit function like following, it doesn't work:
function test1_form($form, &$form_state)
{
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
}
function test1_form_submit($form,&$form_state)
{
$form['image']=array(
'#markup'=>'<img src="sites/all/modules/create_ad/th.jpeg"><br/>', //replace with your own image path
);
}
Any positive help is welcome. Thanks.
You could follow the multi-step form methodology to add additional fields to your form after submission.
Here is a blog post that talks about one approach for multi-step and can give you some insight.
Basically, on your submit function you store your values and set the form to be rebuilt. Then in your form function you check for those stored values and add the new fields if present.
Example:
<?php
function test1_form($form, &$form_state)
{
if (isset($form_state['storage']['show-image'])){
$form['image']=array(
'#markup'=>'<img src="sites/all/modules/create_ad/th.jpeg"><br/>', //replace with your own image path
);
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
}
function test1_form_submit($form,&$form_state)
{
$form_state['rebuild'] = TRUE;
$form_state['storage']['show-image'] = true;
}
Here is another way of doing this with Ajax. This way you will not get any page reload since only the image will be loaded in to the div with wrapper id.
Here is the code:
function test_menu()
{
$items = array();
$items['test'] = array(
'title' => 'test',
'page callback' => 'drupal_get_form',
'page arguments' => array('test1_form'),
'access callback' => array(TRUE),
'type' => MENU_CALLBACK,
);
return $items;
}
function test1_form($form, &$form_state)
{
$form['submit'] = array(
'#type' => 'button',
'#value' => t('Submit'),
'#name' => 'add',
'#ajax' => array(
'callback' => 'ajax_load_picture_callback',
'wrapper' => 'wrapper',
'method' => 'replace',
'effect' => 'fade',
),
);
$form['image'] = array(
'#markup' => '',
'#prefix' => '<div id="wrapper">',
'#suffix' => '</div>',
);
if (array_key_exists('triggering_element', $form_state) &&
array_key_exists('#name', $form_state['triggering_element']) &&
$form_state['triggering_element']['#name'] == 'add')
{
$form['image']['#markup'] ='<img src="themes/bartik/logo.png"><br/>'; //replace with your own image path
}
return $form;
}
function ajax_load_picture_callback($form, $form_state)
{
return $form['image'];
}
function test1_form_submit($form, &$form_state)
{
}

Drupal custom form remembering textfield data

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

Theming a form in Drupal which has to be a part of an Entity's Content

I've a form in my module (mymodule)...
function mymodule_form()
{
$form['mytext'] = array(
'#title' => 'Password',
'#type' => 'password',
'#size' => 10,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Cool!',
);
$form['cool_submit'] = array(
'#type' => 'submit',
'#value' => 'Cool Submit!',
);
return $form;
}
I've used the hook_entity_view hook to display this form under all drupal entities that are displayed.
function mymodule_entity_view($entity, $type, $view_mode, $langcode) {
$entity->content['myadditionalfield'] = mymodule_form();
}
When showing this form, drupal adds a DIV tag to the mytext (password field) by itself. I want to override this and provide my own DIV tags and theme to this form. How do I do it?
Thanks :-)
I worked further on this question to solve it. And the problem got solved. I changed the above code...
function mymodule_entity_view($entity, $type, $view_mode, $langcode) {
$element = array(
'start' => array(
'#type' => 'button',
'#name' => 'start',
'#button_type' => 'submit',
),
'my_text' => array(
'#type' => 'textfield',
'#size' => 30,
'#maxlength' => 50,
),
'my_submit' => array(
'#type' => 'button',
'#name' => 'Submit Discussion',
'#button_type' => 'submit',
),
);
$entity->content['disc_bar'] = $element;
}
And the problem kept creeping up whenever a textfield or a password field was rendered. I checked the type array in systems elements info function (a elements_info hook) and found that textfield and password fields come with a default value for the #theme-wrapper. I tried to override it by this way...
'my_text' => array(
'#type' => 'textfield',
'#size' => 30,
'#maxlength' => 50,
'#theme-wrapper' => '',
),
and It worked. Its not generating any additional division tags... :-)

Categories