From 65083af4551dc83183b4e3d549adc5ba546aeeb5 Mon Sep 17 00:00:00 2001
From: Robert Allerstorfer <roball@405360.no-reply.drupal.org>
Date: Thu, 14 Mar 2013 20:13:54 +0100
Subject: [PATCH] #375307: Allow key|value pairs in selection fields of
 profile.module

---
 modules/profile/profile.admin.inc |  2 +-
 modules/profile/profile.module    | 19 ++++++++++++++++++-
 2 files changed, 19 insertions(+), 2 deletions(-)

diff --git a/modules/profile/profile.admin.inc b/modules/profile/profile.admin.inc
index 089b5ea..236bf5c 100644
--- a/modules/profile/profile.admin.inc
+++ b/modules/profile/profile.admin.inc
@@ -238,7 +238,7 @@ Unless you know what you are doing, it is highly recommended that you prefix the
     $form['fields']['options'] = array('#type' => 'textarea',
       '#title' => t('Selection options'),
       '#default_value' => isset($edit['options']) ? $edit['options'] : '',
-      '#description' => t('A list of all options. Put each option on a separate line. Example options are "red", "blue", "green", etc.'),
+      '#description' => t('A list of all selectable options. One option per line. Key-value pairs may be entered separated by pipes, such as "safe_key|Some readable option". Example: "AT|Austria", "BE|Belgium", etc.'),
     );
   }
   $form['fields']['visibility'] = array('#type' => 'radios',
diff --git a/modules/profile/profile.module b/modules/profile/profile.module
index 141c094..f57f3e7 100644
--- a/modules/profile/profile.module
+++ b/modules/profile/profile.module
@@ -260,6 +260,16 @@ function profile_view_field($user, $field) {
         return check_markup($value);
       case 'textfield':
       case 'selection':
+        $lines = split("[\n\r]", $field->options);
+        foreach ($lines as $line) {
+          if ($line = trim($line)) {
+            $parts = explode('|', $line, 2);
+            if (isset($parts[1]) && $parts[0] == $value) {
+              $value = trim($parts[1]);
+              break;
+            }
+          }
+        }
         return $browse ? l($value, 'profile/'. $field->name .'/'. $value) : check_plain($value);
       case 'checkbox':
         return $browse ? l($field->title, 'profile/'. $field->name) : check_plain($field->title);
@@ -395,7 +405,14 @@ function profile_form_profile($edit, $user, $category, $register = FALSE) {
         $lines = split("[\n\r]", $field->options);
         foreach ($lines as $line) {
           if ($line = trim($line)) {
-            $options[$line] = $line;
+            $parts = explode('|', $line, 2);
+            if (isset($parts[1])) {
+              // "safe_key|value" option
+              $options[trim($parts[0])] = trim($parts[1]);
+            } else {
+              // option = value
+              $options[$line] = $line;
+            }
           }
         }
         $fields[$category][$field->name] = array('#type' => 'select',
-- 

