Sorting a Page List
Permalink
I have a pagelist block that is pulling truncated page data in from pages with the is_featured attribute,
The Page List is copying content from pages that have areas that has been populated using the "Designer Content" block,
One of the fields created by the Designer Content block is a "Date Picker" field (the other two fields are a select box and a text box)
At present the Page List is being sorted by selecting the "in reverse alphabetical order" and I have this code in my Page List Contoller
This keeps the list sorted by the last edited or created page,
What I would like to happen is the Pages to be sorted by the Earliest date in the "Date Picker" fields,
Do you think this can be achieved?
The Page List is copying content from pages that have areas that has been populated using the "Designer Content" block,
One of the fields created by the Designer Content block is a "Date Picker" field (the other two fields are a select box and a text box)
At present the Page List is being sorted by selecting the "in reverse alphabetical order" and I have this code in my Page List Contoller
case 'alpha_desc': $pl->sortBy('cvDateCreated', 'desc'); break;
This keeps the list sorted by the last edited or created page,
What I would like to happen is the Pages to be sorted by the Earliest date in the "Date Picker" fields,
Do you think this can be achieved?
![ConcreteOwl](/files/avatars/79508.jpg)
Bump!
Bump again...
Doesn't anybody have an opinion on this?
Doesn't anybody have an opinion on this?
Just a quick thought on this but if the date is tied to a page attribute then you can search by that
$pl->sortBy('customDate', 'desc');
//this should also work
//not sure if the "C" needs to be a capital?
$pl->sortByCustomDate('desc');
Though if this was created by designer content then this may only be tied to a block not the page?
If so you could save the date to the page attribute when you edit the block.
Loader::model('collection_attributes');
$c = Page::getCurrentPage();
$c->setAttribute('customDate',$customDateField);
$pl->sortBy('customDate', 'desc');
//this should also work
//not sure if the "C" needs to be a capital?
$pl->sortByCustomDate('desc');
Though if this was created by designer content then this may only be tied to a block not the page?
If so you could save the date to the page attribute when you edit the block.
Loader::model('collection_attributes');
$c = Page::getCurrentPage();
$c->setAttribute('customDate',$customDateField);
Thanks TheRealSean
I really appreciate your help,
I think I understand your suggestion,
I will try to implement it and report back..
I really appreciate your help,
I think I understand your suggestion,
I will try to implement it and report back..
I have implemented that by adding the code as suggested and by creating the customDate attribute,
I get no errors but the list is just re-organizing by page date instead of block date...
However, I think some more delving into the date/time attribute may reveal a method that could be implemented by the user to assign a date stamp on the block when editing,
I will pursue that idea for now,
Many Thanks again @TheRealSean for the pointer..
I get no errors but the list is just re-organizing by page date instead of block date...
However, I think some more delving into the date/time attribute may reveal a method that could be implemented by the user to assign a date stamp on the block when editing,
I will pursue that idea for now,
Many Thanks again @TheRealSean for the pointer..
I don't know of a way to do this directly through the Page List class -- it only operates on page data, not anything having to do with the blocks inside pages. That being said, once you have the list of pages, you could loop through them in PHP and inspect the designer content block, and then put the page objects in your own order.
Try this:
Try this:
<?php $block_dates = array(); //page id will be the key, date from custom block will be the value foreach ($pages as $page) { $page_blocks = $page->getBlocks('Main'); //<--change this to the area that the designer content block will be in on the page foreach ($page_blocks as $b) { if ($b->btHandle == 'your_custom_block_handle') { $data = $b->getInstance()->getBlockControllerData(); $date = $data['field_3_date_whatever']; //<--name of database field for the designer content block (look in block's db.xml file to see what it should be) $cID = $page->getCollectionID(); $block_dates[$cID] = $date; break; //stop looping through the page's blocks because we've found the one we're looking for } } } //Now sort the dates (you may want a different sorting function, seehttp://php.net/manual/en/array.sorting.php)...
Viewing 15 lines of 28 lines. View entire code block.
Hi Jordan,
I have used your code to create a custom pagelist template,
Here is the error code that it gives..
Fatal error: Cannot use object of type BlockRecord as array in F:\wamp\www\my_site\blocks\page_list\templates\organiser.php on line 14
line 14 looks like this..
This is taken from the designer content db.xml file as per your instructions,
Any thoughts?
I have used your code to create a custom pagelist template,
Here is the error code that it gives..
Fatal error: Cannot use object of type BlockRecord as array in F:\wamp\www\my_site\blocks\page_list\templates\organiser.php on line 14
line 14 looks like this..
$date = $data['field_2_date_value'];
This is taken from the designer content db.xml file as per your instructions,
Any thoughts?
Bump!
Sorry dude, I replied to this via email a few days back but I guess the C5 system lost it :(
Here ya go...
Maybe try $data->field_2_date_value (instead of $data['field_2_date_value']).
Here ya go...
Maybe try $data->field_2_date_value (instead of $data['field_2_date_value']).
Thanks Jordan,
With the new code I no longer get any errors but I also don't get any page list,
I have PM'd you some of the code I am using in case I have done something really stupid..
With the new code I no longer get any errors but I also don't get any page list,
I have PM'd you some of the code I am using in case I have done something really stupid..