Skip to content
This repository has been archived by the owner on May 8, 2024. It is now read-only.

Linked entries/assets not resolved on preview mode #103

Closed
martzoukos opened this issue Dec 15, 2016 · 7 comments
Closed

Linked entries/assets not resolved on preview mode #103

martzoukos opened this issue Dec 15, 2016 · 7 comments

Comments

@martzoukos
Copy link

martzoukos commented Dec 15, 2016

Edit 16.12.2016: Apologies for the initial confusing title, I had a mixed up impression of what dynamic_entries do, the issue is the same but it has to do with linked entries/assets not being resolved, so please ignore what mention to dynamic_entries I make.


Hello there,

I'm using the with_preview method to preview a specific page that has linked assets and entries in some of its fields.

While the nonlinked fields are working correctly, the ones where an asset or another entry is referenced, are not being fetched, for example:

with_preview(space: 'CONTENTFUL_SPACE', access_token: 'CONTENTFUL_ACCESS_TOKEN' ) do |preview|
   @article = preview.entry('ENTRY_ID')
end

...

@article.inspect:

{
    ...
    [:post_category, "Article"],
    [:main_image, #"Link", :linkType=>"Asset", :id=>"C6y5BNKNYOUaUIIm2ccqU"}>] 
    ...
}

I've snooped into the gem's code a bit and I saw that dynamic entries are set to :auto so this should be getting all the linked stuff.

Is this the intended behaviour? If not I can make a recursive method to fetch all the stuff, but I wanted to check first before I do it.

Thanks again!

@martzoukos martzoukos changed the title dynamic_entries not working on preview mode Linked entries/assets not resolved on preview mode Dec 16, 2016
@dlitvakb
Copy link
Contributor

Hey @martzoukos,

Let me take a look into this, haven't experienced this issue before.

Cheers

@dlitvakb
Copy link
Contributor

Hey @martzoukos,

Found the issue, you're using client.entry which uses the /entries/entry_id endpoint which doesn't resolve includes. Instead, you should be using /entries?sys.id=entry_id and fetching the first element of the collection. Difference is that the #entries endpoint does have includes resolution.

Hope that helps.

Cheers

@martzoukos
Copy link
Author

Thanks David,

so I need to do something like that?

with_preview(space: 'CONTENTFUL_SPACE', access_token: 'CONTENTFUL_ACCESS_TOKEN' ) do |preview|
   @article = preview.entries('ENTRY_ID').first
end

@dlitvakb
Copy link
Contributor

dlitvakb commented Dec 19, 2016

with_preview(space: 'CONTENTFUL_SPACE', access_token: 'CONTENTFUL_ACCESS_TOKEN' ) do |preview|
   @article = preview.entries('sys.id' => 'ENTRY_ID').first
end

Cheers

@martzoukos
Copy link
Author

Ah, apologies. I should have looked here first.

The object I'm fetching in this way, though, has a different schema than the content that's saved in my .yml files.
For example, if a content item has an image field, this is saved in the data folder as:

:image:
  :url: "//images.contentful.com/..."

so I can then access it in my view as

article.image.url

When I switch to the Preview API, though, then I need to change my code to access the same property:

article.main_image.fields[:file].url

If I want to easily switch between production and preview mode in a view, this makes it very hard, if not impossible to access the properties in the same way. Is there any way to work around this?

Thank you again.

@dlitvakb
Copy link
Contributor

dlitvakb commented Dec 20, 2016

Hey @martzoukos,

You can do main_image.image_url I know it's a bit different from the middleman API, I will add a patch release adding an alias for image_url to be url so this is no longer an issue.

Cheers

Update: The PR has been created, you can follow up here: contentful/contentful.rb#112

@dlitvakb
Copy link
Contributor

Release v1.2.0 of contentful.rb has been done, this will allow you to use main_image.url.

Cheers

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

No branches or pull requests

2 participants