1

I'm trying to show a contact form based on the category that an entry is a member of, i'm using the freeform plugin to generate forms and then have a category group (an id of 7) with three categories in (ids of 41,42,43).

Here's the current code:

           {exp:channel:entries}
             {categories show_group="7"}
                {if "{category_id}" == "42"}
                  {exp:freeform:form form_id="6" return="thanks/index"}
                {/if}
                {if "{category_id}" == "41"}
                  {exp:freeform:form form_id="7" return="thanks/index"}
                {/if}
                {if "{category_id}" == "42"}
                  {exp:freeform:form form_id="8" return="thanks/index"}
                {/if}
              {/categories}
{freeform:all_form_fields}
                    <div class="field-group type-{freeform:field_type} field-{freeform:field_name}">
                      <label>{freeform:field_label}</label>
                      {freeform:field_output}
                    </div>
                  {/freeform:all_form_fields}
                  <p>{freeform:submit}</p>
              {/exp:freeform:form}
            {/exp:channel:entries}

This however is simply trying to output all three forms and only showing the first one on every page no matter which categories they are assigned to. Any ideas how I should go about this? I'm guessing it's parsing the freeform tag before the conditional at the moment but not sure how to fix it?

3
  • Please, update your question. Where is the closing tag of freeform?
    – Sobral
    Oct 22, 2013 at 12:06
  • Hi, I have the following beneath the categories but it's not really relevant to my question:
    – Nick
    Oct 22, 2013 at 12:29
  • This made a lot of difference to my answer, actually.
    – Sobral
    Oct 22, 2013 at 12:51

1 Answer 1

2

You need to make sure all data of exp:channel:entries is parsed before exp:freeform. This is almost exactly the point of the sample code in docs about parse="inward".

Try this, please:

{exp:channel:entries parse="inward"}
    {categories show_group="7"}
        {if "{category_id}" == "42"}
            {exp:freeform:form form_id="6" return="thanks/index"}
        {if:elseif "{category_id}" == "41"}
            {exp:freeform:form form_id="7" return="thanks/index"}
        {if:elseif "{category_id}" == "42"}
            {exp:freeform:form form_id="8" return="thanks/index"}
        {/if}
    {/categories}
                {freeform:all_form_fields}
                    <div class="field-group type-{freeform:field_type} field-{freeform:field_name}">
                    <label>{freeform:field_label}</label>
                    {freeform:field_output}
                    </div>
                {/freeform:all_form_fields}
                <p>{freeform:submit}</p>
            {/exp:freeform:form}
{/exp:channel:entries}

This code just works because all conditionals are related to exp:channel:entries, so they can be parsed in the right order.

I changed the conditionals to elseif to prevent the case of some user to chose more than one category.

4
  • Can you use parse="inward" on the entries tag? Isn't it meant to be used on plugin tags?
    – Nick
    Oct 22, 2013 at 13:26
  • couldn't get this working, ended up having to use an embed: {embed="site/_application-form" category_id="{categories show_group="7"}{if category_id == "42"}6{/if}{if category_id == "41"}7{/if}{if category_id == "46"}8{/if}{/categories}"}
    – Nick
    Oct 22, 2013 at 13:52
  • Is there something to do with the fact that your code checks for category_id 42 twice? I just noted this.
    – Sobral
    Oct 22, 2013 at 14:55
  • Nick, can you post your solution as an answer?
    – Anna_MediaGirl
    Oct 23, 2013 at 4:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.