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 content by creation date. #68

Closed
bretmorris opened this issue Jan 6, 2016 · 8 comments
Closed

Ordering content by creation date. #68

bretmorris opened this issue Jan 6, 2016 · 8 comments
Assignees
Labels

Comments

@bretmorris
Copy link

I feel like I'm taking crazy pills here! I've looked all over the internet and cannot find a solution...

The problem:

I am using contentful to essentially create a blog. The content type I am interested in displaying and ordering is called "post". So far I have managed to pull all my posts into /data/blog/posts using the following configuration:

activate :contentful do |f|
    f.space = { blog: 'xxxxxxxx' }
    f.access_token = 'xxxxxxxxxxxxxxxxx'
    f.content_types = { posts: 'xxxxxxxx'}
end

note: I intentionally left out the IDs and tokens

The problem is when I attempt to output those posts in my .erb file using the following:

<% data.blog.posts.each do |id, post| %>
<h1><%= post.title %></h1>
<% end %>

This results in a list of post tiles (as expected) but there appears to be no order to the output. I would expect the output to be ordered by creation date by default. How can one go about adding this order or any other order for that matter?

PS, one of the problems seems to be that data.blog.posts is an object with post IDs as keys. That seems problematic. Thanks for your help! I'm just getting started with middleman so forgive my ignorance!

@dlitvakb
Copy link
Contributor

dlitvakb commented Jan 7, 2016

Hey @bretmorris,

Would you share the f.cda_query in the activate block? As I said in the Stack Overflow post, you can specify order.

On an aside note, you can add Meta-data to your Content-Types and use that for ordering, for example, an Order numeric field, or a Featured Article boolean field are good ways to sort your entries if you need fine-grained specific ordering. Then including that in your cda_query as your order parameter will get you the desired order.

@dlitvakb dlitvakb self-assigned this Jan 7, 2016
@bretmorris
Copy link
Author

Hey @dlitvakb my cda_query looked like this:

f.cda_query = { content_type: 'xxxxxxxxxxx', order: 'sys.createdAt' }

Ordering by createdAt, reverse createAt, publishedAt, etc. seems to have no effect on the output.

Thanks!!

PS, if I can't find a solution here I may do what you suggested and just add a field and attempt to order by that field. Thanks for that suggestion!

@dlitvakb
Copy link
Contributor

Hey @bretmorris,

Sorry it took a bit longer to answer this, but I was trying to understand what the problem was.

In fact, all sys.* orderings you've been using actually work, but at import time. That means, that once data is imported, the entries in the data directory are ordered by id regardless of the ordering provided in the cda_query. This can be overridden in any of the following 2 ways.

Add a CustomMapper that includes the sys property you want to order by. Or include a meta-field in your ContentType through the Contentful UI or via the Management API.

After that, in your template, order like the following:

<% entries = entries.to_a.sort_by{ |id, e| e['the_field_you_want_to_sort_by'] } %>

Hope this helps.

Please let me know if you need any further help

@bretmorris
Copy link
Author

@dlitvakb thank you I actually ended up coming upon a similar solution:

  • I added a date field to my posts
  • I added a custom extension that essentially added a new property to the data hash called posts. I made posts an array and sorted it by the newly added date field.
  • In my templates I then accessed those posts by looping through data.posts.

This seems like the sort of thing that a lot of people using contentful-middleman would need to know to build their app. I ended up getting this information from someone who had built a project with contentful-middleman before. Honestly I'm not sure how they figured it out. Do you think it's worth adding anything about this technique or a similar technique in the documentation?

Thanks again for your help!!!

@dlitvakb
Copy link
Contributor

Hey @bretmorris

Glad it helped you.

Closing the issue

@tommotaylor
Copy link

tommotaylor commented Apr 20, 2018

@dlitvakb Is there any scope to improve the way contentful_middleman handles this? I added an order to my content and sorted by it, but doing things like:

data.project_key.entries.to_a.sort_by{ |id, e| e[:order] }.each

is pretty longwinded when a sort attribute or field in the config would be really handy.

@dlitvakb
Copy link
Contributor

Hey @tommotaylor,

This is not possible sadly, it is a limitation of how Middleman handles data files and not related to the integration itself.

While it would be possible to prepend an index to the data file name, and then middleman have it already sorted, it would remove the ability to do anything useful with the shorthand entry ID, and also would remove direct access to the entry if you don't also happen to know the index it happened to have landed with. Therefore, I consider this approach to be wrong, and much rather prefer a sort function over the collection you're trying to iterate.

Cheers

@wanderingcrow
Copy link
Contributor

Sorry, maybe not the same question, but similar, and after spending all morning on it, this was the closest issue I could find. I'm trying to find out if employees are on a specific team, and if so, sort_by a new field I created called sort_order.

Ruby is not my specialty, I muddle through due to the excellent community and documents around Middleman and Contentful.

What I have so far is:

- data.my_cool_stuff.bios.sort_by{ |id, s| s.last_name }.each do |id, bio|
  - if bio.team == "Executive Team"
    = bio.last_name

And this works. But if I want to sort by sort_order, it fails with a nil class, and the reason is that I don't have sort_order defined in every team content file. It's blank for the majority of them. I know that there is a way I can sort ONLY if they have a sort_order.

@dlitvakb any ideas?

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

No branches or pull requests

4 participants