0

I need a hook (or something similar) where I can set the number of image uploads depending of the role of the user.

In the settings of the field there is no way to set the maximum number of values users can enter, just one value for all roles. For example, for role #1 I want the users can upload just 3 images, for role #2 just 5 images.

I tried several ways but none worked. I tried using #element_validate but the validation never worked.

Is there some way to do that? I forgot to mention is for Drupal 7.

0

Solution1 Limit on the form:

function <module>_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'center_node_form') {
    global $user;
    // Only allow to upload 2 images for role = staff
    if (in_array('staff', $user->roles)) {
      $center_image_limit = 2;
      $form['#field_info']['field_center_image']['multiple'] = $center_image_limit;
      $i = 1;
      foreach ($form['field_center_image'] as $key => $value) {
        if (is_numeric($key)) {
          if ($i > $center_image_limit) {
            unset($form['field_center_image'][$key]);
          }
          $i++;
        }
      }
    }
  }
}

Source: DRUPAL – LIMIT THE NUMBER OF VALUES OF A CONTENT TYPE FIELD BY ROLE @ 1

Solution2 Limit in validation:

function <module>_form_alter(&$form, &$form_state, $form_id) {  
  if ($form_id == 'center_node_form') {
    // Add the extra validation
    array_unshift($form['#validate'], '<module>_addtional_validate');
  }
}

function <module>_addtional_validate($form, $form_state) {
  $count = 0;
  foreach($form_state['values']['field_center_video'] as $video) {
    if (!empty($video['value'])) {
      $count++;
    }
  }

  global $user;
  // Only allow to upload 2 videos for role = staff
  if (in_array('staff', $user->roles)) {
    if ($count > 2) {
      form_set_error('field_center_video', t('You cannot upload more than 2 videos'));
    }
  }
}

Source: DRUPAL – LIMIT THE NUMBER OF VALUES OF A CONTENT TYPE FIELD BY ROLE @ 2

  • Thanks. The first solution only works for Drupal 6, I need the solution for Drupal 7. And the second solution didn't work. – Jose Daniel Feb 1 '15 at 16:57
  • Strange. Second one should work. Check if you changed all the field names to yours. Also check the $form_state. You should have ['fid'] instead of ['value'] for image field. Try to add validation with $form['#validate'][] instead of array_unshift. – milkovsky Feb 2 '15 at 8:46
0

Check if File Field Role Limit module works for image field as well, the module page says

The Filefield Role Limit module works also with ImageField module.

But since image field is moved to core so I am not sure if this module will work out of the box. In case not, it can give you a starting point for building this functionality, best would be to create a issue for feature request on module page and start fixing the module to make it work and contribute back :)

  • Thanks, but I need limit of the number of uploads, not the size of the upload. – Jose Daniel Feb 1 '15 at 16:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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