Mackski Posted January 19, 2014 Share Posted January 19, 2014 I'm uploading multiple images via the API. Is there a way to explicitly set the sort order once uploaded? eg: something like this would be handy: foreach($files as $order => $f) { $p->images->add($f)->sort($order); } Link to comment Share on other sites More sharing options...
Martijn Geerts Posted January 19, 2014 Share Posted January 19, 2014 Short answer, no. But there are alphanumeric sort possibilities on run time. As $images is a Wirearray you could do: $images = $page->images->sort('name'); foreach ($images as $key => $image) { echo "$key => " . $image->url . '<br>'; // hyatt2.jpg => /site/assets/files/1/hyatt2.jpg } This will, sort the images on filename. -- And you can re-save the page with the new sort order. // Page is previously saved, but now we reset the order $page->images->sort('name'); $page->of(false); $page->save(); Link to comment Share on other sites More sharing options...
Mackski Posted January 19, 2014 Author Share Posted January 19, 2014 Does it not make sense to expose the sort field via the API?Front end example would be drag and drop sorting of user uploaded images.Having this order saved and mirrored in PW Admin would be ideal. 1 Link to comment Share on other sites More sharing options...
Mackski Posted January 19, 2014 Author Share Posted January 19, 2014 Not an ideal solution, I ended up renaming the files on upload and append my sorting order on each file name before saving. ie: {sort_order}_{page->id}.{extension} Based on this post: http://processwire.com/talk/topic/206-renaming-uploaded-files-from-tmp-name/ Link to comment Share on other sites More sharing options...
Martijn Geerts Posted January 19, 2014 Share Posted January 19, 2014 Good solution, you can use the ->setName() when using the example of ryan to rename it ( remove the {sort_order} ) Link to comment Share on other sites More sharing options...
Mackski Posted January 19, 2014 Author Share Posted January 19, 2014 Thanks Martijn, I did not know that method exists, happy now I can clean up my code... 1 Link to comment Share on other sites More sharing options...
Martijn Geerts Posted January 19, 2014 Share Posted January 19, 2014 I also did not know that Source code of ProcessWire is Joyful to read. 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now