Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ordering by date in custom field #1368

Closed
vitopepito opened this issue Mar 21, 2017 · 6 comments
Closed

ordering by date in custom field #1368

vitopepito opened this issue Mar 21, 2017 · 6 comments

Comments

@vitopepito
Copy link

I experienced the following issue: In a page collection with a header like in the code below, the items will not be ordered chronologically if the field 'header.event.start' is in date format.

title: 'collection'
content:
    items: '@self.children'
    order:
        by: header.event.start
        dir: desc

This is an example of an item

title: 'item1'
event:
    start: '21-9-2017 20:00'
    end: '21-9-2017 22:00'

I think that the date format in the custom field is not recognized and ordering by number is used. Using the american date format makes the items ordered by month first, but it is not properly recognized as a date.

Work around

The collection header can use a dateRange in combination with order to achieve chronological ordering that can be either descending or ascending.

title: 'collection'
content:
    items: '@self.children'
    dateRange:
        start: today
        field: header.event.start
    order:
        dir: asc

This works for me but is kind of hacky. The documentation of dateRange should be updated to show the ordering options.

@akoebbe
Copy link
Contributor

akoebbe commented Mar 21, 2017

@vitopepito Thanks for creating this. I'll look at it today. Ref: #274

@akoebbe
Copy link
Contributor

akoebbe commented Mar 21, 2017

@vitopepito I think your workaround may not be doing what you think. The order configuration without a by parameter will default to the directory name of the page. So what's happening in your case is that it's sorting by the page directory and not the dateRange.field. The dateRange config does not play in to the order at all. If you want to use the same field for both you have to restate it in order.by.

Now, there is a problem with using custom dates in the header as your sort field. The sorter does not know the type of value the custom field is (aside from array fields, which are imploded), so it falls back to treating the field as a string for sorting.

So the real issue here is how we can determine if the field in the order.by setting is a date or not. I'm thinking about possibilities, but happy to hear suggestions. There is a order.sort_flags setting but that is expected to be a PHP sanctioned sort flag.

@akoebbe
Copy link
Contributor

akoebbe commented Mar 21, 2017

My current thought is to make a new Utils method to regex check a string and see if it is in one of the allowed date formats from \Grav\Common\Utils::dateFormats. Any objections?

@vitopepito
Copy link
Author

You are probably right about my workaround.

Of course it would be nice if custom fields are recognised as dates.

Another option would be to just use a date format of type YYYY-MM-DD hh:mm for the custom field. The string sorter would work and with a twig |date() filter it could be converted to any other format for front facing content.

@rhukster
Copy link
Member

Closing to due to inactivity

@otahirs
Copy link

otahirs commented Jun 27, 2018

Hi, I have the same problem as the original author. I would like to sort collection by date in header.event.start using format dd-mm-yyyy.

Is it possible now, a year later?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants