ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Limiting the related entries dropdown

December 14, 2010 1:28pm

Subscribe [2]
  • #1 / Dec 14, 2010 1:28pm

    jamestowers

    53 posts

    I’m trying to set up my entry publish forms, on the front end with Safecracker, so that users can relate entries from one channel to an entry in another - this is pretty simple…

    However, I need to limit the available entries in the relationship dropdown to only those the user has created.
    I’ve found this post http://ellislab.com/forums/viewthread/123776/ which does almost exactly what i want but I can’t get it to work in safecracker.

    Unfortunately this standard dropdown code doesn’t work for a relationship field in Safecracker:

    <label>Parent campaign:</label>
    <select name="asset_parent" id="asset_parent">
        {options:asset_parent}
            <option value="{option_value}"{selected}>{option_name}</option>
        {/options:asset_parent}
    </select>

    You have to use this instead:

    <label>Parent campaign:</label>
    {field:asset_parent}

    Rob at Barrett Newton suggested doing some sort of custom SQL query to achieve what i’m trying to do and suggested I ask in these forums to see if anyone could help..

    Here’s our convo: http://barrettnewton.com/forums/viewthread/1389/

    Any one got any ideas??

  • #2 / Dec 14, 2010 1:57pm

    jamestowers

    53 posts

    Well, the ever helpful Rob Sanchez has got back to me and provided the perfect solution so I thought I should post it here for others:

    This should do the trick. Make sure you have the query module enabled. Enable PHP in your template, parsed on input, and replace channel_id = 1 with the channel id of the relationship channel.

    <label>Parent campaign:</label>
    <select name="asset_parent" id="asset_parent">
            <option value="">--</option>
    {exp:query sql="SELECT entry_id AS rel_entry_id, title AS rel_title, (SELECT rel_child_id FROM exp_relationships WHERE rel_id = '{asset_parent}' LIMIT 1) AS rel_child_id FROM exp_channel_titles WHERE channel_id = 1 AND author_id = <?php echo $this->EE->session->userdata('member_id'); ?>"}
            <option value="{rel_entry_id}"{if rel_child_id == rel_entry_id} selected="selected"{/if}>{rel_title}</option>
    {/exp:query}
    </select>
  • #3 / Dec 15, 2010 3:51am

    John Henry Donovan

    12339 posts

    Glad Rob got you up and running again.Feel free to start a new thread if you have any more questions.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases