# The Link Field - pulling related data

**URL:** <https://community.hubspot.com/t/the-link-field-pulling-related-data/42207>\
**Category:** CMS Development\
**Created:** [August 12, 2021, 8:30am UTC](https://community.hubspot.com/t/the-link-field-pulling-related-data/42207 "2021-08-12T08:30:00Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Oloph](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/oloph/32/44643_2.png) [@Oloph](https://community.hubspot.com/u/Oloph)\
**Post date:** [August 12, 2021, 8:30am UTC](https://community.hubspot.com/t/the-link-field-pulling-related-data/42207/1 "2021-08-12T08:30:00Z")

</div>

Hi forum!

The HubL link field looks like a handy shortcut to allow content editors to link to pages, blogs, files and external links all from one field. My question is: is it possible to extract all sorts of data from the selected item?

Say that a content editor selects “Content” and picks a page. The default snippet spits out

`{% set href = module.link_field.url.href %}`

thus providing the link to the content. But I also want the title, name, featured image, author and so on. Is there some magic that can achieve this?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![miljkovicmisa](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/miljkovicmisa/32/86357_2.png) [@miljkovicmisa](https://community.hubspot.com/u/miljkovicmisa)\
**Post date:** [August 12, 2021, 11:44am UTC](https://community.hubspot.com/t/the-link-field-pulling-related-data/42207/2 "2021-08-12T11:44:56Z")

</div>

Hello @Oloph and thank you for writing in the forum, this is definately possible but you will have to use another function to achieve it, it’s called ‘content\_by\_id’ and its parameter is the id of the page wich you can retrieve from the link module field like this: “module.link\_field.url.content\_id”.  
So in order to achieve all of the above your code will look like this:

```lia-code-sample
{# this is how we get all the content of the page #}
{% set my_content = content_by_id(module.link_field.url.content_id) %}

{# this will output json string with ALL the data from the page #}
{# {{ my_content|pprint }} #}

{# below are some data extracted like you mentioned #}
{{my_content.featuredImage}} <br/>
{{my_content.name}} <br/>
{{my_content.authorName}}

```

Learn more about the [get\_content\_by\_id function in this link](https://developers.hubspot.com/docs/cms/hubl/functions#content-by-id).  
_ **If my answer was helpful please mark it as a solution.** _

---

<div class="post-metadata">

**Author:** ![Oloph](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/oloph/32/44643_2.png) [@Oloph](https://community.hubspot.com/u/Oloph)\
**Post date:** [August 13, 2021, 7:26am UTC](https://community.hubspot.com/t/the-link-field-pulling-related-data/42207/3 "2021-08-13T07:26:44Z")

</div>

Hi Milos, thank you for taking the time to help me out! As I understand this, there are separate functions to retrieve the content for pages, blogs, and files respectively. That’s why I have split it out like below in the module I’m drafting (work in progress by a self-taught noob, excuse the mess please!).

```lia-code-sample
{% for item in module.entries %}
 {# this is how we get all the content of the linked page #}
 {% set contentid = item.link_field.url.content_id %}
 {% set page = content_by_id(contentid) %}
 {% set blog = blog_by_id(contentid) %}
 {% set recentblog = blog_recent_posts(contentid, 1) %}
 {% set file = file_by_id(contentid) %}

 <a href="{{ item.link_field.url.href }}" {% if item.link_field.open_in_new_tab %}target="_blank"{% endif %}>

 {# PAGE, BLOG POST #}
 {% if item.link_field.url.type == 'CONTENT' %}
 {# this will output json string with ALL the data from the page #}
 ContentID variable: {{ page|pprint }}<br/>
 ID: {{ page.id }}<br/>
 {% if item.featured_image %}Featured image: <img src="{{page.featured_image}}"> <br/>{% endif %}
 Name: {{page.name}} <br/>
 Title: {{ page.title }} <br/>
 Author: {{page.blogAuthor}}

 {# BLOG #}
 {% elif item.link_field.url.type == 'BLOG' %}
 {% for post in recentblog %} 
 ContentID variable:{{ blog|pprint }}<br/>
 ID:{{ blog.id }}<br/>
 {% if item.featured_image %}Featured image: <img src="{{post.featured_image}}"> <br/>{% endif %}
 Title: {{ post.title }} <br/>
 Author: {{post.blogAuthor}}
 {% endfor %}

{# File #}
 {% elif item.link_field.url.type == 'FILE' %}
 ContentID variable: {{ file|pprint }} <br/>
 ID: {{ file.id }}<br/>
 {% if item.featured_image %}Featured image: <img src="{{page.featured_image}}"> <br/>{% endif %}
 Name: {{file.name}}}<br/>
 Type: {{file.extension}} <br/>
 Path: {{ file.url }} <br/>
 Size: {{ file.size|filesizeformat }}

 {% endif %} 
 </a>
<p> ______________________ </p>
{% endfor %}

```

But I’m having trouble with getting the file data - nothing renders when a file is selected with the above. If I punch in a static fileID in the file variable (line 7) it works as expected, e.g.:

` {% set file = file_by_id(35696060089) %}`

Any idea why it’s not working dynamically?

I’d also happily accept feedback on a smarter way to deal with the different link types!

Cheers! Oloph

---

<div class="post-metadata">

**Author:** ![Oloph](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/oloph/32/44643_2.png) [@Oloph](https://community.hubspot.com/u/Oloph)\
**Post date:** [August 12, 2021, 11:57am UTC](https://community.hubspot.com/t/the-link-field-pulling-related-data/42207/4 "2021-08-12T11:57:45Z")

</div>

You are the best! I’ll try this out and get back for support if you dont mind:)

Big thanks - Oloph

---

<div class="post-metadata">

**Author:** ![miljkovicmisa](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/miljkovicmisa/32/86357_2.png) [@miljkovicmisa](https://community.hubspot.com/u/miljkovicmisa)\
**Post date:** [August 13, 2021, 8:13am UTC](https://community.hubspot.com/t/the-link-field-pulling-related-data/42207/5 "2021-08-13T08:13:27Z")

</div>

Hello @Oloph , first of all, you’ve done a great job with those variables and if statements, well done!  
The issue with the file is that the `{% set contentid = item.link_field.url.content_id %}` for the file is returning NULL, which means that when you call the `file_by_id(contentid)`

function the `contentid` variable is null istead of an actual id of the file, that’s why it works when you put an actual id inside, **so this is an issue with the link field not bringing in the content\_id of a file but for other types it does, @dennisedson are you aware of any way to report this issue?**  
As for the code you shared the only thing I would change is putting all the variables for the various functions inside their respective `if` statements. This is because all those functions are called even if you don’t use them, so it’s better to be called only when you need them, in the if statement they won’t be parsed unless the statement if true. So your code would become something like below (I’ve added some comments regarding the file\_id):

```lia-code-sample
{% for item in module.entries %}
 {# this is how we get all the content of the linked page #}

 {# this is null if item.link)field.url.type == 'FILE' #}
 {% set contentid = item.link_field.url.content_id %}

 <a href="{{ item.link_field.url.href }}" {% if item.link_field.open_in_new_tab %}target="_blank"{% endif %}>

 {# PAGE, BLOG POST #}
 {% if item.link_field.url.type == 'CONTENT' %}
 {% set page = content_by_id(contentid) %}
 {# this will output json string with ALL the data from the page #}
 ContentID variable: {{ page|pprint }}<br/>
 ID: {{ page.id }}<br/>
 {% if item.featured_image %}Featured image: <img src="{{page.featured_image}}"> <br/>{% endif %}
 Name: {{page.name}} <br/>
 Title: {{ page.title }} <br/>
 Author: {{page.blogAuthor}}

 {# BLOG #}
 {% elif item.link_field.url.type == 'BLOG' %}

 {% set blog = blog_by_id(contentid) %}
 {% set recentblog = blog_recent_posts(contentid, 1) %}

 {% for post in recentblog %} 
 ContentID variable:{{ blog|pprint }}<br/>
 ID:{{ blog.id }}<br/>
 {% if item.featured_image %}Featured image: <img src="{{post.featured_image}}"> <br/>{% endif %}
 Title: {{ post.title }} <br/>
 Author: {{post.blogAuthor}}
 {% endfor %}

{# File (nothing inside of this will work because 'file' variable is null) #}
 {% elif item.link_field.url.type == 'FILE' %}

 {{ this doesn't work because contentid variable is actually null for file types }}
 {% set file = file_by_id(contentid) %}

 ContentID variable: {{ file|pprint }} <br/>
 ID: {{ file.id }}<br/>
 {% if item.featured_image %}Featured image: <img src="{{page.featured_image}}"> <br/>{% endif %}
 Name: {{file.name}}}<br/>
 Type: {{file.extension}} <br/>
 Path: {{ file.url }} <br/>
 Size: {{ file.size|filesizeformat }}

 {% endif %} 
 </a>
<p> ______________________ </p>
{% endfor %}

```

---

<div class="post-metadata">

**Author:** ![Oloph](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/oloph/32/44643_2.png) [@Oloph](https://community.hubspot.com/u/Oloph)\
**Post date:** [August 13, 2021, 8:55am UTC](https://community.hubspot.com/t/the-link-field-pulling-related-data/42207/6 "2021-08-13T08:55:44Z")

</div>

Thank you for your encouragement and your suggestions, very helpful!

So am I correct in assuming that

`{% set contentid = item.link_field.url.content_id %}`

does not work as intended when

`item.link_field.url.type == 'FILE'`

and that this is a bug within the Hubspot core? I have been playing around with

`{% set contentid = item.link_field.url.file_id %}`

and other random expressions, but I’m just shooting in the dark.

The alternative to make the module flexible enough is to build it out with a choice field and dedicated fields for each case, but I loved the idea of using just the link field to take care of all of it.

Anyway, thank you again, and I’d be happy for this to be reported if it is in fact a bug.

Take care and enjoy the coffee - Oloph

---

<div class="post-metadata">

**Author:** ![miljkovicmisa](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/miljkovicmisa/32/86357_2.png) [@miljkovicmisa](https://community.hubspot.com/u/miljkovicmisa)\
**Post date:** [August 13, 2021, 9:02am UTC](https://community.hubspot.com/t/the-link-field-pulling-related-data/42207/7 "2021-08-13T09:02:45Z")

</div>

@Oloph , there is actually no need for shooting in the dark, the way this is debugged easily is by just outputting the data of the link field like so :

`{{ item.link_field | pprint }}`

This will help you “see” inside the link field an check what it brings to you!  
Happy to be helpful,  
Cheers! 😉

---

<div class="post-metadata">

**Author:** ![dennisedson](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/dennisedson/32/28044_2.png) [@dennisedson](https://community.hubspot.com/u/dennisedson)\
**Post date:** [August 13, 2021, 3:59pm UTC](https://community.hubspot.com/t/the-link-field-pulling-related-data/42207/8 "2021-08-13T15:59:31Z")

</div>

@miljkovicmisa , would you happen to have an example of the bug in your portal? If so, mind sending a link to me and I will check it out 😀

---

<div class="post-metadata">

**Author:** ![miljkovicmisa](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/miljkovicmisa/32/86357_2.png) [@miljkovicmisa](https://community.hubspot.com/u/miljkovicmisa)\
**Post date:** [August 13, 2021, 4:11pm UTC](https://community.hubspot.com/t/the-link-field-pulling-related-data/42207/9 "2021-08-13T16:11:44Z")

</div>

@dennisedson Yes! [This is the link from my test portal](https://19569395.hs-sites.com/testing-link-data)

---

<div class="post-metadata">

**Author:** ![Oloph](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/oloph/32/44643_2.png) [@Oloph](https://community.hubspot.com/u/Oloph)\
**Post date:** [August 16, 2021, 9:42am UTC](https://community.hubspot.com/t/the-link-field-pulling-related-data/42207/10 "2021-08-16T09:42:27Z")

</div>

OK, one last try: now I’m using a File field instead of the Link field for files. But I am still unable to get the file ID using

` {% set fileid = item.file_field.url.content_id %}`

Is there no way to get the ID and metadata of a target file in a module in Hubspot?

Best regards, Oloph
