3

When I set contextual filters in a view for Content: my field: country and Content: my field: administrative area (state/province) and select "Display a summary" for "When the filter value is not available", it returns a list of country/administrative area codes (e.g. "JP"), not the names of the country/administrative area (e.g. "Japan").

How can I show the names of the countries/administrative areas instead of the codes?

3 Answers 3

3
+50

Okay, well the major issue with this particular field is that id doesn't have all the properties you need to go through the standard routes to display the term instead of the id.

So, without a major rewrite of the module I would do this if you really need this feature.

Copy the views-view-summary-unformatted.tpl.php to your admin theme directory and edit it to include a large switch statement with all of the country codes you are interested in, probably copied in from the module itself. Here is a working example of one such modification for your example of Japan. Obviously this is not ideal, but without altering the field information of the module there is no good way to link the Key used here with the value. Sorry there does not seem to be a better way.

    <?php
    /**
     * @file views-view-summary-unformatted.tpl.php
     */
    ?>
    <?php foreach ($rows as $id => $row): ?>
      <?php print (!empty($options['inline']) ? '<span' : '<div') . ' class="views-summary views-summary-unformatted">'; ?>
        <?php if (!empty($row->separator)) { print $row->separator; } ?>
        <a href="<?php print $row->url; ?>"<?php print !empty($row_classes[$id]) ? ' class="' . $row_classes[$id] . '"' : ''; ?>>
        <?php 
         include_once DRUPAL_ROOT . '/includes/locale.inc';
         $countries = country_get_list();
         if($countries[$row->link]) {
            print $countries[$row->link];
         }
         else {
          print $row->link;
         } 
        ?></a>
        <?php if (!empty($options['count'])): ?>
          (<?php print $row->count; ?>)
        <?php endif; ?>
      <?php print !empty($options['inline']) ? '</span>' : '</div>'; ?>
    <?php endforeach; ?>
7
  • Adding the country information as a field within the view works fine. However, when the address field is used as a contextual filter (not a field) and set to "Display a summary", the abbreviation of the country is displayed. The "Display a summary" allows the list of countries to be shown if no country has already been selected. You can comment once you have more reputation on the site. Commented Jun 15, 2012 at 6:20
  • Ahh, I can reproduce your situation now. I will look into the solution. Commented Jun 15, 2012 at 6:59
  • I have updated and tested the code above, (based on Feature's comments), which seems to work great. I haven't extensively tested it for all different countries but you could write a unit test for it to ensure that it works. Commented Jun 15, 2012 at 17:42
  • I am using this for the default theme of my site, not my admin theme. The file in question is views-view-summary.tpl.php. When I added your code to that file, the country name now appears instead of the abbreviation, which is great. Unfortunately, when you click the country name, it takes you to the list of administrative areas (two contextual filters: countries and administrative area). However, these administrative areas are converted from their abbreviations into the country abbreviations. Also, I get an error (Undefined index: <No value>) on the line if($countries[$row->link]) { Commented Jun 16, 2012 at 2:12
  • 1
    It will always include and check the country list for any view summary on the site — try using template suggestions to narrow this template usage. Or even better, go with a template preprocess (see my answer below). Commented Jun 17, 2012 at 6:55
4

Here is an alternative to @Kenny and @Bearden solution, using template preprocess instead of overriding the template. Not only it makes for a more readable solution, you can reuse it for other views. It should also work for list summaries, just remove _unformatted from the function name.

/**
 * Implements hook_preprocess_views_view_summary_unformatted().
 */
function MYTHEME_preprocess_views_view_summary_unformatted($variables) {
  if ($variables['view']->name == MYVIEW) {
    // Necessary for country_get_list().
    include_once DRUPAL_ROOT . '/includes/locale.inc';
    $countries = country_get_list();

    // Looping through the row keys for performance reasons.
    foreach (array_keys($variables['rows']) as $rowid) {
      $countryid = $variables['rows'][$rowid]->link;
      if (!empty($countries[$countryid])) {
        $variables['rows'][$rowid]->link = $countries[$countryid];
      }
    }
  }
}
1
  • 1
    Note that I intentionally avoid including the file on every summary template by checking for the view name first. Commented Jun 17, 2012 at 6:47
2

You can use the locale.module function country_get_list() which will give you an array of country codes and their full names. You should be able to simply use this array to find your code (as the key) and get the resulting country name.

As an example you should be able to do something like this to find the readable name of the US (untested):

$countries = country_get_list()
$us_code = $countries['US'];
return $us_code;

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.