It would be nice to allow other modules to add additional validators that can fire directly upon file upload, rather than doing it in hook_nodeapi (op = validate) or using sneaky tricks. To my knowledge, this is not currently possible, so I added a hook called hook_filefield_validators(). Patch attached.

CommentFileSizeAuthor
filefield_additional_validators.patch645 bytescwgordon7

Comments

quicksketch’s picture

You can already add additional validators by implementing hook_elements(), then adding additional validators in a #process callback after FileField has expanded its widget. This is how modules like ImageField Extended and FileField Sources already add their own validation and additional functionality to FileField.

cwgordon7’s picture

Status: Needs review » Closed (works as designed)

That didn't occur to me, clever. Thanks! I guess this is by design then.

cwgordon7’s picture

Category: task » support
Status: Closed (works as designed) » Active

Actually, I can't seem to make that work. Here is my hook_elements code:


/**
 * Implementation of hook_elements().
 */
function my_module_elements() {
  $elements['filefield_widget']['#process'] = 'my_module_filefield_widget_process';
  return $elements;
}

And here is my process callback code:


/**
 * FAPI #process callback.
 */
function my_module_filefield_widget_process($element) {
  $element['#upload_validators']['my_module_file_validate'] = array();
  return $element;
}

I know the _process() callback is being called because one I put a drupal_set_message() in there, it shows up. When I print_r() the $element variable at the end of my _process() callback, it seems to have worked - the fourth #upload_validators validator is there. However, I don't think this is actually doing anything because a) my validate function is never called, and b) when I print out the form in hook_form_alter(), there are only the default three #upload_validators in the form. Any ideas as to what I'm doing wrong?

Thanks!

cwgordon7’s picture

Category: support » task
Status: Active » Needs review

Ok, I've done a bit of investigating, and here's what I think is the problem, which is why I believe the patch is necessary:

The validation is done in #value_callback. This is called before #process is called in http://api.drupal.org/api/function/_form_builder_handle_input_element/6. Therefore, there is no way for other modules to alter the #upload_validators FAPI property as far as I see. The other modules you referenced do not attempt to alter the #upload_validators property. Setting back to needs review.

quicksketch’s picture

Hmm sure enough. We may just be out of luck here. FileField has been moved into core and I'm not going to add any new APIs that don't have a corresponding piece in Drupal 7 core. My feeling is that this really doesn't belong in FileField anyway. If this functionality is needed for FileField, it'd also be needed for other widget types. I'd say the better way to go is to have a hook_field_widget_alter() that allows you to modify the widget before it goes into the form. Of course you'll have the same battle in CCK/Field module as you would here, both modules are frozen.

quicksketch’s picture

Status: Needs review » Closed (won't fix)

Moving to won't fix as FileField cannot introduce new APIs that do not also exist in core.