--- contrib/image_attach/image_attach.module 2009-11-25 08:09:09.000000000 +0000 +++ contrib/image_attach/image_attach.module 2009-11-25 18:20:15.000000000 +0000 @@ -147,6 +147,14 @@ function image_attach_form_alter(&$form, '#options' => array(0 => t('Disabled'), 1 => t('Enabled')), '#description' => t('Should this node allows users to upload an image?'), ); + $number_of_images = array(0 => t('Unlimited'), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 20, 30, 40, 50, 75, 100, 150, 200, 300, 400, 500); + $form['image_attach']['image_attach_maximum'] = array( + '#type' => 'select', + '#title' => t('Maximum number of images'), + '#default_value' => variable_get('image_attach_maximum_'. $form['#node_type']->type, 0), + '#options' => $number_of_images, + '#description' => t('Select the maximum number of images that may be attached to this node type.'), + ); $form['image_attach']['image_attach_size_teaser'] = array( '#type' => 'select', '#title' => t('Teaser image size'), @@ -294,29 +302,54 @@ function image_attach_image_add_submit(& } /** + * Image attach validation handler for node edit form. + * + * Check that the number of images has not exceeded the maximum. * Capture node form submission and immediately create an image if one has been * uploaded. * Note that the new image nodes are created even on preview. Taking several * attempts may create trash. */ function image_attach_validate(&$form, &$form_state) { - $validators = array( - 'file_validate_is_image' => array(), - ); - if ($file = file_save_upload('image', $validators)) { - $image_title = $_POST['image_title'] ? $_POST['image_title'] : basename($file->filepath); - // Initialize an image properly. - $image = image_create_node_from($file->filepath, $image_title, ''); - if ($image && !form_get_errors()) { - drupal_set_message(t("Created new image to attach to this node. !image_link", array('!image_link' => l($image_title, 'node/'. $image->nid) ))); - // Append image nid to array of images. - $form_state['values']['iids'][$image->nid] = $image->nid; + // Validate the number of attached images. Filter out the 'None' with array_filter. + if ($maximum = variable_get('image_attach_maximum_'. $form['#node']->type, 0)) { + $uploading_new_image = ($form_state['clicked_button']['#value'] == t('Attach')); + $num_images = count(array_filter($form_state['values']['iids'])); + if ($num_images >= $maximum && $uploading_new_image) { + // This error will be set when attempting to upload a new image. The number already selected may be equal to the + // maximum, in which case the error is just to alert the user that their upload has not been performed, and allow + // them to unselect an image and proceed to upload the new one. + form_set_error('iids', format_plural($num_images, + 'You have selected @num_images image and the maximum for a @type entry is @maximum. The new image has not been uploaded.', + 'You have selected @num_images images and the maximum for a @type entry is @maximum. The new image has not been uploaded.', + array('@num_images' => $num_images, '@maximum' => $maximum, '@type' => $form['#node']->type))); + } + elseif ($num_images > $maximum) { + form_set_error('iids', t('You have selected @num_images images but the maximum for a @type entry is @maximum.', + array('@num_images' => $num_images, '@maximum' => $maximum, '@type' => $form['#node']->type))); } } - else { - // Only raise error if user clicked specific Attach button. - if ($form_state['clicked_button']['#value'] == 'Attach') { - form_set_error('image_attach', t('Invalid or missing image file for upload and attach.')); + + // Validate and save the uploaded image, providing that there are no errors set. + if (!count(form_get_errors())) { + $validators = array( + 'file_validate_is_image' => array(), + ); + if ($file = file_save_upload('image', $validators)) { + $image_title = $_POST['image_title'] ? $_POST['image_title'] : basename($file->filepath); + // Initialize an image properly. + $image = image_create_node_from($file->filepath, $image_title, ''); + if ($image && !form_get_errors()) { + drupal_set_message(t("Created new image to attach to this node. !image_link", array('!image_link' => l($image_title, 'node/'. $image->nid) ))); + // Append image nid to array of images. + $form_state['values']['iids'][$image->nid] = $image->nid; + } + } + else { + // Only raise error if user clicked specific Attach button. + if ($form_state['clicked_button']['#value'] == t('Attach')) { + form_set_error('image_attach', t('Invalid or missing image file for upload and attach.')); + } } } }