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

quicksketch’s picture

Project: Insert » ImageCache
Version: 6.x-1.0-beta4 » 6.x-2.0-beta10

Insert module just uses the order defined by ImageCache. You'll notice that ImageCache doesn't put these into alphabetical order anywhere within Drupal.

toomanypets’s picture

Thanks 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).

$result = db_query('SELECT * FROM {imagecache_preset} ORDER BY presetname');

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:

uasort($styles, '_user_sort');

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.

quicksketch’s picture

Project: ImageCache » Insert
Version: 6.x-2.0-beta10 » 6.x-1.0-beta4

Oh 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.

toomanypets’s picture

I'll take a stab at a new sorting function, but it will have to wait a few a days.

toomanypets’s picture

StatusFileSize
new795 bytes

The 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.

quicksketch’s picture

Status: Active » Needs work

Thanks 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 of strcmp (.

toomanypets’s picture

Thanks 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.

toomanypets’s picture

Status: Needs work » Needs review
quicksketch’s picture

This 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.

toomanypets’s picture

I 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.

quicksketch’s picture

Looks super! I'll review this next time I'm working on Insert module.

quicksketch’s picture

Status: Needs review » Fixed

Thanks! I've committed this patch to both branches. Works great.

toomanypets’s picture

Thank you for another exceedingly useful module.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.