ImageCache doesn't allow you to set a weight for the presets that you define. So I created presets named:
1_thumbnail
2_xxsmall
3_xsmall
4_small
5_medium
6_large
7_xlarge
I was expecting these to sort alphabetically in the (a) "Insert" section of the of my CCK field configuration, and (b) when choosing a style from the select list just before inserting in the image into the text area.
That didn't happen -- the sort order didn't seem to have any logic to it.
At line 95 of insert.module I commented out
uasort($styles, '_user_sort');
and that solved my problem.
Comments
Comment #1
quicksketchInsert module just uses the order defined by ImageCache. You'll notice that ImageCache doesn't put these into alphabetical order anywhere within Drupal.
Comment #2
toomanypets commentedThanks for the quick reply. So it looks like...
/insert/insert.module/insert_styles() calls
/insert/includes/imagecache.inc/imagecache_insert_styles() which calls
/imagecache/imagecache.module/imagecache_presets() which sorts the presets by presetname (line 909).
At this point the presets from imagecache are sorted alphabetically (as desired, at least by me).
Then /insert/insert.module/insert_styles() sorts the styles again (line 95) using:
I'm not sure what this line is supposed to do, but it's messing up the alphabetical sort described above. At least I think so, because when I comment out this line I am getting the desired results.
Comment #3
quicksketchOh sure enough, back over to Insert we go... I didn't think we sorted the Insert styles, but I must've changed it at some point.
So from what I can tell, we're going to need to write our own sorting function instead of trying to re-use _user_sort(). If you take a look at _user_sort(), it sorts on "weight" and "title". But Insert includes keys for "weight" and "name", not "title". This makes it so that all styles that have the same weight all end up mixed, since uasort() doesn't guarantee that the same order is used for items of the same value.
Comment #4
toomanypets commentedI'll take a stab at a new sorting function, but it will have to wait a few a days.
Comment #5
toomanypets commentedThe keys created by insert_styles() vary by module...
imagecache:
[label]
[name]
[module]
lightbox2:
[label]
[field types]
[name]
[module]
insert:
[label]
[weight]
[name]
[module]
The attached patch will sort on the logical common key: [label]
Please note that this is the first patch file I've created; please check for formatting.
Comment #6
quicksketchThanks for the patch! However if a weight is present that should probably take precedence over label. Additionally this function should be name-spaced, probably as "_insert_style_sort".
As a minor note, you don't need the space between function calls and parenthesis: i.e.
strcmp(instead ofstrcmp (.Comment #7
toomanypets commentedThanks for the review. Weight now takes precedence over label, the function is properly name-spaced, and the formatting has been corrected. Additionally, the label sort uses "a case insensitive natural order" algorithm.
Comment #8
toomanypets commentedComment #9
quicksketchThis code could be shortened up quite a bit. If something doesn't have a weight specified, it should probably be 0 rather than 9999. See the sort function in user module for a short example: http://api.lullabot.com/_user_sort.
Comment #10
toomanypets commentedI avoided the code in _user_sort() because I did not understand it. After a little research I now understand how the union array operator works (duplicated keys are not overwritten). Thanks for your patience.
Comment #11
quicksketchLooks super! I'll review this next time I'm working on Insert module.
Comment #12
quicksketchThanks! I've committed this patch to both branches. Works great.
Comment #13
toomanypets commentedThank you for another exceedingly useful module.