CMS Development

NPeters
Contributeur

Unable to get Page field to pull in page data. Docs for content_by_id() not matching output

Résolue

I have created a module that uses the Page field.

 

The aim is populate a block on a landing page that has the page title, and page link. Would be great to also pull in a cover image.

 

I have followed the docs here:

https://developers.hubspot.com/docs/cms/building-blocks/module-theme-fields#page

 

Tells me to expect an id to be returned. That is not true in my case. The output I get is some thing like:

{url={type=CONTENT,%20content_id=47075729930,%20href=http://19957450.hs-sites.com/-temporary-slug-cce86271-271e-4ab4-a1b4-60b8ce2dab52},%20open_in_new_tab=false,%20no_follow=false}

To access the id, it appears I need to do something like:

{% set my_content = content_by_id(article.page_link.url.content_id) %}

 Seems very verbose, but it is what it is. I move on.

 

The docs for content_by_id show that I should be able to access the keys 'absolute_url' and 'title'.

https://developers.hubspot.com/docs/cms/hubl/functions#content-by-id

 

None are avaliable when I try

<a href="{{ my_content.absolute_url }}">{{my_content.title}}</a>

When I try to inspect 'my_content' to see if anything will return, I get a string with the output:

Page 47075729930 Blog - Meet our new team members

  So im guessing its a page type + page id + page title.

 

Am I reading the docs wrong?

0 Votes
1 Solution acceptée
dennisedson
Solution
Équipe de développement de HubSpot
Équipe de développement de HubSpot

Unable to get Page field to pull in page data. Docs for content_by_id() not matching output

Résolue

@NPeters , when in doubt, pprint.

I believe this is what it should be {{ content_by_id(module.page_field).featuredImage }}

But you should try {{ content_by_id(module.page_field)|pprint }} and see what that spits out in your preview.  Should be a giant json object with everything available

Voir la solution dans l'envoi d'origine

3 Réponses
NPeters
Contributeur

Unable to get Page field to pull in page data. Docs for content_by_id() not matching output

Résolue

Hi had a facepalm moment.

 

And what I have realised  is I have been caught up in between the differences of a "Page" field and a "Link".

 

In my mind I had used a "Link" field because I wanted to link to a page within the CMS. So I can see the docs show what will be returned.

 

When I do use the "Page" field. Everything gets returned as expected.

 

I guess while I have you, I am keen to find a way to pull in a featured image for that page. Is this possible?

 

Thanks for your patience.

dennisedson
Solution
Équipe de développement de HubSpot
Équipe de développement de HubSpot

Unable to get Page field to pull in page data. Docs for content_by_id() not matching output

Résolue

@NPeters , when in doubt, pprint.

I believe this is what it should be {{ content_by_id(module.page_field).featuredImage }}

But you should try {{ content_by_id(module.page_field)|pprint }} and see what that spits out in your preview.  Should be a giant json object with everything available

dennisedson
Équipe de développement de HubSpot
Équipe de développement de HubSpot

Unable to get Page field to pull in page data. Docs for content_by_id() not matching output

Résolue

Hey @NPeters 

Everything is working as expected in my portal...this is the code block I am using:

// below is to see what is output by the field alone
{{ content_by_id(module.page_field) }} 

// below is getting a li with the content title and url set by the field
{% set my_content = content_by_id(module.page_field) %}
<ul>
    <li>
        <a href="{{ my_content.absolute_url }}">{{my_content.title}}</a>
    </li>
</ul>

Could you add an example of your module html file?