--- nat.module	2011-02-02 21:11:42.000000000 +0000
+++ nat.new.module	2011-03-07 17:48:30.000000000 +0000
@@ -200,17 +200,127 @@ function nat_form_alter(&$form, &$form_s
 }
 
 /**
+ * Implements hook_token_info()
+ */
+
+function nat_token_info() {
+  $type = array(
+    'name' => t('Nat field instances'), 
+    'description' => t('Tokens related to node auto term field instances.'), 
+    'needs-data' => 'nat-field-instance',
+  );
+
+  // Core tokens for nodes.
+  $instance['id'] = array(
+    'name' => t("NAT Field Instance ID"), 
+    'description' => t("The unique ID of the field instance which the NAT link is in."),
+  );
+  $instance['field-id'] = array(
+    'name' => t("NAT Field ID"),
+    'description' => t("The unique ID of the field which the NAT link is in."),
+  );
+  $instance['field-name'] = array(
+    'name' => t("NAT Field name"), 
+    'description' => t("The name of the field which the NAT link is in."),
+  );
+  $instance['label'] = array(
+    'name' => t("NAT Field label"),
+    'description' => t("The human readable label of the field instance which the NAT link is in."),  
+  );
+  return array(
+    'types' => array('nat-field-instance' => $type), 
+    'tokens' => array('nat-field-instance' => $instance),
+  );
+}
+
+/**
+ * Implements hook_tokens()
+ */
+function nat_tokens($type, $tokens, array $data = array(), array $options = array()) {
+  $sanitize = !empty($options['sanitize']);
+  $replacements = array();
+  if ($type == 'nat-field-instance' && !empty($data['nat-field-instance'])) {
+    $instance = $data['nat-field-instance'];
+    foreach ($tokens as $name => $original) {
+      switch ($name) {
+        // Simple key values on the node.
+        case 'id':
+          $replacements[$original] = $instance->id;
+          break;
+
+        case 'label':
+          $replacements[$original] = $sanitize ? check_plain($instance->label) : $instance->label;
+          break;
+        
+        case 'field-id':
+          $replacements[$original] = $instance->field_id;
+          break;
+        
+        case 'field-name':
+          $replacements[$original] = $instance->field_name;
+          break;
+      }
+    }
+
+  }
+
+  return $replacements;
+}
+
+
+
+/**
  * Implements hook_field_formatter_info().
  */
 function nat_field_formatter_info() {
   return array(
     'nat_link' => array(
       'label' => t('NAT link'),
-      'field types' => array('taxonomy_term_reference')
+      'field types' => array('taxonomy_term_reference'),
+      'settings' => array('missing_link_handler_path' => '')
     )
   );
 }
 
+function nat_field_formatter_settings_summary($field, $instance, $view_mode) {
+  $display = $instance['display'][$view_mode];
+  $settings = $display['settings'];
+    
+  $summary = '';
+  if ($display['type'] == 'nat_link') {
+    if ($settings['missing_link_handler_path'] == '') {
+      $summary = t('No missing link handler path set');
+    }
+    else {
+      $summary = t('Missing link handler path: @path ', array('@path' => check_plain($settings['missing_link_handler_path'])));
+    }
+  }
+  return $summary;
+}
+
+/**
+ * Implements hook_field_formatter_settings_form().
+ */
+function nat_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
+  $display = $instance['display'][$view_mode];
+  $settings = $display['settings'];
+    
+  $element = array();
+      
+  if ($display['type'] == 'nat_link') {
+    $element['missing_link_handler_path'] = array(
+      '#title' => t('Missing link handler path'), 
+      '#type' => 'textfield', 
+      '#size' => 20, 
+      '#default_value' => $settings['missing_link_handler_path'], 
+      // TODO - Need to add validation code.
+      //'#element_validate' => array('_element_validate_integer_positive')
+      //'#required' => TRUE,
+     );
+  }
+  return $element;
+}
+
 /**
  * Implements hook_field_formatter_prepare_view().
  *
@@ -227,7 +337,7 @@ function nat_field_formatter_prepare_vie
  */
 function nat_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
   $element = array();
-
+  $settings = $display['settings'];
   foreach ($items as $delta => $item) {
     if ($item['tid'] == 'autocreate') {
       $element[$delta] = array('#markup' => check_plain($item['name']));
@@ -235,17 +345,41 @@ function nat_field_formatter_view($entit
     else {
       $term = $item['taxonomy_term'];
       $nids = array_keys(nat_get_nids(array($term->tid), FALSE));
-      $element[$delta] = array(
-        '#type' => 'link',
-        '#title' => $term->name,
-        '#href' => "node/" . $nids[0]
-      );
+      if ($nids && $nids[0]) {
+        $element[$delta] = array(
+          '#type' => 'link',
+          '#title' => $term->name,
+          '#href' => "node/" . $nids[0]
+        );
+      }
+      else {
+        // it's a bad link, so mark it as such and redirect to a handling page.
+        if ($settings['missing_link_handler_path'] != '') {
+          // first substitute the arguments.
+          $tokens=array();
+          $tokens['term']=$term;
+          $tokens[$entity_type] = $entity; // should work for node and other cases.
+          $tokens['nat-field-instance'] = (object) $instance;
+          $href=token_replace($settings['missing_link_handler_path'],$tokens);
+          $element[$delta] = array(
+            '#type' => 'link',
+            '#title' => $term->name,
+            '#href' => check_plain($href), // TODO - check whether check_url is needed.
+            '#prefix' => '<span class="nat-missing-link">',
+            '#suffix' => '</span>'
+          );
+        }
+        else {
+          $element[$delta] = array('#markup' => check_plain($term->name));
+        }
+      }
     }
   }
 
   return $element;
 }
 
+
 /**
  * Implements hook_views_api().
  *
