I know it might be a very simple solution for this, but I am struggling to achieve it. I need to add a checked attribute in one of the two radio buttons of the same group.
Here is my code:
$options = array(
'yes'=>t('Yes'),
'no'=>t('No')
);
$form['checklist_fieldset']['heating'] = array(
'#type' => 'radios',
'#title' => t('Heating options'),
'#options' =>$options,
'#default_value'=>$options['yes'],
);
I am getting yes as default value when I submit the form, but for users I need to show that as checked already. How can I achieve it?
Try this
$options = array(
'yes'=>t('Yes'),
'no'=>t('No')
);
$form['checklist_fieldset']['heating'] = array(
'#type' => 'radios',
'#title' => t('Heating options'),
'#options' =>$options,
'#default_value'=>'yes',
);
Related
I am creating a custom module. I have a form that I would like to add some HTML to, but I don't know what the best way to do this is. In this example, I have a page with a textbox, dropdown list, and text area. I want to add a a div between the dropdown list and text area. But, I'm not sure how to add raw html to a web form. Here is what I have:
function myModule_add_form($form_state){
try{
$form = array();
$form['myModule_title'] = array(
'#title' => 'Title',
'#type' => 'textfield',
'#size' => '30',
'#weight'=>1,
);
$form['myModule_type_list']=array(
'#type'=>'select',
'#title' => 'Type List',
'#options' => $someArray,
'#multiple' => false,
'#attributes'=>array('size'=>1),
'#weight'=>2,
);
$form['myModule_description'] = array(
'#title' => 'Description',
'#type' => 'textarea',
'#size' => '255',
'#weight'=>3,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
'#weight'=>4,
);
return $form;
}catch(Exception $e){
$errrmsg = "Error with creating form: " .$e->getMessage();
throw New Exception($errrmsg);
}
}
Thanks
jason
Here is the reference for this:
https://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/6
I think the best option is the markup form type (This is the default type) so if you do not set the 'type' it will be markup, so you can simply insert something like this wherever you want:
$form['some_div'] = array(
'#value' => '<div>The div</div>',
);
The other option is to use prefix or suffix as also mentioned in the form API, e.g.:
$form['myModule_type_list']=array(
'#type'=>'select',
'#title' => 'Type List',
'#options' => $someArray,
'#multiple' => false,
'#attributes'=>array('size'=>1),
'#weight'=>2,
'#suffix' => '<div>The div</div>',
);
I added the div as a suffix of your list, or it could be a prefix of the text area.
Another way of doing the first option which you may prefer for readability is like this:
$form['some_div'] = array(
'#type' => 'markup',
'#prefix' => '<div>',
'#value' => 'The div content',
'#suffix' => '</div>',
);
I have a problem with drupal_render (assuming that drupal_render is the right way for me to get what I want - feel free to correct me =).
I am building a form. Since the FAPI does not provide a "table"-field, I want to make one myself. My approach: use the theme()-function, specifically theme('table', ...) or theme_table(), and fill it with the respective form fields (with the intention of adding AHAH functionality later on). This forces me to use drupal_render as the value for the table cells, which causes some problems with the form elements.
The table collects numbers of employees by year, for the organisation the user is editing at this moment. The code looks as follows:
$form['employees'] = array(
'#type' => 'fieldset',
'#title' => t('Employees'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$employee_query = db_query("SELECT * FROM {employees} WHERE id_organisation = %d", $org['idoOrganisation']);
$employee = array();
while ($row = db_fetch_array($employee_query)) {
$employee[] = $row;
}
$header = array(
t('Year'),
t('Total'),
t('Internal'),
t('External'),
t('Aerospace')
);
$em_delta = 0;
$rows = array();
foreach($employee as $em_delta => $value) {
$form['employees'][$em_delta]['year'] = array(
'#title' => '',
'#type' => 'date_select', // Comes with the date module
'#date_format' => $format_year,
'#date_label_position' => 'within',
'#date_year_range' => '-50:+3',
'#default_value' => $value[$em_delta]['year'],
'#id' => 'edit-employees-' . $em_delta . '-year', // Allready a quickfix, since the form is rendered without id
'#name' => 'employees['.$em_delta.'][year]', // Same here
);
$form['employees'][$em_delta]['total'] = array(
'#type' => 'textfield',
'#title' => '',
'#default_value' => $value['total'],,
'#size' => 1,
'#id' => 'edit-employees-' . $em_delta . '-total',
'#name' => 'employees['.$em_delta.'][total]'
);
$form['employees'][$em_delta]['internal'] = array(
'#type' => 'textfield',
'#title' => '',
'#default_value' => $value[$em_delta]['internal'],
'#size' => 1,
'#id' => 'edit-employees-' . $em_delta . '-internal',
'#name' => 'employees['.$em_delta.'][internal]',
);
$form['employees'][$em_delta]['external'] = array(
'#type' => 'textfield',
'#title' => '',
'#default_value' => $value[$em_delta]['external'],
'#size' => 1,
'#id' => 'edit-employees-' . $em_delta . '-external',
'#name' => 'employees['.$em_delta.'][external]',
);
$form['employees'][$em_delta]['aero'] = array(
'#type' => 'textfield',
'#title' => '',
'#default_value' => $value[$em_delta]['aero'],
'#size' => 1,
'#id' => 'edit-employees-' . $em_delta . '-aero',
'#name' => 'employees['.$em_delta.'][aero]',
);
$rows[] = array(
drupal_render($form['employees'][$em_delta]['year']),
drupal_render($form['employees'][$em_delta]['total']),
drupal_render($form['employees'][$em_delta]['internal']),
drupal_render($form['employees'][$em_delta]['external']),
drupal_render($form['employees'][$em_delta]['aero']),
);
}
$form['employees']['table'] = array (
'#value' => theme('table', $header, $rows, array(), NULL)
);
Here are the problems I am encountering:
ID- and Name-Attributes of the form elements are empty. I found something on this on the drupal site and have made my peace with it (although I don't understand it), setting those attributes manually now.
Default-values of the text fields are ignored. The fields are empty. When I let drupal_get_form render the field, the default_value shows. Someone around here suggested to set the #value-property instead, but then again I read that this is something completly different and may cause problems.
The date_select-Field is not rendered in it's entirety. The wrappers are there, the select field however appears outside of the code, just before the table (i.e. where it appears in the code).
Let's hope that's it =)
Can anybody help? What am I doing wrong?
A colleague of mine pointed out that using drupal_render within the form function is not event remotely close to being a good idea, as it removes part of the form from the whole process of validating and submitting.
Thus, figuring out why the function does not work as intended is futile. The better approach would be to simply generate the necessary amount of form fields, let them be rendered as they are within drupal_get_form(), and use the forms theme-function later on to put them into a table.
Stupid me =)
I'm trying to add a couple of sections onto the user profile in Drupal 7 under:
<div class="profile" typeof="sioc:UserAccount" about="/drupal/user/1">
I'm adding three new sections, but the problem is that, although I am using the same way to add the three sections, only one of them is rendered as a child of the div above, while the other two are rendered as siblings. What am I doing wrong?
This is how I'm creating the content:
function plan_user_user_view($account) {
//Create the markup for the events region
$account->content['events'] = array(
'#type' => 'user_profile_item',
'#theme' => 'events',
'#events' => $events);
//Create the region for the venues
$account->content['venues'] = array(
'#type' => 'user_profile_item',
'#theme' =>'venues',
'#userid' => $user->uid,
'#venues' => $venues);
//Create the region for creating an event
$account->content['creator'] = array(
'#prefix' => '<div class="user-event-item" id="quick-event-creator">',
'#suffix' => '</div>',
'#type' => 'user_profile_item',
'#title' => t('QUICK EVENT CREATOR'),
'#markup' => drupal_render(drupal_get_form('event_creation')));
}
Also, is there a better way to create that last piece of content there? The other two seem fine in a template file but the last one since it's a form I was wondering if there are better ways of doing that.
Thanks,
Maybe you should have a look on this project profil2 which is the successor of content_profil for Drupal 6. With it you will be able to add informations onto users profils and if you want to code your custom fields by yourself it should be a good starting point to read.
Best.
How about this:
// Create the category.
$account->content['mymodule'] = array(
'#type' => 'user_profile_category',
'#title' => t('My module content'),
);
// Create first item (child).
$account->content['mymodule']['events'] = array(
'#type' => 'user_profile_item',
'#title' => t('Events'),
'#markup' => t('Whatever'),
);
// Create second item (child).
$account->content['mymodule']['venues'] = array(
'#type' => 'user_profile_item',
'#title' => t('Venues'),
'#markup' => t('Whatever'),
);
and so on. The bottom line is that user_profile_item items should be children of a user_profile_category item.
I am refactoring some code is a Drupal module I wrote sometime age. In order for others to use it, I am adding a configuration page.
I have successfully defined a fieldset but I don't know how to 'insert' content in to it.
The following code sets up radios for each node type defined on my site:
$node_types = node_get_types('names');
$test = array(
'#title' => t('tweeting node'),
'#type' => 'radios',
'#options' => $node_types,
'#default_value' => 'Page',
'#weight' => 0,
);
And the following defines my fieldset into which I want to insert the radio buttons generated above:
$form['twitterhelper_nodecollection'] = array(
'#type' => 'fieldset',
'#title' => t('select a node'),
'#weight' => 0,
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#parents' => $test,
);
to add any form element inside the fieldset you should insert this form element inside the field set array ...
E.g
$form['myfieldset'] = array(
'#type' => 'fieldset' ,
'#collapsible' => TRUE ,
'#title' => t('My FIeldset'),
'#attributes' => array('id' => 'myfieldset-id'),
);
$form['myfieldset']['myradios'] = array(
'#type' => 'radios' ,
'#attributes' => array('id' =>'myradio-attributes') ,
....etc
);
so the fieldset is the parent of the radios not the contrast
hop that help you
UPDATE:
you can append the radios inside the field set by using the jquery as the following
jQuery(document).ready(start) ;
function start(){
jQuery("#myradio-attributes").appendTo("#myfieldset-id");
// i added this id by '#attributes'
}
but its not the drupal way
How would you go about constructing a step by step form that uses AJAX through Drupal to pull the next form step?
For example,
Step 1:
I like Baseball
I don't like Baseball.
When that person clicks on either Like or Don't Like, I want to use AJAX to recognize and pull the next part of the form, remove/hide the first section since its not needed, and present the next section.
Example:
Step 1:
I like Baseball
*click
(fade out)
Step 2:
My favorite team is __________
The player I like most is __________
What is the best way to do this through Drupal Form API? I know how to build the forms and modules, but I have never used AJAX yet. I know a few things exist out there that are supposed to help, but I wanted to know if anyone here has done it and how they approached it.
You may want to give a look at the AHAH helper module.
usually i am create full form with fieldsets, then control them manually by jquery.
i assume there lot of ready to go modules in drupal, some of these:
http://drupal.org/project/conditional_fields / http://drupal.org/project/multistep
also: http://www.google.ru/search?q=drupal+multistep+ajax+form
If you don't want to write any code, and don't need the entered data to be drupal nodes, I suggest using the webform module. It has a pretty simple UI for building forms, and allows you do do multipage forms with conditional fields. You can then export the results as CSV, email them, etc.
I have made to solutions for this problem in drupal 7. First one I solve it with Ajax as was requested(if someone want I can convert this to drupal6), however it should be better to solve this using attribute #states. So also made a solution in the bottom using states.
How to solve this using Ajax:
function ajax_in_drupal_form($form, &$form_state)
{
$baseball = array(
'like' => t('I like Baseball'),
'unlike' => t('I don\'t like Baseball')
);
$form['step'] = array(
'#prefix' => '<div id="baseball-wrapper">',
'#suffix' => '</div>',
);
if ($form_state['values']['baseball'] == 'like') {
$form['step']['team'] = array(
'#type' => 'textfield',
'#title' => t('My favorite team is'),
);
$form['step']['player'] = array(
'#type' => 'textfield',
'#title' => t('The player I like most is'),
);
}
else if ($form_state['values']['baseball'] == 'unlike') {
$form['step']['other'] = array(
'#type' => 'textfield',
'#title' => t('What do you like'),
);
}
else {
$form['step']['baseball'] = array(
'#type' => 'radios',
'#options' => $baseball,
'#title' => t('Select your option'),
'#ajax' => array(
'callback' => 'ajax_update_step_callback',
'wrapper' => 'baseball-wrapper',
),
);
}
return $form;
}
function ajax_update_step_callback($form, $form_state) {
return $form['step'];
}
Here is the solution using #states(The preferred way of solving it):
function states_in_drupal_form($form, &$form_state)
{
$baseball = array(
'like' => t('I like Baseball'),
'unlike' => t('I don\'t like Baseball')
);
// step 1
$form['step']['baseball'] = array(
'#type' => 'radios',
'#options' => $baseball,
'#title' => t('Select your option'),
'#states' => array(
'invisible' => array(':input[name="baseball"]' => array('checked' => TRUE),
),
)
);
// step 2 like baseball
$form['step']['team'] = array(
'#type' => 'textfield',
'#title' => t('My favorite team is'),
'#states' => array(
'visible' => array(':input[name="baseball"]' => array('checked' => TRUE)),
'visible' => array(':input[name="baseball"]' => array('value' => 'like')),
)
);
$form['step']['player'] = array(
'#type' => 'textfield',
'#title' => t('The player I like most is'),
'#states' => array(
'visible' => array(':input[name="baseball"]' => array('checked' => TRUE)),
'visible' => array(':input[name="baseball"]' => array('value' => 'like')),
)
);
// step 2 I don't like baseball
$form['step']['other'] = array(
'#type' => 'textfield',
'#title' => t('What do you like'),
'#states' => array(
'visible' => array(':input[name="baseball"]' => array('checked' => TRUE)),
'visible' => array(':input[name="baseball"]' => array('value' => 'unlike')),
)
);
return $form;
}