CMS Development

praveen
Participant

Dropdown Choice field with all Authors to choose from in Custom Module

Hi,

I am trying to get multiple people listed in any blog post as co-author or contributor.

 

What I am trying to do is to create a custom module with dropdown list of authors (similar to how you select Author before publishing the blog in HubSpot) 

Choice Dropdown I am trying to achieveChoice Dropdown I am trying to achieve

 

I am using blog_authors function to list all authors and loop it through to populate the choices, but I am not having any luck with this yet. I don't want to lose all the meta-data associated with the author like author_url, avatar, etc. otherwise I would have given a simple text field to put the name.

 

Any suggestion regarding how I may achieve this?

 

Thanks in advance.

 

- Praveen

0 Upvotes
3 Replies 3
praveen
Participant

Dropdown Choice field with all Authors to choose from in Custom Module

In an older thread, I found the following logic but I am not able to make it work.

 

The modified code I am using is

{% set ta_authors = blog_authors('default', 250) %}

{% macro keyValues() %}
	[{% for author in ta_authors %}[\"{{ author.id }}\", \"{{ author.display_name }}\"]{% if !loop.last %},{% endif %}{% endfor %}]
{% endmacro %}
{% set keyValues = keyValues() %}
    {{ keyValues }}

{% choice "co_author" label='Select Co-Author', export_to_template_context=True, value='', choices='{{ keyValues }}' %}

I am using this in the template, not in a custom module.

 

So, in editor mode, instead of populated choices what I am getting like the following image.Screen Shot 2018-08-08 at 11.18.23 PM.png

 

I've tried with different values like author.id and author.display_name but the result is the same.

If I print {{ keyValues }} the output is normal (I guess)

[[\"3457941083\", \"abbe vogels\"],[\"4756481638\", \"Alhad Akole\"],[\"4212980404\", \"Amitabh Mendiratta\"],[\"3457941158\", \"Art Kleiner\"],[\"6004790340\", \"Ashish K\"],[\"5229291537\", \"Bajrang Bahadur\"],[\"3583596640\", \"casey dilloway\"],[\"3457941173\", \"Chaithra Rajanna\"],[\"3392944889\", \"chris clark\"],[\"3392944864\", \"christopher\"],[\"5638749441\", \"Dhinoj Dings\"],[\"3457941138\", \"elyn andersson\"],[\"3392944874\", \"Gene Bellinger\"],[\"3457941163\", \"Ipsita Priyadarshini\"],[\"3457941153\", \"Jake Jacobs\"],[\"3416538216\", \"Kerry Sinclair\"],[\"3583596630\", \"Lindsey Rich\"],[\"3583596625\", \"Matt Sadinsky\"],[\"3583596620\", \"Miriam Hawley\"],[\"3392944894\", \"Miriam Messinger\"],[\"4810204355\", \"Namrata Datta\"],[\"4133860139\", \"Naveen Tiwari\"],[\"3950479065\", \"Navya BJ\"],[\"4807055205\", \"Nikhil Rajan\"],[\"3583596645\", \"Ruth Sirman\"],[\"5506564670\", \"Sarthak Gupta\"],[\"4755706965\", \"Scott Downs\"],[\"5978256209\", \"Shivali Joshi\"],[\"3392944869\", \"Siraj Sirajuddin\"],[\"5846967547\", \"Sriharsha B\"],[\"3457941068\", \"stiaadmin\"],[\"3583596635\", \"Susan Gibson\"],[\"5254666421\", \"Vandana Sinha\"],[\"3392944879\", \"Victor Garcia\"]]

Also, if I use static value instead of {{ keyValues }} the choice drop-down display fine.

Any idea what I am missing here?

0 Upvotes
lgerodiaz
Contributor

Dropdown Choice field with all Authors to choose from in Custom Module

Hey there,

 

Have you tried calling it directly as a macro? Perhaps that would work.

{% choice "co_author" label='Select Co-Author', export_to_template_context=True, value='', choices='{{ keyValues() }}' %}

Also, it might be a good practice to give your variable and macros some other distinct yet meaningful names other than keyValues itself.

 

L

praveen
Participant

Dropdown Choice field with all Authors to choose from in Custom Module

Hi L,

 

Thanks for the reply.

It didn't work, see the screenshot below

Screen Shot 2018-08-13 at 6.17.05 PM.png

 

 

So, meanwhile, I am trying to achieve it by providing a static list in choice and then combining it with content.blog_post_author value.

 

- Praveen

 

 

0 Upvotes