0

How do I restrict users with specific roles to upload files with specific extensions on node edit pages. On my site I have two specific roles editor and animator. Conditions are:

Editors can only upload .docx .doc .pdf files while Animators can upload .mp4 .mp3 .jpg files.

I added a validate function with hook_form_alter() and the validate does work but the user has to save the entire form before validation spits out an error about the wrong file extension. I want to get the file extension as soon as the user has finished uploading their file and display the error message before submitting the form but after uploading the file.

1 Answer 1

0

I have a quick solution for this using hook_form_alter.

So in your hook_form_alter you can check the user role and alter the validators key for that field. In my case the field was called field_material

global $user;
$valid_role = 'animator';

if(in_array($valid, $user->roles)) {
    //Change the extension in the validators key of that upload field
    $form['field_material']['und'][0]['#upload_validators']['file_validate_extensions'][0]  = 'mp4 mp3';  
} 

I am sure there are better solutions but this is quickest one so far for me to get going.

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.