I've created a commerce add to cart form with a file upload field. The user can use this to upload a logo but I don't want anonymous users to be able to do this. I can see that the file module uses ajax to handle the upload but I can't see how you can disable the upload button for non logged in users.

enter image description here

  • May be you could use some jquery and css to hide the button.
    – Aparna
    Commented Oct 21, 2016 at 10:40
  • You may be right but unfortunately I don't know jquery so hoping for a drupal way to do this. May be there is an ajax validation callback or something like that? Commented Oct 21, 2016 at 10:54

2 Answers 2


The Field permissions module should help you achieve this.

The Field Permissions module allows site administrators to set field-level permissions to edit, view and create fields on any entity.

Enable field permissions on any entity, not just nodes.

Role-based field permissions allowing different viewing patterned based on what access the user has.

Author-level permissions allow viewing and editing of fields based on who the entity owner is.

Permissions for each field are not enabled by default. Instead, administrators can enable these permissions explicitly for the fields where this feature is needed.


A directed hook_form_alter can be used to do this. Add to cart forms are special breed in that the form ID's include hashes for uniqueness, so you can't use hook_form_FORM_ID_alter:

function MY_MODULE_form_alter(array &$form, FormStateInterface $form_state, string $form_id){
  // This will get all Add To Cart Forms. If you need something more specific, you'll have to tailor it for such.
  if (strpos($form_id, 'commerce_order_item_add_to_cart_form_') === 0) {
    if (\Drupal::currentUser()->isAnonymous()) {
      ... unset() the $form elements representing the upload stuff here.
      ... I would delete the file input and the upload button.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.