Skip to content

Instantly share code, notes, and snippets.

@christopherhill
Last active December 19, 2015 14:18
Show Gist options
  • Save christopherhill/5967901 to your computer and use it in GitHub Desktop.
Save christopherhill/5967901 to your computer and use it in GitHub Desktop.
Drupal block of insertable code to query a content type against a field value and return matching field values. In this case, use the Smart IP module to grab the user country, and find international fields.
<?php
// get country (requires Smart IP installed)
$smart_ip_session = smart_ip_session_get('smart_ip');
$user_country = $smart_ip_session['location']['country'];
// execute a query against the international_data content type using field_location_name
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'international_data')
->fieldCondition('field_location_name', 'value', $user_country);
$result = $query->execute();
if (!empty($result)) {
$nids = array_keys($result['node']);
$node = node_load($nids[0]); // loads only the first result
$fieldValueA = $node->field_value_a[$node->language][0]['value'];
$fieldValueB = $node->field_value_b[$node->language][0]['value'];
}
echo '<h2>' . $fieldValueA . '</h2>';
echo '<h2>' . $fieldValueB . '</h2>''
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment