function image_attach_validate in Image 6
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.
1 string reference to 'image_attach_validate'
- image_attach_form_alter in contrib/
image_attach/ image_attach.module - Implementation of hook_form_alter().
File
- contrib/
image_attach/ image_attach.module, line 393 - image_attach.module
Code
function image_attach_validate(&$form, &$form_state) {
// Test for whether a file is being uploaded cribbed from file_save_upload().
$uploading_new_image = isset($_FILES['files']) && $_FILES['files']['name']['image'] && is_uploaded_file($_FILES['files']['tmp_name']['image']);
// Validate the number of attached images. Filter out the 'None' with array_filter.
if ($maximum_images = variable_get('image_attach_maximum_' . $form['#node']->type, 0)) {
if (is_array($form_state['values']['iids'])) {
$num_images = count(array_filter($form_state['values']['iids']));
}
else {
$num_images = 0;
}
$node_type_label = node_get_types('name', $form['#node']->type);
if ($num_images >= $maximum_images && $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', t('There are @count_images images already attached. A new image cannot be uploaded until one or more attached images are unselected.', array(
'@count_images' => format_plural($num_images, '1 image', '@count images'),
'@maximum' => $maximum_images,
'%type' => $node_type_label,
)));
}
elseif ($num_images > $maximum_images) {
form_set_error('iids', t('You have selected @count_images but the maximum for a %type is @maximum.', array(
'@count_images' => format_plural($num_images, '1 image', '@count images'),
'@maximum' => $maximum_images,
'%type' => $node_type_label,
)));
}
}
// 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 Upload button.
if ($uploading_new_image) {
form_set_error('image_attach', t('Invalid or missing image file for upload and attach.'));
}
}
}
}