Drupal module settings page construction - drupal

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

Related

drupal 6 add html to web forms

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>',
);

Drupal Commerce custom checkout pane state not working as expected

I'm attempting to add a conditionally displayed fieldset in a custom checkout pane. The fieldset is always displayed regardless of the state, unlike on a standard form.
For example, in the code below I want the "hungry_fields" fieldset displayed based on the value of the "hungry" radio button.
function hungry_pane_checkout_form($form, &$form_state, $checkout_pane, $order) {
$pane_form['hungry'] = array(
'#type' => 'radios',
'#options' => array('yes' => t('Yes'), 'no' => t('No')),
'#required' => TRUE,
'#title' => t('I am hungry')
);
$pane_form['hungry_fields'] = array(
'#title' => 'Hungry',
'#type' => 'fieldset',
'#collapsible' => FALSE,
'#collapsed' => FALSE,
'#states' => array(
'visible' => array(
':input[name="hungry"]' => array('value' => 'yes'),
),
),
);
return $pane_form;
}
I'm new to Drupal Commerce so it's entirely possible I'm missing something.
I fixed this by using a form callback with drupal_get_form() instead of adding the fields to the array directly.
For example:
function hungry_pane_checkout_form($form, &$form_state, $checkout_pane, $order) {
$pane_form['form'] = drupal_get_form('hungry_form');
}
function hungry_form($form, &$form_state) {
...
}
Using this method the forms work properly.

drupal-6 form API add <html> to module page

I made a form module (only segment shown) and would like to add some text between questions in the form so I wrote the following, but no text 'text is here' appears.
... $form['name1'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
?><html><p>Text is here </p></html><?
$form['name2'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#collapsible' => TRUE,
'#collapsed' => FALSE, ...
One of the reasons why that will not work is because, in your function you are building a form array for Drupal to render not actually outputting the form yourself. so when you break out of the php and output the HTML, it will be output when Drupal runs your function.
What you'll want to do is add a form item of type markup. See the form API
Also, semi-unrelated the #collapsible and #collapsed properties only apply to fieldsets.
So your code would like something like this.
$form['name1'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
);
$form['betweenfields-html'] = array(
'#value' => '<p>Text is here </p>',
);
$form['name2'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
);

How do I add an onchange handler to a dropdown list in Drupal?

How can I add an onchange handler to a dropdown list in Drupal? The dropdown list is added using hook_form(). I have to perform a function depending on the onchange function.
Is there a way to do this?
You can add a form like this:
hook_form()
{
$form1["dropdown"] = array(
'#type' => 'select',
'#title' => t('Preview the page with themes available'),
'#options' => $ptions,
'#default_value' => 'defalut_value',
'#attributes' => array('onchange' => "form.submit('dropdown')"),
);
//Submit button:
$form1['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit '),
'#attributes' => array('style' => 'display: none;'),
);
Now you can add submit functionality using hook_submit().
Here is the simple example using '#ajax' property
$form['select'] = array(
'#type' => 'select',
'#title' => 'Option #1',
'#options' => $option,
'#ajax' => array(
// Call function that rebuilt other field
'callback' => 'ajax_load_field',
'method' => 'replace',
// div to be get replace by function output
'wrapper' => 'chart',
'effect' => 'fade'
),
);
If you generate another form field by using this drop down.
Then use AHAH for that.
$form['my_form_submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
'#weight' => 1,
'#submit' => array('my_form_submit'),//none JS version
'#ahah' => array(
'event' => 'click',
'path' => 'mymodule/js', //Your ajax function path
'wrapper' => 'myform-wrapper',
'method' => 'replace',
'effect' => 'fade',
'progress' => array(
'type' => 'bar',
'message' => t('Loading...')
)
),
While I am sure that Nicholas is way past this issue by now, this may help some who are searching for a solution.
I'm using D7 and getting the single quotes around dropdown encoded to ' I suppose by >check_plain. >How do I avoid that? – Nicholas Tolley Cottrell Jun 2 at 16:03
I just found the "Drupal" way of doing this.
Step 1, set a variable to contain dropdown by using drupal_add_js:
drupal_add_js(array('mymodule' => array('varname' => 'dropdown')), 'setting');
Step 2, add the attributes line as
'#attributes' => array('onchange' => "form.submit(Drupal.settings.mymodule.varname)"),

jquery accordion drupal 6 id problem

I am using jquery accordion (v.1.8). When I use prefix for div creation id is missing i.e.
$form['container'] = array(
'#prefix' => '<div>',
'#attributes' => array('id' => 'accordion'),
'#suffix' => '</div>'
);
I cant see any id = "accordion" in DOM
plus if I use this method below:
$form['container'] = array(
'#prefix' => '<div id = "accordion">',
'#value' => t('&nbps;'),
'#suffix' => '</div>'
);
it works can you please tell me the reason since am new to drupal
That's because Drupal is looking for some form element to add the id to but none are found. For instance, if you make #type a fieldset as in below, the fieldset will have an id of "accordian".
$form['container'] = array(
'#type' => 'fieldset',
'#prefix' => '<div>',
'#attributes' => array('id' => 'accordion'),
'#suffix' => '</div>',
);
The code above will output a DIV that contains a fieldset with an id of "accordian". From there you could add other form elements inside the fieldset, like:
$form['container']['fake'] = array(
'#type' => 'textfield',
'#title' => t('My Textfield'),
'#size' => 30,
'#maxlength' => 128,
'#required' => TRUE,
);
For more information about Drupal forms, see the Forms API Reference, it is an extremely valuable source information.

Resources