Sorting K2 Attachments

I recently ran into an issue where I needed to upload a large number of attachments to a post. Simple enough right? With K2, of course! The only problem was, the attachments needed to be sorted by name. I tried altering the order of the attachments by adding number prefixes (01_file.jpg) to the filename, but this did not work. For some reason, 6 would always appear before 1…

No problem! I just needed to specify a sort order for the array in the template code. Wait array what? I know, it sounds complicated, but it is really very simple. Of course, you want to use template overrides rather than modifying the original template. If you are unfamiliar with the MVC template override feature in K2, you can read the K2 templating page on getk2.org.

Once you have created your template override, open item.php and search for the following code:

foreach ($this->item->attachments as $attachment):

Just above this code, add:

//Sort attachment by filename
$sortArray = array();

foreach($this->item->attachments as $attachmentItem){
foreach($attachmentItem as $key=>$value){
if(!isset($sortArray[$key])){
$sortArray[$key] = array();
}
$sortArray[$key][] = $value;
}
}

$orderby = "filename";

array_multisort($sortArray[$orderby],SORT_ASC,$this->item->attachments);

That’s it! Your attachments will now appear in alphabetical order.

 

Be Sociable, Share!
Tarik Assagai
Tarik (@tarik916) is a web developer and owner of ACS Digital Media, a Sacramento based web development agency specializing in open source CMS systems. If you are looking for support for your current or upcoming projects, visit www.acs-digital.com.

24 Comments

  1. That’s handy!
    Is there any solution to sort in date of uploading?
    That would be nice.

    • tarik916 /

      Not that I know of. I don’t believe there is any date data saved with the attachments.

  2. Bicss /

    Hi!
    It doesnt work for me,

    I tryed changing the code as this:

    item->attachments); foreach ($this->item->attachments as $attachment): ?>

    And nothing :S

    Any solution?
    It has to work to all the attachments or to the next uploaded attachments?

  3. Bicss /

    The reply is not showing all the code but i wrote this:

    item->attachments); foreach ($this->item->attachments as $attachment): ? >

    • tarik916 /

      You’re right. I’m not actually sure where that single line sort code worked. I’ve updated it with the code that I use often.

  4. Bicss /

    Thank you so much! I will try it :)

  5. Bicss /

    Hi! I cant find the code that you say.
    In my item.php is not any word with “sort” text

    PD: Can you upload your template with the changes done?

    Thx!

  6. Bicss /

    It works without the last code 😀

    • tarik916 /

      Thanks! I updated the post but forgot to move the last line.

  7. My client wanted to post newsletters and needed the newest to post at the top and oldest at the bottom. Since the old files didn’t follow a naming convention and it was more trouble to rename them all, i added the following code to allow the attachments to sort by title instead. This is what i added:

    $sortArray = array();

    foreach($this->item->attachments as $attachmentItem){
    foreach($attachmentItem as $key=>$value){
    if(!isset($sortArray[$key])){
    $sortArray[$key] = array();
    }
    $sortArray[$key][] = $value;
    }
    }

    $orderby = “title”;

    array_multisort($sortArray[$orderby],SORT_DESC,$this->item->attachments);

  8. Chen /

    thanks!
    do you know how can I sort exists attachments list?

    • Tarik Assagai /

      This method would sort an existing list but if the files are not named to be sorted, then you will need to re-upload the files. Hopefully they implement an admin sort feature in future releases.

  9. Does anybody know what I would have to change to make it the opposite order.
    From Z to A instead from A to Z.
    Thank You.

  10. Muhaymin /

    Any Tricks to make the download attachments resumable one? Because the attachment download can’t be resumed if paused.

    Thanks

    • Tarik Assagai /

      Hi, sorry, I have no idea how to change the download behavior.

Leave a Reply