0

I have a custom content type, Contacts. Under contacts I have a custom File field, Contracts.

I want to hide the any file that have been uploaded under the File Field, Contracts in the node edit form. It should be view-able only by the admin.

I want to display the upload option in the field to all the users, however, not the files that have already been uploaded under the node.

Please advice

Thanks

The modified code:

MODULE NAME = Hide_Uploaded_Files.

CONTENT TYPE = Candidates.

CUSTOM fIELD = field_resume.

<?php
function Hide_Uploaded_Files_form_alter(&$form, &$form_state, $form_id) {
global $user;    
if ($form_id === 'Candidates_node_form' && !in_array('administrator', $user-
>roles) && $form['field_resume']['und'][0]['#default_value']['fid'] > 0) {
$form['field_resume']['#access'] = FALSE;   
}
}
?>   

1 Answer 1

1

Create a custom module and implements hooK_form_alter(). and set field access to false for other user except administrator.


function MODULENAME_form_alter(&$form, &$form_state, $form_id) {
  global $user;    
  if ($form_id === 'CONTENTTYPE_node_form' && !in_array('administrator', $user->roles) && $form['field_image']['und'][0]['#default_value']['fid'] > 0) {
    $form['field_image']['#access'] = FALSE;    
  }
}

MODULENAME = Your costom module machine name.

CONTENTTYPE = Your content type machine name.

field_image = Your custom file field machine name.

7
  • Thank you for the code @Yogesh. I did as per your advice, however, it doesnt seem to work. Appreciate if you could guide me. I created the custom module & updated the code with my custom content & field information. Do advice me of any other options I could try? Thank you May 24, 2017 at 14:36
  • @GirishCholayill I have tested this code before posting my answer. Please clear your cache and then test again. if you are logged in with administrator role then you can see image/file field on edit form but you can't see the image/file field on edit if logged in with other than administrator. Please show me your code so that I can help you. May 25, 2017 at 6:19
  • Sorry for the delay, was trying different permutations & combinations, still a no go for me, I did clear the cache as well as tested with a Non-Admin user, but, the files are still displaying in the node edit form. I am not developer, hence do not know where I goofed up. I have edited my question to input the modified code, do let me know what changes I need to do to make it work? May 29, 2017 at 15:53
  • There is a little mistake you have in your code. Replace $form_id === 'Candidates_node_form' to $form_id === 'candidates_node_form' May 30, 2017 at 4:41
  • Thank you for pointing it out. It hides the uploaded files now, however, it also hides the option to upload any files as well. What changes should I make, if I want to hide the uploaded files, but have the option to display upload new files to the user in the node edit form? Thank you very much for your guidance. I truly appreciate it. May 31, 2017 at 5:38

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.