? destination.patch Index: nodereference_url.install =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/nodereference_url/nodereference_url.install,v retrieving revision 1.1 diff -u -p -r1.1 nodereference_url.install --- nodereference_url.install 22 Jan 2009 02:25:29 -0000 1.1 +++ nodereference_url.install 1 Jun 2009 21:57:48 -0000 @@ -37,3 +37,38 @@ function nodereference_url_disable() { drupal_load('module', 'content'); content_notify('disable', 'nodereference_url'); } + +/** + * Update 'enabled' widget setting to handle teaser and full views separately. + */ +function nodereference_url_update_6001() { + module_load_install('content'); + module_load_include('inc', 'content', 'includes/content.admin'); + module_load_include('inc', 'content', 'includes/content.crud'); + + $ret = array(); + + content_clear_type_cache(TRUE); + + // Grab the list of fields to update. + foreach (content_types_install() as $type_name => $fields) { + foreach ($fields as $field) { + if ($field['type'] == 'nodereference' && $field['widget']['type'] == 'nodereference_url') { + // Widgets are each updated individually. + if (isset($field['widget']['node_link']['enabled'])) { + $field['widget']['node_link']['teaser'] = TRUE; + $field['widget']['node_link']['full'] = TRUE; + $field['widget']['node_link']['destination'] = 'default'; + + content_field_instance_update($field); + $ret[] = array('success' => TRUE, 'query' => 'Updated widget settings for ' . $field['field_name']); + } + } + } + } + + content_clear_type_cache(TRUE); + + return $ret; +} + 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 1 Jun 2009 21:57:48 -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; @@ -175,7 +244,8 @@ function nodereference_url_fallback_vali * Element validation function that makes title required when creating a link. */ function nodereference_url_node_link_validate($element, &$form_state) { - if ($form_state['values']['node_link']['enabled'] && empty($form_state['values']['node_link']['title'])) { + if (empty($form_state['values']['node_link']['teaser']) && empty($form_state['values']['node_link']['full']) + && empty($form_state['values']['node_link']['title'])) { form_error($element['title'], t('A link title must be specified if creating links on referenceable content.')); } }