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?
Comment | File | Size | Author |
---|---|---|---|
#2 | views_send-2076659-2.patch | 1.87 KB | undersound3 |
Comments
Comment #1
barraponto CreditAttribution: barraponto commentedThe
#upload_validators
is only processed bymanaged_file
elements. Regularfile
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 amanaged_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.Comment #2
undersound3 CreditAttribution: undersound3 commentedThe 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.
Comment #3
hansfn CreditAttribution: hansfn commentedYes, 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 ...
Comment #4
barraponto CreditAttribution: barraponto commentedThe 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).
Comment #5
hansfn CreditAttribution: hansfn commentedOK, 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.
Comment #6
barraponto CreditAttribution: barraponto commentedYeah, basically, I think VBO is a better approach.
Comment #6.0
barraponto CreditAttribution: barraponto commentedadd module file name
Comment #7
hansfn CreditAttribution: hansfn commentedThe 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.
Comment #8
undersound3 CreditAttribution: undersound3 commentedAh that's great!. Thanks hansfn