1

How do I display the currently selected category for the channel entry inside a Safecracker form?

I tried:

{categories}
    {if checked}
       {category_name}
    {/if}
{/categories}

I also tried {if checked != ''}, and the same with {selected} instead of {checked}.

Any ideas?

2
  • What happens if you try them as variables instead of conditionals: {categories} {selected} {category_name} {/categories}? Are they blank? Apr 3 '13 at 15:04
  • I believe the {selected} variable will output the text selected="selected", since it looks like it's meant to be used to loop through a set of form fields.
    – kmgdev
    Apr 3 '13 at 16:12
1

Have you tried something like this:

<select name="my_field_name" id="categories" >
    <option value="option_value" {if my_field_name == "option_value"}selected="selected"{/if}>option_name</option>
</select>

So you break it out a little more then can through in conditional checking on each option value.

2
  • My goal is actually just to output the name of the currently selected category as plain text. I'm not trying to make it an editable field inside Safecracker. In your example, just putting the variable {selected} instead of the conditional will accomplish the same thing: <option value="option_value" {selected}>Option Name</option>
    – kmgdev
    Apr 3 '13 at 17:22
  • Yeah from there you can just edit out the field itself and leave in the conditional and that should work. If so then don't forget to mark the answer as accepted.
    – Ian
    Apr 3 '13 at 18:51
1

I got it to work by using an embedded template:

The main template:

{exp:safecracker
    channel='mychannel'
    url_title='{segment_3}'
    return='site/success'
}

    {!-- Safecracker Fields --}

    {embed='site/entry_category' entry_id='{entry_id}'}

    {!-- More Safecracker Fields --}

{/exp:safecracker}

The embedded template (site/entry_category):

{exp:channel:entries
    channel='mychannel'
    entry_id='{embed:entry_id}'
    disable='member_data|pagination|custom_fields'
    limit='1'
}
    {categories backspace='2'}{category_name}, {/categories}
{/exp:channel:entries}

This works because embedded templates are parsed after module tags (like Safecracker). So EE loops through the Safecracker tag and performs all the functions inside it, then, when it's done, it loads the embedded template and processes the tags inside that. So the two don't interfere with each other and I get my entry's selected categories.

0

The {checked} variable is either empty or contains checked="checked". to check it in a conditional you have to check that it is not empty, and because it contains quotes the conditional needs to be in single quotes. The following should work:

{categories}
  {if '{checked}' != ''}
     {category_name} 
  {/if}
{/categories}
0

I just encountered a similar situation where I'm in a safecracker tag pair and want to output the selected categories of the entry. GDmac's comment above solved it perfectly, thanks GDmac (I had tried {if {selected}} to no avail.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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