Drupal Dev Thursdays: Post here with development questions or discussion

Relatively big question:

How do I prevent fields from a field collection that don't pass the conditional statement from appearing? I've built out a function in a field_collection template file that checks the user's IP address, and displays fields with markup from that field collection based on the condition. However, fields from the field collection that I don't want rendered are also appearing on the page underneath the desired fields for some reason.

This is the php code I put in the field collection template file:

$ipvalue = findCountryByIP(); 
$cookie_value = $ipvalue;
$location = '';
if (in_array($cookie_value, ['SV'],true)) {
   $location = 'Latin America';
} else {
    $location = 'Not Latin America';
}

if ($content['field_location_one']['#items'][0]['value'] == '1' && $location == 'Not Latin America') {?>
--markup and fields go here--
<? } else if ($content['field_location_two']['#items'][0]['value']== '1' && $location == 'Latin America') { ?>
--markup and fields go here-- 
<?php } ?>

This all works how I want it to, aside from the fact that un-treated fields are also rendering out after all of the code above. How do I remove these unwanted fields from appearing?

/r/drupal Thread