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.

if statement and preload_replace problem

January 17, 2012 8:54am

Subscribe [4]
  • #1 / Jan 17, 2012 8:54am

    Origin Media

    20 posts

    I have the following code:

    {if group_id == "1"}
     {preload_replace:set_status="Open|Draft"}
    {if:else}
     {preload_replace:set_status="Open"}
    {/if}
    
    {group_id}
    {set_status}
    
    {exp:channel:entries channel="products" status="{set_status}"...

    The problem I am having is that whatever group_id is, set_status is always “Open”.

    I gather that this is because of the order that things are parsed?

    What possible solution is there for this? Is it possible to set variables based on the outcome of an if statement?

    Additional:

    I am trying to achieve this:

    http://richhayler.com/articles/article/display_draft_entries_before_publishing_live

    I presume the author managed to get it to work but from my tests it seems to be fundamentally flawed unless I am missing something obvious

  • #2 / Jan 17, 2012 10:24am

    Boyink!

    5011 posts

    I’m guessing it’s a parse order issue.  Here’s a doc that outlines in what order EE parses out templates:

    http://loweblog.com/downloads/ee-parse-order.pdf

  • #3 / Jan 17, 2012 10:33am

    According to Low, who wrote the “definitive” blog post on the subject, your code could be simplified a bit like this:

    {if group_id == "1"}
     {preload_replace:set_status="Open|Draft"}
    {/if}
    
    {preload_replace:set_status="Open"}

    The relevant part of that post is:

    if you add multiple values of the same preload replace variable in one template, the first one is evaluated, the rest is ignored.

    I’d make sure that your conditional is being evaluated as you’d expect it to as well, for instance, if you run this, does it display “The group ID is 1”?

    {if group_id == "1"}
     The group ID is 1
     {preload_replace:set_status="Open|Draft"}
    {/if}
  • #4 / Jan 17, 2012 12:30pm

    Low

    407 posts

    Actually, the problem here is the conditional. “group_id” is not an early parsed variable, so the conditional will not resolve in time to set the preload_replace variable to the correct value. You either need to enable PHP (on input) in your template to get to the group_id early, or use an add-on like Low Variables (blatant pitch, I know) to set them as early parsed variables (then available as {logged_in_group_id}).

  • #5 / Jan 18, 2012 11:38am

    Origin Media

    20 posts

    Thanks for the reply PressEnter. I did ensure that the group id was indeed one and did many tests however it was not changing the value of set_status.

    It does seem that this is indeed a parsing problem. As far as I can make out that group_id (and member_group) will not be parsed in time to be able to set the preload_replace variable correctly which is why I am getting some strange results as Lodewijk pointed out.

    I am intrigued how to use php to get the group_id early? I have enabled PHP on input and tried but have not been able to do this.

    I attempted:

    <?php
    $group_id = "{group_id}"; 
    echo $group_id;
    ?>

    This outputs to the browser but once again because of the parsing issue I can not set my preload_replace variable so I am back where I started!

    How can I access the group_id variable using PHP to achieve this?

  • #6 / Jan 18, 2012 2:44pm

    Dan Decker

    7338 posts

    Hi Origin Media,

    PHP won’t get you there either, as {group_id} is still parsed *after* PHP regardless of input or output. Low’s suggestion of using Low Variables to move {group_id} up the chain.

    Cheers!

  • #7 / Jan 19, 2012 6:22am

    Origin Media

    20 posts

    Yeah, I realize the way I have done that wouldn’t work. Lodewijk just hinted there maybe another way to get the variable into php early. Someone showed me how to extract segments early with this code:

    <?php
    $this->EE =& get_instance();
    $seg1 = $this->EE->uri->segment(1);
    ?>

    So I thought there maybe a way to get the group_id early???

    I will consider purchasing Low Variables but to be honest I have seen a drafts/workflow plugin for a similar price which I may as well purchase as that is what I was trying to achieve myself.

  • #8 / Jan 26, 2012 6:29pm

    Sean C. Smith

    3818 posts

    Origin Media,

    It looks like you’ve got a couple of options to look at. Have you resolved this issue now?

    Sean

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

ExpressionEngine News!

#eecms, #events, #releases