Since HubSpot doesn't have that inbuilt support, what I am trying to do is:
Choice field with all Authors as options to choose from.
Get Meta of Selected Author like Avatar, Slug, etc.
Once the co-author is selected via the choice field, my intention is to compare it against the authors' list again so that I can get Author Avatar, Author Slug (I hope so). I am not concerned whether the co-author will be credited for the post, at the minimum I need some basic Author meta-data for display.
The choice field is displaying choice and I can get the value of the selection.
Now, what I am trying to do to get author avatar and slug is
1. get all authors in sequence var all_auth
2. get the choice field value (auth id), var co_auth
3. loop through the sequence
for item in all_auth
if co_auth == item.id <-- get the author object
set co_auth = item <-- update the variable with the matched author object - possible typecasting issue
endif
endfor
4. Get co_auth.avatar & co_auth.slug
I am not having any luck in the iterative loop and if statement so far.
I just need the selected author from choice to be listed with Author Avatar, Slug so if someone clicks the URL he or she is able to find other live posts of the author.
I dug into your code for a while, but I ended up starting fresh in my own portal. Here's the solution I managed to come up with:
{# Initailize an empty array to store the choices #}
{% set author_choices = [] %}
{# loop through blog's authors #}
{% for author in blog_authors('default', 250) %}
{# in each loop iteration, create a value/label pair with the author's display name & id #}
{# Each pair should look like this: ["Display Name","123456"]#}
{% set author_name_id = '["'+author.id+'","'+author.display_name+'"]' %}
{# Add the author's name and id to author_choices#}
{% set author_choices = author_choices + author_name_id %}
{# On the last loop iteration, create the choice module with the list of authors #}
{% if loop.last %}
{% choice "co_author" label='Select Co-Author', export_to_template_context=True, value='', choices={{author_choices}} %}
{% endif %}
{% endfor %}
{# This macro loops through a blog's authors and prints info on the author with the provided ID #}
{% macro print_author_info(author_id) %}
{% for author in blog_authors('default', 250) %}
{% if author.id == author_id %}
{# Everything inside this if statement is just example markup; here's where you'd put your co-author markup #}
<div style="background: AliceBlue;">
<h3>{{ author.display_name }}</h3><img src="{{ author.avatar }}" style="max-width:50px;">
<p>{{ author.bio }}</p>
</div>
{% endif %}
{% endfor %}
{% endmacro %}
{# Finally, you can call the macro wherever you want your co-author markup to appear in your template #}
{{ print_author_info(widget_data.co_author.value) }}
Let me know if this works for you, or if you have any other questions!
I dug into your code for a while, but I ended up starting fresh in my own portal. Here's the solution I managed to come up with:
{# Initailize an empty array to store the choices #}
{% set author_choices = [] %}
{# loop through blog's authors #}
{% for author in blog_authors('default', 250) %}
{# in each loop iteration, create a value/label pair with the author's display name & id #}
{# Each pair should look like this: ["Display Name","123456"]#}
{% set author_name_id = '["'+author.id+'","'+author.display_name+'"]' %}
{# Add the author's name and id to author_choices#}
{% set author_choices = author_choices + author_name_id %}
{# On the last loop iteration, create the choice module with the list of authors #}
{% if loop.last %}
{% choice "co_author" label='Select Co-Author', export_to_template_context=True, value='', choices={{author_choices}} %}
{% endif %}
{% endfor %}
{# This macro loops through a blog's authors and prints info on the author with the provided ID #}
{% macro print_author_info(author_id) %}
{% for author in blog_authors('default', 250) %}
{% if author.id == author_id %}
{# Everything inside this if statement is just example markup; here's where you'd put your co-author markup #}
<div style="background: AliceBlue;">
<h3>{{ author.display_name }}</h3><img src="{{ author.avatar }}" style="max-width:50px;">
<p>{{ author.bio }}</p>
</div>
{% endif %}
{% endfor %}
{% endmacro %}
{# Finally, you can call the macro wherever you want your co-author markup to appear in your template #}
{{ print_author_info(widget_data.co_author.value) }}
Let me know if this works for you, or if you have any other questions!
Many thanks for your time and effort for looking into this. I was missing the extra loop to search again in blog authors instead I was directly assigning the object, which was not working.
Thanks for your patience here; I must have missed this post. I'm not seeing anything obviously wrong with your HubL code; can you send me a link to the blog you're working on so that I can take a closer look?