diff --git a/modules/nodereference/nodereference.module b/modules/nodereference/nodereference.module index 5f49e79..976a9d9 100644 --- a/modules/nodereference/nodereference.module +++ b/modules/nodereference/nodereference.module @@ -124,6 +124,19 @@ function nodereference_field_settings($op, $field) { '#required' => FALSE, '#description' => t('Provide a comma separated list of arguments to pass to the view.'), ); + // Add token list for view argument in advance setting. + if (module_exists('token')) { + $form['advanced']['token_help'] = array( + '#title' => t('View argument token replacement patterns'), + '#type' => 'fieldset', + '#collapsible' => TRUE, + '#collapsed' => TRUE, + '#description' => t('Prefer raw-text replacements for text to avoid problems with HTML entities!'), + ); + $form['advanced']['token_help']['help'] = array( + '#value' => theme('token_help', 'node'), + ); + } } else { $form['advanced']['no_view_help'] = array( @@ -221,7 +234,7 @@ function nodereference_field($op, &$node, $field, &$items, $teaser, $page) { } // Prevent performance hog if there are no ids to check. if ($ids) { - $refs = _nodereference_potential_references($field, '', NULL, $ids); + $refs = _nodereference_potential_references($field, '', NULL, $ids, NULL, $node->nid); foreach ($items as $delta => $item) { if (is_array($item)) { $error_element = isset($item['_error_element']) ? $item['_error_element'] : ''; @@ -648,17 +661,22 @@ function nodereference_buttons_process($element, $edit, $form_state, $form) { * */ function nodereference_autocomplete_process($element, $edit, $form_state, $form) { - // The nodereference autocomplete widget doesn't need to create its own // element, it can wrap around the text_textfield element and add an autocomplete // path and some extra processing to it. // Add a validation step where the value can be unwrapped. $field_key = $element['#columns'][0]; + // Find the node ID in the node object on the current form + $nid = isset($form['#node']->nid) && intval($form['#node']->nid) ? $form['#node']->nid : 0; + $element[$field_key] = array( '#type' => 'text_textfield', '#default_value' => isset($element['#value']) ? $element['#value'] : '', - '#autocomplete_path' => 'nodereference/autocomplete/'. $element['#field_name'], + // Include the current node ID from the form in the autocomplete path + // and a custom attribute + '#autocomplete_path' => 'nodereference/autocomplete/' . $element['#field_name'] . '/' . $nid, + '#nodereference_source_nid' => $nid, // The following values were set by the content module and need // to be passed down to the nested element. '#title' => $element['#title'], @@ -738,7 +756,7 @@ function nodereference_autocomplete_validate($element, &$form_state) { } else { // No explicit nid. - $reference = _nodereference_potential_references($field, $value, 'equals', NULL, 1); + $reference = _nodereference_potential_references($field, $value, 'equals', NULL, 1, $element['#nodereference_source_nid']); if (empty($reference)) { form_error($element[$field_key], t('%name: found no valid post with that title.', array('%name' => t($field['widget']['label'])))); } @@ -789,6 +807,9 @@ function nodereference_allowed_values($field) { * ignored). * @param $limit * If non-zero, limit the size of the result set. + * @param $source_nid + * Optional node id of node containing the node reference field. Passed to + * _nodereference_potential_references_views for advanced token functionality. * * @return * An array of valid nodes in the form: @@ -800,7 +821,7 @@ function nodereference_allowed_values($field) { * ... * ) */ -function _nodereference_potential_references($field, $string = '', $match = 'contains', $ids = array(), $limit = NULL) { +function _nodereference_potential_references($field, $string = '', $match = 'contains', $ids = array(), $limit = NULL, $source_nid = NULL) { static $results = array(); // Create unique id for static cache. @@ -808,7 +829,7 @@ function _nodereference_potential_references($field, $string = '', $match = 'con if (!isset($results[$cid])) { $references = FALSE; if (module_exists('views') && !empty($field['advanced_view']) && $field['advanced_view'] != '--') { - $references = _nodereference_potential_references_views($field, $string, $match, $ids, $limit); + $references = _nodereference_potential_references_views($field, $string, $match, $ids, $limit, $source_nid); } // If the view doesn't exist, we got FALSE, and fallback to the regular 'standard mode'. @@ -827,7 +848,7 @@ function _nodereference_potential_references($field, $string = '', $match = 'con * Helper function for _nodereference_potential_references(): * case of Views-defined referenceable nodes. */ -function _nodereference_potential_references_views($field, $string = '', $match = 'contains', $ids = array(), $limit = NULL) { +function _nodereference_potential_references_views($field, $string = '', $match = 'contains', $ids = array(), $limit = NULL, $source_nid = NULL) { $view_name = $field['advanced_view']; if ($view = views_get_view($view_name)) { @@ -865,11 +886,16 @@ function _nodereference_potential_references_views($field, $string = '', $match // Get arguments for the view. if (!empty($field['advanced_view_args'])) { - // TODO: Support Tokens using token.module ? $view_args = array_map('trim', explode(',', $field['advanced_view_args'])); - } - else { - $view_args = array(); + // Support for Tokens using token.module. + if (module_exists('token')) { + // Load the node from a stored form value or the current menu item + $node = !empty($source_nid) ? node_load($source_nid) : menu_get_object('node', 1); + if (!empty($node->nid)) { + // Replace tokens using node information. + $view_args = token_replace($view_args, 'node', $node); + } + } } // We do need title field, so add it if not present (unlikely, but...) @@ -965,13 +991,13 @@ function nodereference_autocomplete_access($field_name) { /** * Menu callback; Retrieve a pipe delimited string of autocomplete suggestions for existing users */ -function nodereference_autocomplete($field_name, $string = '') { +function nodereference_autocomplete($field_name, $nid, $string = '') { $fields = content_fields(); $field = $fields[$field_name]; $match = isset($field['widget']['autocomplete_match']) ? $field['widget']['autocomplete_match'] : 'contains'; $matches = array(); - $references = _nodereference_potential_references($field, $string, $match, array(), 10); + $references = _nodereference_potential_references($field, $string, $match, array(), 10, $nid); foreach ($references as $id => $row) { // Add a class wrapper for a few required CSS overrides. $matches[$row['title'] ." [nid:$id]"] = '
'. $row['rendered'] . '
';