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;
Related
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'm trying to add custom fields to the user registration form. I need to save 2 of them into the Drupal User's database and 1 into my own separate database.
I'm using hook_form_alter to add the fields and then hook_form_submit to save the fields.
How do I submit the first/last name fields into drupal's user table and then save the SSN into my own? I have a function to switch between the 2 databases already but am just not sure how to use hook_submit_form to save some in one database and some in another?
function myModule_form_alter(&$form, &$form_state, $form_id){
if($form_id == 'user_registration_form'){
$form['f_name'] = array(
'#type' => 'textfield',
'#title' => t('First Name'),
'#required' => TRUE,
'#attributes' => array('class' => array('fname'))
);
$form['l_name'] = array(
'#type' => 'textfield',
'title' => t('Last Name'),
'#required' => TRUE,
'#attributes' => array('class' => array('lname'))
);
$form['ssn'] = array(
'#type' => 'textfield',
'#title' => t('Social Security Number')
'#maxlength' => 11,
'#required' => TRUE,
'#attributes' => array('class' => array('ssn'), 'placeholder' => '999-99-9999')
);
Would it be something like this?
function myModule_form_submit($form, &$form_state){
//Array of information to save into drupal user's table
$edit = array(
'f_name' => $form_state['values']['f_name'],
'l_name' => $form_state['values']['l_name'],
'second_email' => $form_state['values']['second_email'],
);
user_save(drupal_anonymous_user(), $edit);
drupal_set_message(t('The form has been submitted.'));
}
I'm not sure if I can use drupal's user_save() function and pass it this new information to save or if I have to do something in the .install file to add these new columns and then target them in the form_submit?
In what field will you save the custom field data? You should create a custom table for those fields and save data in there. When you load the user, you should also load those details from that table. There is no way you can directly load those custom field data when user object is loaded.
I have found the way to add Multiupload Filefield Widget into my custom form from this answer, https://drupal.stackexchange.com/questions/90637/does-multiupload-filefield-widget-for-drupal-provides-a-form-api-element/90658#90658?newreg=8dba9580e3d74b4d8b2cd1b140a4f08d
After adding the multiple file upload field into the form, and I try to upload some files, it won't show the list of uploaded files after the loading bar is gone.
Here is how I declare the field in my form:
$form['downloadsformset'] = array(
'#type' => 'fieldset',
'#title' => t('Downloads'),
'#collapsible' => TRUE,
'#group' => 'myform'
);
$form['downloadsformset']['download_items'] = array(
'#type' => 'mfw_managed_file',
'#title' => t('Download items'),
'#description' => t('You are allow to upload jpg, jpeg, png and gif'),
'#progress_indicator' => 'bar',
'#upload_location' => "public://dev/tmp",
"#upload_validators" => array(
"file_validate_extensions" => array("png jpg jpeg"),
'file_validate_size' => array(1024*1024*1024),
),
'#group' => 'myform'
);
What I have missed in order to show the uploaded file list?
You can try plupload module. This module also supports drag and drop file upload.
After installing module, you can do this in code,
$form['my_element'] = array(
'#type' => 'plupload',
It seems that the Multiupload Filefield Widget is not really meant to be used in the form API.
I have a checkbox defined in a form:
$form['membership_block']['membership_info_is_verified'] = array(
'#type' => 'checkbox',
'#title' => t('<strong>I confirm my information is correct.</strong>'),
'#description' => t('Select the checkbox here to confirm your information is correct, then be sure to <strong>click the Save button</strong> below to save your change.'),
);
I would like the title and description to be surrounded by a box with a light green background. I have not been able to figure out how to specify this.
Form elements can have (optional) elements '#prefix' and '#suffix'.
$form['membership_block']['membership_info_is_verified'] = array(
'#prefix' => '<div class="green_background">',
'#suffix' => '</div>',
// ...
);
This way you can surround the HTML code of Form elements with additional code.
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.