Ordering Date Nav by a custom date attribute

Permalink
I'm trying to use Date Nav core add-on with an event add-on that uses a custom attribute for start and end times. I would like to use Date Nav and order the pages by the custom start date attribute rather than the Collection Public Date attribute that I believe Date Nav is sorted by on default.

It looks like I need to ovverride the date_nav.php file found in concrete\core\controllers\blocks. I just don't know what to change in that file to order by a custom attribute.

kspitzley
 
kspitzley replied on at Permalink Reply
kspitzley
I figured this out. Maybe this will help someone else out.

I wanted to override the core date nav controller from a custom template so that it would sort by a custom date attribute. I found the core date_nav controller in concrete=>core=>controllers=>blocks=>date_nav.php. I copied the function that containined the sortBy code and pasted it in my custom template's controller.php file and changed the sortBy part of the function like this:

class DateNavBlockController extends Concrete5_Controller_Block_DateNav {
function getPages($query = null) {
//blah blah blah
$pl->sortBy('my_custom_attribute', 'desc');
//blah blah blah
}
}


Then I loaded my newly edited controller.php file to blocks=>date_nav.

I wasted some time loading the controller.php file to blocks=>date_nav=>templates=>my_template.
kspitzley replied on at Permalink Reply
kspitzley
Another note: placing a custom controller.php file in blocks=>date_nav will affect every instance of the date nav block on your site, which is not ideal. So in my case, since I only want to sort by my custom attribute if that custom attribute has a value, and in all other cases I want to sort by the default PublicDate attribute, I modified the code above to this:

$pl = new PageList();
         $pl->setNameSpace('b' . $this->bID);
         $scdate = CollectionAttributeKey::getByHandle('custom_attribute');
         $cArray = array();
         //$pl->sortByPublicDate();
         if ( is_null($scdate)) {
         $pl->sortBy('custom_attribute', 'desc');
         } else {
         $pl->sortByPublicDateDescending(); 
         }
patrickduchhart replied on at Permalink Reply
hi there,

I am trying to order my date navigation on a custom attribute as well.
However if I use your code snippet it does not seem to work correctly.
I modified the variable $pl in my controller.php in my blocks > date_nav folder (!! not in the core directory !!);
My variable reads: $pl->sortBy('item_start', 'desc');

however it does not order my date_nav accordingly.
When I change the $pl to another custom attribute (address) it reorders the date_nav alphabetically. So it seems to work, just not when I try to order based on a date. Do I have to use a specific date format?

Is it possible for you to post the entire modified controller.php so I can see what I amn doing wrong?
Or do you have any pointers as where I have to look for my solution.

Any help would be much appreciated.

with kind regards,

Patrick
kspitzley replied on at Permalink Reply 1 Attachment
kspitzley
Sure - I've attached the whole controller.php for you. I have to admit, I'm pretty new at php so I tend to just stumble into solutions so I'm not the ideal person to help troubleshoot! What does it appear to sort by when you use item_sort? Have you looked into your item_start attribute to make sure its a date type?
patrickduchhart replied on at Permalink Reply
Thanks for you reply and script.
To be honest I did not have time to test it out yet.
But when I get to it I will let you know if it worked.

Thanks for your help.