1

I use image field with possibility to upload unlimited images. BUT under the user registration, I want to limit the possibility to just one image. So that the anonymous user, who wants to become a registered user, can only upload one image. After they become users, then they can upload as many images as they wish.

Does anybody know how to do that?

1 Answer 1

1

You want to use a hook_form_alter to process this for just that form, ex.

function mymodule_form_alter(&$form, $form_state, $form_id) { 

    if ($form_id == 'user_register') {
        // do stuff here
    }

}

The 'stuff' is the real challenge, especially since you are allowing for unlimited uploads. There is a previous post on this with a solution for a limited set (<10):

https://stackoverflow.com/questions/5701488/change-fields-number-of-values-per-role

My suggestion for resolving this (though not ideal) would be to add a custom handler as part of your logic that would check to see if one already exists and gently prod them not to add more than one.

if ($form_id == 'user_register') {

    $form['<image field name>']['#element_validate'] => array('_mymodule_only_one_image'),

}

and then for your validation function, you could simply test whether a 2nd image exists and prod the user to only submit one. Rough psuedo-code example here, haven't tied this to an actual form.

function _mymodule_only_one_image($element, &$form_state) {

    if (!empty($element['<image_field_name>'][1])) {
        form_error($element, t('Please only upload one image');
    }

}

Another option would be to try and disable the ajax handler that initiates the 2nd upload through your own custom jquery override, which you could also add in to this custom form through the above form_alter.

1
  • Thankyou very much for a great answer!! I haven't thought about the element_validate possibility. I will try it out, and thanks once again.
    – Raf A.
    Commented Jan 30, 2012 at 20:38

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.