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?
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:
{# 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}}
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!).
{% 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!
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):
{% 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 %}
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.
@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!