I am working on a custom module with multi-page form on drupal 6.
I found that #default_value is not working when my '#type' => 'textfield'.
However, when '#type'=>'textarea', it displays correctly with the '#default_value' specified.
Basically, I wrote a FormFactory to return different form definition ($form) based on the post parameter received. Initially, it returns the display of directories list, user then selects from radio buttos until a specific directory contains a xml file, it will become edit form. The edit form will have text fields display the data (#default_value) inside the xml file, however the type 'textarea' works here rather than 'textfield'.
How can I make my '#default_value' work in this case?
Below is the non-working field definition:
$form['pageset']['newsTitle'] = array(
'#type' => 'textfield',
'#title' => 'News Title',
'#default_value' => "{$element->newsTitle}",
'#rows' => 1,
'#required' => TRUE,
);
Then I changed it to textarea as shown below to make it work:
$form['pageset']['newsTitle'] = array(
'#type' => 'textarea',
'#title' => 'News Title',
'#default_value' => "{$element->newsTitle}",
'#rows' => 1,
'#required' => TRUE,
);
There should be no difference between a textfield and a textarea form element concerning the usage of the '#default_value' attribute, and both work for me as advertised. So if it does not work in your case, you should check for typos or other differences that might cause the faulty behavior.
Could you edit your question and add your form definition code?
What Drupal versions are you on? I'm on 6.16 and I'm having weird behaviour for defaults too. In my case, not working for textareas.
I'm a non-english speaker and my default_value had non ascii characters. It's fixed now using translation.
Related
I am using this function to render form in frontend.
acf_form(['id' => 'onboarding',
'post_id' => 'new_post',
'new_post' => array(
'post_type' => 'onboarding_form',
'post_title' => "$current_user->user_email ($current_user->last_name $current_user->first_name)",
'post_status' => 'publish'),
'submit_value' => __('Save and continue', 'swp'),
'html_submit_button' => $submitBtnHtml,]);
It worked perfectly, but now I tried to translate it. And when I access the page with this form, the form is there in both languages, no matter which one is currently set.
It shows me the same forms together, first one is in English and second one is the translated one.
Did I set up something wrong in Polylang or in ACF?
Or is it possible to get form based on current language?
Thank you
I'm new to Drupal and am trying to build a module. Part of what this module does is allow you to add preset classes from a drop down field.
For the most part I've got this working but for one thing: I seem to only be able to retrieve the select options name, not it's value.
The code I have is below.
In the config form creation function I have:
$styles = array(
'None' => '',
'Blue Buttons' => 'btn blue-btn',
'Red Buttons' => 'btn red-btn',
);
$mymodule_form['style'] = array(
'#type' => 'select',
'#required' => TRUE,
'#title' => t('Style'),
'#description' => t('Style for buttons'),
'#default_value' => $form_values['style'],
'#empty_option' => t('- Select -'),
'#options' => drupal_map_assoc(array_keys($styles)),
);
But, when I run dpm($this->options['style']); later on in my code when I want to use those styles, I get the key names return (eg Button Red)
Would anyone know how I can retrieve the values?
I was using drupal_map_assoc when I didn't need to.
'#options' => $styles,
is fine.
(Also, I had my key names and values around the wrong way).
I have a simple managed file form element that is the only element in my form. It looks like this:
$form['upload'] = array(
'#type' => 'managed_file',
'#title' => t('Select a YML file'),
'#progress_message' => t('Please wait...'),
'#progress_indicator' => 'bar',
'#description' => t('Click "Browse..." to select a file to upload.'),
'#required' => TRUE,
'#upload_validators' => array('file_validate_extensions' => array('yml txt docx')),
'#upload_location' => $upload_dest,
);
When I render the form using the drupal_get_form callback in hook_menu I get a perfectly formed managed_file upload field with the browse and upload buttons. Things change when I decide I want to add a table of information underneath the form. This requires building a table using theme functions then adding that to the form by rendering the form and appending the table. I create my table and add it to the form:
$rows = array();
foreach($yml_files as $yml_file){
$rows[] = array($yml_file->uri, $yml_file->filename);
}
$output = drupal_render($form['upload']);
$output .= theme('table', array('header'=>$header, 'rows'=>$rows));
return $output;
When I generate the form using drupal_render, I get the nice help text, but no upload form. The table renders fine in both scenarios and I'm not seeing any errors.
If Drupal uses drupal_render to render its forms why would the form look different in the second scnenario? Is there a way to get the entire form? I've tried a variety of ways of passing the form and using dpm to print the form at various stages and I'm not sure where to go from here.
Standard file upload fields render correctly as do other form elements. It seems to be limited to the managed_file element.
When using a drupal_get_form menu callback, you should return your $form array and not an already rendered themeable array. Probably you are missing #attached js files for the managed_file field.
What you could do in your case is to add the table output on a markup field of your form.
$form['upload'] = array(
'#type' => 'managed_file',
'#title' => t('Select a YML file'),
'#progress_message' => t('Please wait...'),
'#progress_indicator' => 'bar',
'#description' => t('Click "Browse..." to select a file to upload.'),
'#required' => TRUE,
'#upload_validators' => array('file_validate_extensions' => array('yml txt docx')),
'#upload_location' => $upload_dest,
);
$form['table'] = array(
'#markup' => theme('table', array('header' => $header, 'rows' => $rows)),
);
return $form;
I'm having some issues with form tabindexing. I have two submit buttons in my form, and the one I want to be indexed first isn't. This is currently what I have on my buttons:
$form['prev']['#attributes']['tabindex'] = '0'
$form['next']['#attributes']['tabindex'] = '1'
On this live version it shows up in the attributes they show up on both buttons.
I'm using Drupal 7 and creating my own custom form module.
$form['company'] = array(
'#title' => t('Company'),
'#type' => 'textfield',
'#required' => TRUE,
'#attributes' => array('tabindex' => '6'),
);
this is how you set your tab index programatically.
Is it possible to use a WYSIWYG editor in texarea
for Drupal site configuration form (system_settings_form).
This is how the configuration is coded now...
$form['my_module_text_bottom'] = array(
'#type' => 'textarea',
'#title' => t('Some text'),
'#default_value' => variable_get('my_module_text_bottom', 'This is configurable text found in the module configuration.'),
'#size' => 1024,
'#maxlength' => 1024,
'#description' => t("Some text."),
'#required' => TRUE,
);
return system_settings_form($form);
Here it is for Drupal 7 and Drupal 6.
For D7:
<?php
// Retrieve the default values for 'value' and 'format', if not readily
// available through other means:
$defaults = array(
'value' => '',
'format' => filter_default_format(),
);
$my_richtext_field = variable_get('my_richtext_field', $defaults);
// Just construct a regular #type 'text_format' form element:
$form['my_richtext_field'] = array(
'#type' => 'text_format',
'#title' => t('My richtext field'),
'#default_value' => $my_richtext_field['value'],
'#format' => $my_richtext_field['format'],
);
?>
For D6:
<?php
// Your saved or new data is supposed to have a value and a format. Just like
// $node has a $node->body and $node->format. May also come from a
// variable_get('mymodule_admin_setting', array('value' => '', 'format' => NULL));
$mydata = mymodule_data_load();
$form['myfield']['mytextarea'] = array(
'#type' => 'textarea',
'#title' => t('My textarea'),
'#default_value' => $mydata->value,
);
$form['myfield']['format'] = filter_form($mydata->format);
?>
I kept searching for this issue for about 6 hours and finally i found the reason, for your custom textarea field you must add this line, to use the default input format (Full HTML):
$form['format'] = filter_form();
be careful if you use this form element inside fieldset you must include this fieldset:
$form['donation-instructions']['format'] = filter_form();
I hope this will help you
The WYSIWYG or CKEditor modules should be able to do this.
I found this question similar to:
Drupal 6: Implement Wysiwyg on Custom Module Form
One of the answers there pointed to this drupal.org page:
http://drupal.org/node/358316
which provides fairly detailed examples of the "format" array key and filter_form(), also describing how it's used if your form has multiple textareas.
The approach given there doesn't apply to Drupal 7.
I ran into a similar situation where I'd downloaded and installed and installed CKEditor and it displayed when editing content nodes, but didn't display for the textarea on a configuration form for my module.