I want to limit the allowed extensions in $form['mail']['views_send_attachments']

On rule # 544 in views_send.module:
$file = file_save_upload('views_send_attachments', array(), $dir);
there is an empty array being provided which is why validation will fallback to the default safe list of extensions

I tried adding:

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'),
        ),
     );
  }
}

but without any luck.

Then, in my own custom module, I added a custom submit handler via:
$form['actions']['submit']['#submit']

And copy/pasted all the code from:
function views_send_form_submit($form, &$form_state)
into this custom submit handler

This seemed to work but I obviously will have to update this function when updates are made to:
function views_send_form_submit

What's the proper way to provide my own allowed extension list without being concerned about future updates?

CommentFileSizeAuthor
#2 views_send-2076659-2.patch1.87 KBundersound3
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

barraponto’s picture

The #upload_validators is only processed by managed_file elements. Regular file elements don't use that array. Right now you can either hack the module to add a validator by adding it to theparametes passed to file_save_upload (see the docs); or patch the module to sport a managed_file in the form.

What's even cooler about managed_file extension validator is that the file doesn't even get uploaded -- validator works from client-side! But it'll be harder, though.

undersound3’s picture

FileSize
1.87 KB

The managed file solution seems to be the most flexible solution indeed but I am unsure how to accomplish that.

I am not sure if this is helpful for anyone but for now I solved my problem with this patch which allows me to set my own custom allowed extensions list on the admin screen.

hansfn’s picture

Category: support » feature

Yes, maybe we need to do some thing.

If you are fine with limiting the default list of file extensions (which currently is "jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp" as defined by the core function file_save_upload), you can just use hook_form_alter to add you own validator. I haven't tested, but it should work - no need to hack or patch the module. (See http://befused.com/drupal/additional-validation-function for an example.)

However, if you need to use an extension which isn't in the default list, you need to modify the module in some way. That isn't satisfactory, so I'll definitely update the module in some way. What do you think is best - adding a new configuration option like undersound3 did in the patch above, just making it alterable with some hook (little work) or use managed_file (more work). Personally, I think there is enough configuration options in Views Send already ...

barraponto’s picture

Version: 7.x-1.0-rc3 » 7.x-1.x-dev

The downside to current approach is that it limits the file extensions for every view :/
But then, it's mostly due to current design (adding views send features through a field plugin, instead of a view display type).

hansfn’s picture

But then, it's mostly due to current design (adding views send features through a field plugin, instead of a view display type).

OK, you lost me there. (I'm not a Views expert.) Anyway, if this is relevant for the discussion in #2072415: restore use of VBO, but as an operation plugin, please comment there.

barraponto’s picture

Yeah, basically, I think VBO is a better approach.

barraponto’s picture

Issue summary: View changes

add module file name

hansfn’s picture

Issue summary: View changes
Status: Active » Fixed

The module can set it's own list of valid file extensions since version 7.x-1.2 - see #2237585: Make allowed file extensions for attachments configurable. I think that resolves this issue too.

undersound3’s picture

Ah that's great!. Thanks hansfn

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.