I have a set of >100 webforms in my projects.

I need to add an upload field to all webforms in my hook_form_alter().

I guess I need to do something like the following.

function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
  if (strpos($form_id, 'webform_client_form_') === 0) {
    $form['file'] = array(
      '#type' => 'file',
      '#title' => t('File'),
      '#weight' => 50,

I need to set as on certain attributes such as:

  • fieldkey
  • max upload size
  • allowed file extensions

How can this be achieved?

enter image description here

  • No answers yet? :). The field can upload: doc, xls, pdf, jpg mais pas des formats comme xlsx, docx. – drupal_stuff_alter Dec 18 '12 at 8:24

In this way (hook_form_alter) you will have this field in existing and next created webforms, and you must have own additional _validate and _submit function to process the $form['file'] field. Other way, the question is how to add programmatically file upload component to existing webform? To see structure of webform file component, add manually field file to one existing webform and make your setting from the picture, and see devel output tab (enable devel module). Also you may be interested to see http://api.drupalize.me/api/drupal/function/webform_component_insert/7. Then create update script to loop webforms.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.