I'm not too savvy with CCK fields yet but the attached patch works for my needs so far. I figure there are better ways to do several of these things but wanted to contribute and see if any of it is useful.
1) max number of images. This settings allows a cap on the number of images that can be added to the node. It works by counting both the images already attached to the node and the images in the current session that are not yet part of the node.
2) max image size. Checks the uploaded filesize against the defined max images size in Kb.
3) rewrite filename. I wanted to be assured of unique filenames and also to discard user supplied filenames. Right now the resulting filename is a bit long and ugly in the display area but that can be cleaned up. Uses md5_file function on the uploaded file.
Comment | File | Size | Author |
---|---|---|---|
#19 | imagefield.module.137518.patch | 2.74 KB | jaydub |
#18 | imagefield.module.patch | 2.74 KB | jaydub |
#15 | imagefield.module.patch | 2.51 KB | jaydub |
#12 | imagefield.module.patch | 2.51 KB | jaydub |
#5 | imagefield.module_7.patch | 4.49 KB | jaydub |
Comments
Comment #1
quicksketchThese all sound like valuable features to me and I'd like to see them in imagefield. Some notes on your patch:
- Use t() to translate string:
Rather than
'The file you uploaded has a filesize greater than the maximum allowed filesize of '.$field['widget']['max_filesize'].'kb'
uset('The file you uploaded has a filesize greater than the maximum allowed filesize of @sizekb', array('@size' => $field['widget']['max_filesize']))
- Avoid duplicate code. Rather than including
in two places in the same function, use an IF condition to skip the necessary code to get down to where this code already exists.
I put in a separate issue that adds mime-type validation (http://drupal.org/node/132054) which includes a seperate _validate function which you might consider as a cleaner way to accomplish the validation.
Comment #2
quicksketchOne more small thing, after uploading several images, if I upload a large one over the filesize it causes all images in the SESSION to not be displayed. Attempting to upload another image makes them all come back.
Comment #3
jaydub CreditAttribution: jaydub commentedrevsied patch taking into account quicksketch's comments. moved the validation routines for max images and max filesize to functions.
Comment #4
quicksketchOh dear I'm sorry I wasn't quite clear. Make one validation function and include all of the various validates in that one function. In the other patch I'm using
_imagefield_widget_upload_validate()
.Anyway, I don't think it matters much because both patches are going to need to be re-factored a little bit if they both go in. The rest of the patch looks okay.
@dopry Should this be an imagefield 2.0 feature or make it into the 1.2 release?
Comment #5
jaydub CreditAttribution: jaydub commentedbetter patch that actually works!
@quicksketch - just saw your update about rolling into one validate function. well for now just got this updated patch. I'm open to further fixing but as you said there are a number of other features such as your image type validation that need to be worked into the main project. I just wanted to put out there an attempt to add some of the features that I had seen in the issue queue and that I had needed as well. I'm good with letting the module author(s) take over and use as appropriate if that's the best course of action.
Comment #6
yched CreditAttribution: yched commentedThe 'max number of images' part seems a possible workaround for CCK's current inability to control the number of multiple values in a generic / all-field-types way.
One small remark, though : the error message ("You are only allowed to upload up to @maximages images") is misleading - you'd think this is a site-wide limit,
And I think the convention is to end error messages with a period.
Nitpicking, really...
Comment #7
dopry CreditAttribution: dopry commentedJaydub,
if you're up to a quick round of revision...
1) reroll for drupal-5--2, I'm not putting any new features into older branches.
2) move validation into _imagefield_widget_upload_validate
3) you can probably refector the max images to be something like count($node_field) + count($_SESSION['imagefield'][$fieldname])... if those aren't arrays in that validate function
something is probably wrong and we should figure out why they're not initialized instead of
testing if they're init'd before acting on them.
4) if you're using node_field, use items instead...
Comment #8
dopry CreditAttribution: dopry commentedoops I forgot about the status update.
Comment #9
jaydub CreditAttribution: jaydub commentedI've been laid out by a virus as of late so sorry but no chance to reroll this patch with suggested changes. will keep you posted.
Comment #10
m3avrck CreditAttribution: m3avrck commentedSubscribing...
Comment #11
dopry CreditAttribution: dopry commentedI'd still love to see this land in imagefield after the 2.0 release. Any chance of re-rolling the patch jaydub?
Comment #12
jaydub CreditAttribution: jaydub commentedSee attached patch for max filesize and max number of images. Rolled against the current dev version. This patch only includes 1) and 2) of the original post.
TODO:
- add in text regarding constraints to the form created in _imagefield_widget_form
Comment #13
bobdalob CreditAttribution: bobdalob commentedExcellent,
with one bug if editing a published node.UPDATE: My mistake - just Excellent! Bug was not related to this patch (it was due to another fix elsewhere that I had applied shortly beforehand and didn't notice)
Comment #14
dopry CreditAttribution: dopry commentedThis patch looks great so far, but the for issue really needs to be addressed before I can commit it. I think we could set #disabled on the upload field and update the description text if items >= max images.
Comment #15
jaydub CreditAttribution: jaydub commentedIf we set #disabled when max images is triggered then would the user not be able to set an existing image to be deleted? If the user decides that with a say 3 image limit that the first image is not as useful as the 4th image then they should be able to delete image 1, submit and be able to upload image 4...
This of course would work a lot better if image delete was also javascript as is upload but I see that the patch for that issue is not ready at this point.
For now I've added the patch reroll for the RC2 release.
Comment #16
dopry CreditAttribution: dopry commentedI wasn't talking about setting #disabled on any of the image rows for uploaded images... only on the new file upload element. This would force users to select an image to delete before uploading a new one.
The last patch you posted looks like it is reversed.
Comment #17
jaydub CreditAttribution: jaydub commentedHow to go about modifying the form to disable the file element? The validation function only returns whether the current upload is valid or not and none of the function parameters are references (node, field, or items) so I can't modify a variable that would be available in _imagefield_widget_form.
Comment #18
jaydub CreditAttribution: jaydub commentedok patch re-rolled against HEAD release from Jan 5 with the proper ordering of the files.
Comment #19
jaydub CreditAttribution: jaydub commentedre-rolled against 5.x-2.0-rc4
Comment #20
jaydub CreditAttribution: jaydub commentedComment #21
dopry CreditAttribution: dopry commentedcommitted to DRUPAL-5--2 with modifications.
Comment #22
Anonymous (not verified) CreditAttribution: Anonymous commentedAutomatically closed -- issue fixed for two weeks with no activity.