Drupal Multiupload Filefield Widget in Form API not really work? - drupal

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.

Related

ACF form file upload not working for non-admin users

I have an acf_form() on my website that includes some file upload fields. When I am logged in as admin, clicking the file upload button shows the wp media loader, when I am logged in as the custom role "user", I get the standard browser file upload and when the form is submitted, the file is not uploaded and does not appear in the media library.
Here is my acf_form() function:
acf_form(
array(
'id' => 'client-portal-acf-form',
'post_id' => 'new_post',
'new_post' => array(
'post_type' => 'technical_brief',
'post_status' => 'publish',
'post_title' => 'testing',
),
'return' => get_the_permalink(624) . '?bid=%post_id%',
'submit_value' => 'Upload your brief',
'form_attributes' => array(
'enctype' => 'multipart/form-data',
),
'uploader' => 'wp',
)
);
and here is the user role declaration giving permissions:
add_role( 'user', __('User'),
array(
'read' => true,
'upload_files' => true,
'edit_posts' => true,
'edit_pages' => true,
)
);
Any suggestions for allowing file uploads for the User role?
Update:
It seems that the "user" role was part of the problem. I checked the capabilities of this role, and despite me adding capabilities, it only ever shows "read", so I guess this role is reserved by WP.
Another part of the issue seems to be the file types uploaded. PDF, DOC, DOCX, and ODT all work fine, but .txt and .rtf will not upload - the form submits, but the files do not appear in the back end. As such, the issue is not 100% solved, but is working enough that it is suitable for my project. I'll leave it as not answered for now in case any further knowledge shows up later that may help.

Drupal: how do I retrieve the select type values from a module Config form?

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).

managed_file form element doesn't render correctly

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;

Video Module with Drupal 7 custom form

How can I use video module drupal 7 version with Custom form to create a field behave exactly the field on the node form? To do so I created a form with video field type but its not displaying anything
$form['file'] = array(
'#type' => 'video',
'#name' => 'files[]',
'#title' => t('Upload some videos'),
'#attributes' => array('multiple' => 'multiple'),
);
I used this From the test file of video.The filename is. VideoField.test

WYSIWYG editor in texarea for Drupal configuration form

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.

Resources