0

I have an Event entry which can has one child Location entry. The Location entry has two fields: Title and Floor. Sometimes floor is empty so I'm doing this in my template to see if there's a floor entered:

<h2>
  {event_location:title}
  {if event_location:floor}
    , {event_location:floor}
  {/if}
</h2>

...and it's not working. Title always shows up and Floor never does unless I take off the conditional, which I don't want to do because I don't want a comma followed by nothing when there's no floor. Any ideas?

2
  • Welcome to ExpressionEngine Answers huertanix! FYI : It's good form to mark the answer so others can find it, if you get a chance Wes's answer is bang on :)
    – Blatant
    Commented May 1, 2015 at 11:34
  • It's important to me that I verify that a solution works before I mark it as answered so that I don't mislead anyone with the same problem hoping this is a solution. Since the upgrade failed, I'll try doing surgery on core libraries as suggested and mark it as answered if it works.
    – huertanix
    Commented May 5, 2015 at 23:08

2 Answers 2

3

What version are you using? We fixed this problem in 2.9.3, though you can apply the solution if you'd rather not update.


Open system/expressionengine/libraries/channel_entries_parser/Preparser.php and find the _find_modified_conditionals() method around line 374, and replace it with this:

public function _find_modified_conditionals()
{
    $prefix = $this->_prefix;
    $unfiltered_all_field_names = array();
    $all_field_names = array();

    if (strpos($this->_tagdata, LD.'if') === FALSE)
    {
        return array();
    }

    foreach($this->_channel->cfields as $site_id => $fields)
    {
        $unfiltered_all_field_names = array_unique(array_merge($unfiltered_all_field_names, $fields));
    }

    // Do a rough cut to slim down the number of fields
    // else the string can be too long for the preg_match_all
    foreach (array_keys($unfiltered_all_field_names) as $name)
    {
        if (strpos($this->_tagdata, $name) !== FALSE)
        {
            $all_field_names[] = $name;
        }
    }       

    $modified_field_options = $prefix.implode('|'.$prefix, $all_field_names);
    $modified_conditionals = array();

    if (preg_match_all("/".preg_quote(LD)."((if:(else))*if)\s+(($modified_field_options):(\w+))(.*?)".preg_quote(RD)."/s", $this->_tagdata, $matches))
    {
        foreach($matches[5] as $match_key => $field_name)
        {
            $modified_conditionals[$field_name][] = $matches[6][$match_key];
        }
    }

    return array_map('array_unique', $modified_conditionals);
}
3
  • Ah, looks like I'm just one point behind at v. 2.9.2. I'll try upgrading and mark as answered if that works.
    – huertanix
    Commented May 4, 2015 at 0:20
  • Just spent the day upgrading to 2.10.1. No change.
    – huertanix
    Commented May 5, 2015 at 23:06
  • Looks like the code function is identical to what 2.10.1 has, so the problem must be elsewhere.
    – huertanix
    Commented May 5, 2015 at 23:36
0

I've found the right syntax to use from looking at this question: Conditionally display a field from a related entry if it's not empty (native relationships field). Basically, the field needs to be compared against an empty string as so:

<h2>
  {event_location:title}
  {if '{event_location:floor}' != ""}
    , {event_location:floor}
  {/if}
</h2>

Not remotely elegant, but this did the trick.

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.