3

Using the Views Send module to send mass email. On the email creation page I want to set my custom validation extensions for the file field provided by Views Send in combination with MimeMail (r# 267 in views_send.module).

For the file field I am trying to set the only allowed extension to be of type .docx by using the following function:

function my_module_form_alter(&$form, &$form_state, $form_id) {
  if($form_id == "views_form_views_send_page") {
    if ($form_state['step'] == 'views_send_config_form') {

      $form['mail']['views_send_attachments'] = array(
        '#type' => 'file',
        '#title' => t('Attachment'),
        '#upload_validators' => array(
          'file_validate_extensions' => array('docx'),
        ),
     );
  }
}

Unfortuanetely this is not working. What am I doing wrong here?

1 Answer 1

3
+25

Turns out the file behavior you're looking for is not defined for file elements, but for managed_files. It's as easy as:

/**
 * Implements hook_form_FORM_ID_alter().
 */
function my_module_form_views_form_send_page_alter(&$form, &$form_state) {
  if ($form_state['step'] == 'views_send_config_form') {
    $form['mail']['views_send_attachments']['#type'] = 'managed_file';
    $form['mail']['views_send_attachments']['#upload_validators']['file_validate_extensions'][] = 'docx';
  }
}

But views_send will have to be patched to support managed_files. This still needs work in the issue queue.


Yet, as I mention in the issue, you can hack it directly in the module. It's not recommended, but we don't have a hook here and the issue above might take a while to be solved. So just change the line

$file = file_save_upload('views_send_attachments', array(), $dir);

to

$file = file_save_upload('views_send_attachments', array('file_validate_extensions' => array('docx')), $dir);
5
  • When I make it a managed file Views Send is complaining: Warning: Invalid argument supplied for foreach() in views_send_confirm_form() (regel 483 van /var/www/example.com/sites/all/modules/contrib/views_send/views_send.module)
    – devunder
    Aug 30, 2013 at 15:35
  • To clarify better, I want to set my custom validation extensions for the file field provided by Views Send in combination with MimeMail (r# 267 in views_send.module)
    – devunder
    Aug 30, 2013 at 15:40
  • Hm, I was just being naive. If you want to use managed_file then you'll have to patch views_send. It's an easy patch, though. I'll see you in the issue queue. Aug 30, 2013 at 16:24
  • Thanks, I opened up an issue there as well drupal.org/node/2076659
    – devunder
    Aug 30, 2013 at 16:34
  • Thanks, I tried that myself already but this is exactly what I am trying to avoid so I was hoping for another solution. I was thinking about creating a patch to add another textfield to the admin page where you can set your allowed extensions and then do a simple check with variable_get in file_save_upload
    – devunder
    Aug 30, 2013 at 19:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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