Recently a customer of mine asked for a page with a list of K2 items, sortable by the custom fields and item titles. And I accepted the challenge. a first search on the "k2 list sort by extra fields" term returned a whopping number of 1,120,000+ results... and most of them where how to hack the core component to obtain a list sorted by a given extra field - which was not what I wanted. But, reading these pages an ideea sparkled - and in less, than a hour I had in place a solution wich was clean, no core code was hacked, and the table of results is freely sortable.

You can see it clicking here.

And here is the secret recipe:

A. Create a template override for the K2 Content Module (mod_k2_content)

Be sure to name it something else, like default. You might need the default one elsewhere. I named it "filelist". Let's say you have 5 extra fields in your K2 , with aliases like "Extrafield1Alias" to "Extrafield5Alias".

The key part of the template override looks like this:

	$html='{someplugincode}<table class="sortable">';
     $html.='<thead><tr>
            <th>Title</th>
            <th>'.$item->extraFields->Extrafield1Alias->name.'></th>
            <th>'.$item->extraFields->Extrafield2Alias->name.'></th>
            <th>'.$item->extraFields->Extrafield3Alias->name.'></th>
            <th>'.$item->extraFields->Extrafield4Alias->name.'</th>
            <th>'.$item->extraFields->Extrafield5Alias->name.'</th>
        </tr></thead>
        <tbody>';
    foreach ($items as $key=>$item):    
        $html.='<tr>
            <td><a href="'.$item->link.'">'.$item->title.'</a></td>
            <td>'.$item->extraFields->Extrafield1Alias->value.'</td>
            <td>'.$item->extraFields->Extrafield2Alias->value.'</td>
            <td>'.$item->extraFields->Extrafield3Alias->value.'</td>
            <td>'.$item->extraFields->Extrafield4Alias->value.'</td>
            <td>'.$item->extraFields->Extrafield5Alias->value.'</td>
            </tr>';
    endforeach;
    $html.='</tbody></table>{/someplugincode}';
    echo JHTML::_('content.prepare', $html);?>

First question is what a heck is that {someplugincode} - is simple, one of available plugins in JED wich can turn the correctly formatted HTML tables in sortable ones ;)

My preferates are ArtData and Szaki Table, you can find them easily.

B. Choose your sort table plugin

Install it, and publish it. And, of course, adapt the above code to match your plugin.

C. Publish the K2 Content module

Choose a unused (maybe unexisting) module position, as "k2itemlist", and publish the module.

Set the module parameters to use the K2 items you want to include,  and set as the template you want to be used the template override you created in step A. (Don't know, how to create a template override? Search this site, you will find the solution!)

D. Create a new content item

Add to it, whatewer you need to add before and after the sortable table, add the code { loadposition k2itemlist } (no spaces before and after the curly brackets, of course), link it to a menu item, or make it accessible as you want.

E. Grab a beer, a glass of fine red wine, or some tea, and celebrate.

You're the new K2 guru in the town. Enjoy - at your own risk.

0
0
0
s2sdefault
Category: K2 hacks