? destination.patch
Index: nodereference_url.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/nodereference_url/nodereference_url.module,v
retrieving revision 1.9
diff -u -p -r1.9 nodereference_url.module
--- nodereference_url.module	17 May 2009 00:57:38 -0000	1.9
+++ nodereference_url.module	31 May 2009 07:22:39 -0000
@@ -48,31 +48,82 @@ function nodereference_url_widget_info()
  * Implementation of hook_link().
  */
 function nodereference_url_link($type, $object, $teaser = FALSE) {
-  $links = array();
-
   if ($type == 'node') {
-    $fields = content_fields(NULL, $object->type);
-    foreach ($fields as $field_name => $field) {
-      foreach(_nodereference_url_field_instances($field_name) as $target_type => $instance) {
-        if ($instance['widget']['type'] == 'nodereference_url' && !empty($instance['widget']['node_link']['enabled'])) {
-          if (!empty($field['referenceable_types'][$object->type]) && user_access('create ' . $target_type . ' content')) {
-            $links[$target_type .'_'. $field_name] = array(
-              'title' => $instance['widget']['node_link']['title'],
-              'href' => 'node/add/'. str_replace('_', '-', $instance['type_name']) .'/'. $object->nid,
-            );
-            if (!empty($instance['widget']['node_link']['hover_title'])) {
-              $links[$target_type .'_'. $field_name]['attributes']['title'] = $instance['widget']['node_link']['hover_title'];
-            }
+    return nodereference_url_build_all_links($object, $teaser);
+  }
+}
+
+/**
+ * Build an array of links for nodereference_url widgets that point to this node.
+ *
+ * @param $node
+ *   A fully loaded node object.
+ * @param $teaser
+ *   
+ * @return
+ *   An array of links for use with theme_links().
+ */
+function nodereference_url_build_all_links($node, $teaser) {
+  $links = array();
+  $fields = content_fields(NULL, $object->type);
+  foreach ($fields as $field_name => $field) {
+    foreach(_nodereference_url_field_instances($field_name) as $target_type => $instance) {
+      if ($instance['widget']['type'] == 'nodereference_url') {
+        $link_settings = $instance['widget']['node_link'];
+        if (($link_settings['teaser'] && $teaser == TRUE) || ($link_settings['full'] && $teaser == FALSE)) {
+          if ($link = nodereference_url_build_link($node, $instance)) {
+            $links[$target_type .'_'. $field_name] = $link;
           }
         }
       }
     }
   }
-
   return $links;
 }
 
 /**
+ * Build an individual link.
+ *
+ * Checks to ensure that the current node can be referenced by the field, ensures
+ * the current user has permission to create the field's node type, and builds
+ * the link based on the field's settings.
+ *
+ * @param $node
+ *   A fully loaded node object.
+ * @param $field
+ *   A CCK field instance.
+ *
+ * @return
+ *   An array containing properties to build a single link.
+ */
+function nodereference_url_build_link($node, $field) {
+  $link = array();
+
+  if (!empty($field['referenceable_types'][$node->type]) && user_access('create ' . $field['type_name'] . ' content')) {
+    if ((!empty($field['widget']['node_link']['teaser']) && $teaser == TRUE)
+        || (!empty($field['widget']['node_link']['full']) && $teaser == FALSE)) {
+      $link = array(
+        'title' => $field['widget']['node_link']['title'],
+        'href' => 'node/add/'. str_replace('_', '-', $field['type_name']) .'/'. $node->nid,
+      );
+      if (!empty($field['widget']['node_link']['hover_title'])) {
+        $link['attributes']['title'] = $field['widget']['node_link']['hover_title'];
+      }
+      if (!empty($field['widget']['node_link']['destination'])) {
+        if ($field['widget']['node_link']['destination'] == 'source') {
+          $link['query'] = drupal_get_destination();
+        }
+        elseif ($field['widget']['node_link']['destination'] == 'node') {
+          $link['query'] = 'destination='. urlencode(drupal_get_path_alias('node/'. $node->nid));
+        }
+      }
+    }
+  }
+
+  return $link;
+}
+
+/**
  * Helper function to retrieve all instances of a field.
  */
 function _nodereference_url_field_instances($field_name) {
@@ -132,15 +183,22 @@ function nodereference_url_widget_settin
           '#required' => TRUE,
           '#element_validate' => array('nodereference_url_fallback_validate'),
         );
+
         $form['node_link'] = array(
           '#tree' => TRUE,
-          '#weight' => 2,
+          '#type' => 'item',
+          '#title' => t('Create a link on referenceable content'),
           '#element_validate' => array('nodereference_url_node_link_validate'),
         );
-        $form['node_link']['enabled'] = array(
+        $form['node_link']['full'] = array(
           '#type' => 'checkbox',
-          '#title' => t('Create a link on referenceable content'),
-          '#default_value' => isset($widget['node_link']['enabled']) ? $widget['node_link']['enabled'] : '1',
+          '#title' => t('For full node view'),
+          '#default_value' => isset($widget['node_link']['full']) ? $widget['node_link']['full'] : TRUE,
+        );
+        $form['node_link']['teaser'] = array(
+          '#type' => 'checkbox',
+          '#title' => t('For teaser node view'),
+          '#default_value' => isset($widget['node_link']['teaser']) ? $widget['node_link']['teaser'] : FALSE,
         );
         $form['node_link']['title'] = array(
           '#type' => 'textfield',
@@ -154,6 +212,17 @@ function nodereference_url_widget_settin
           '#default_value' => isset($widget['node_link']['hover_title']) ? $widget['node_link']['hover_title'] : '',
           '#description' => t('Text shown while hovering over the link.'),
         );
+        $form['node_link']['destination'] = array(
+          '#type' => 'select',
+          '#title' => t('Return path'),
+          '#default_value' => isset($widget['node_link']['destination']) ? $widget['node_link']['destination'] : 'default',
+          '#options' => array(
+            'default' => t('The default location'),
+            'node' => t('The node being referenced'),
+            'source' => t('The page the link was clicked on'),
+          ),
+          '#description' => t('The path to return to after creating the node.'),
+        );
       }
       return $form;
 
