The flag is great, but is there a way to also display the country name?

Comments

mrfelton’s picture

Status: Active » Fixed

You can do this by overriding the function theme_ip2cc_flag()

vako’s picture

Thanks for the reply. I would appreciate if you can provide a little bit of guidance in doing so please.

So far I have located the file - \ip2cc\contrib\ip2cc_node\ip2cc_node.module
and the section is:

function theme_ip2cc_flag($country, $addr = '') {
  if (!user_access('view country')) {
    return "";
  }
  if (isset($country)) {
    $cc = strtolower($country->country_code);
    if (empty($cc)) {
      $cc = "unknown";
    }
    $title = "$cc $country->country_name";
    if (user_access('view IP address')) {
      $title .= " $country->ip";
    }
    if (user_access('view network name') && $country->net_name != 'Unknown') {
      $title .= " $country->net_name";
    }
  }
  else {
    $cc = 'unknown';
    $title = (empty($addr))?'unknown':$addr;
  }
  $path = drupal_get_path('module', 'ip2cc_node') ."/flags/$cc.png";
  $attribs = array('width' => 14, height => 14);
  return theme('image', $path, $cc, $title, $attribs, FALSE);
}

Which lines can I replace with what to be able to get the following output:
from {flag} {country name}

appreciate your help!

mrfelton’s picture

Firstle, do not edit any of the files that came with the module! You should override the theme function in your theme. Open up your themes template.php file and copy and paste the above code. Rename the function from 'theme_ip2cc_flag' to 'name-of-your-theme_ip2cc_flag' (where name-of-your-theme is the name of your theme!). Refresh your theme registry (visiting the /admin/build/theme page should be enough to do that). Now, you can edit your new function as you need to and it will override the one from the module. For more infor about overriding theme functions, see http://drupal.org/node/55126

To get thhe output you desire, this should do it:

function theme_ip2cc_flag($country, $addr = '') {
  if (!user_access('view country')) {
    return "";
  }
  if (isset($country)) {
    $cc = strtolower($country->country_code);
    if (empty($cc)) {
      $cc = "unknown";
    }
    $title = "$cc $country->country_name";
    if (user_access('view IP address')) {
      $title .= " $country->ip";
    }
    if (user_access('view network name') && $country->net_name != 'Unknown') {
      $title .= " $country->net_name";
    }
  }
  else {
    $cc = 'unknown';
    $title = (empty($addr))?'unknown':$addr;
  }
  $path = drupal_get_path('module', 'ip2cc_node') ."/flags/$cc.png";
  $attribs = array('width' => 14, height => 14);
  $icon = theme('image', $path, $cc, $title, $attribs, FALSE);
  return 'from '. $icon .' '. $cc->country_name;
}

Note: All I did here was alter the last couple of lines.

vako’s picture

Wow! unbelievable! I learned so many things from your last post, it's amazing!!
THANK YOU THANK YOU THANK YOU!! for the speed and accuracy of your help.

I suppose all functions (from other modules) can be used the same way, so we don't have to edit the actual modules themselves.
That's another great feature of working with Drupal. I am learning as I go and love it so far. Can't do without the help of people like you though.

Thanks again.

mrfelton’s picture

Status: Fixed » Closed (fixed)

Glad you got there in the end :)

vako’s picture

I come again with two issues and seek your help please.

A) the Canadian and US flags are not showing, the $cc code is 'unknown'
B) am unable to extract the country name, only the flag.

would appreciate your input on this please. thanks!

mrfelton’s picture

There have been other reports of US IP addresses not working anymore. Please see #579632: Database not currently supporting IP ranges it once supported

vako’s picture

thanks mrfelton, can you please check the helpful code you had sent above as how to also display the country name? Currently it only displays from {flag}, would like to have from{flag}{country name}, if possible.
on the last line of your code $cc->country_name; is always blank.

thanks.

mrfelton’s picture

Have you got the countries_api module installed? This module makes extra information, including the country name available.

vako’s picture

I solved it by changing the line to: return 'from '.$icon.' '. "$country->country_name";

thanks again.

mrfelton’s picture

Ah yes, good catch.