Hi,

I have a country field used in Drupal's default user account fields.

I am printing this user info in my comment.tpl through variables created in template.php through the user_load function.

I am able to print the ISO2 and can therefore use the theme function to print the flag icon, but I'd also like to print the country name. This information doesn't seem to be available to me when I dpm in Devel.

Here is the relevant part of the function in template.php:

function mytheme_preprocess_comment(&$variables) {
  $comment = $variables['comment'];
  $user = user_load($comment->uid);
  $country = field_get_items('user', $user, 'field_country');
  if ($country) {
    $variables['user_country_name'] = $country[0]['iso2'];
    $variables['user_country_flag'] = theme('countryicons_icon', array('code' =>  $country[0]['iso2'], 'iconset' =>  'shiny'));   
  }
  else {
    $variables['user_country_name'] = '';
  }

If anyone could help me out with this I'd really appreciate it.

Comments

Alan D.’s picture

Component: Code » Documentation
Category: bug » support
Status: Active » Fixed

Untested:

  $comment = $variables['comment'];
  $user = user_load($comment->uid);
  if ($country_items = field_get_items('user', $user, 'field_country')) {
    $country_item = array_shift($country_items);
    if ($country = country_load($country_item['iso2'])) {
      $variables['user_country_name'] = $country->label();
      $variables['user_country_flag'] = theme('countryicons_icon', array('code' => $country->iso2, 'iconset' =>  'shiny'));  
    }
  }
  $variables += array(
    'user_country_name' => '',
    'user_country_flag' => '',
  );

I thought that there was a country load in the field load, if so, then this should be in $user->field_country['und'][0]['country']. Doing a dpm($user) would quickly reveal this.

Reopen if you need more help.

Jill L’s picture

country_load was exactly what I needed...thank you for the help!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.