This is a more advance feature request that should probably come after #945004: [release blocker] Missing views-mode for picking userreference and nodereference.

I have a need to set dynamic views arguments based on the current user or state of the node. The interface as it currently stands allows me to set up a views argument in the field settings, but I'm having a hard time finding a way to update it based on other conditions. Specifically, I have a node with a dropdown of locations (actually a term_reference field) and I want a node_reference field (multiple entry and autocomplete) to present a list of content based on that location. As a more general issue summary: I'm trying to think through the least invasive place to update the field settings on the node_reference auto complete when the node is being edited.

Has anyone given any thought and/or can point me in the right direction to how something like this might be accomplished? Thanks in advance.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

valderama’s picture

I would need exactly the same feature. Is there a way to accomplish that at the moment?

something with hook_form_alter maybe?? hmm

alaa’s picture

or maybe render the exposed filters in the view as part of the widget?

geerlingguy’s picture

See: #1299210: File upload AJAX error "An illegal choice has been detected" - I have my view set to grab the node nid from the URL and set options based on that, which works (you could wrangle a view to work for this) as long as you're not doing anything with AJAX on your form (file uploads, etc.).

ccshannon’s picture

I cannot get it to work. Here are the steps:

I have a Taxonomy Term. I added a Node Reference field to the Term form.

I create a 'References' display in the Taxonomy Term View, using the 'Contextual Filter' "Content: Has taxonomy term ID (with depth) " which is set to pull a TermID (tid) from the URL.

I save the View.

I go to the Node Reference field and set the View Argument for the field to use the References display type I just created in Views. Save.

I go to the edit form for my Term, and type in text for a Node that I know is marked with that Term. No results in the Node Reference field.

I go back to the View and disable the Contextual Filter entirely, basically allowing all nodes to pass through to the Node Reference field. Save the View.

Go back to the Term edit form. Reload. Try typing in any node and get a Javascript alert: "An HTTP error 500 occurred".

So, has anyone gotten this to work, and, if so, could you please print the detailed steps here? Thanks.

acbramley’s picture

I'm trying to get this kind of functionality too, but you can't use url arguments to pass in things like tid or nid because the url for the autocomplete request is something like:

"/node_reference/autocomplete/node/content_type/field_name/autocompletesting"

petu’s picture

I solved this issue for my case. I use views_php module and get all the necessary data from views' arguments.

Example for selecting different translations for node reference field:
I added translation argument to views and PHP code as a default value:

$nid = arg(1);
if($_GET['language']){
  return $_GET['language'];
}elseif(is_numeric($nid)){
  $node = node_load($nid);
  return $node->language;
}else{
  return 'en';
}
happysnowmantech’s picture

Attached is a patch for passing dynamic views arguments to the node reference autocomplete widget by adding token support to the "Views arguments" field. Here is an example of how to do it:

1. Apply the patch.

2. Use hook_form_alter() on the node edit form to append your Views arguments to the #autocomplete_path of the node reference field.

function mymodule_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'my_content_type_node_form') {

    // Set the views arguments
    // These could come from the form values, the node, the current URL, etc.
    $views_arg_1 = ...;
    $views_arg_2 = ...;

    // Add the views arguments to the end of the #autocomplete_path
    $form['field_my_node_ref']['und'][0]['nid']['#autocomplete_path'] = "node_reference/autocomplete/node/my_content_type/field_my_node_ref/$views_arg_1/$views_arg_2";
  }
}

3. In the "Views arguments" section of your node reference field settings, enter tokens to use as your Views arguments. For example:

[current-page:url:args:value:5], [current-page:url:args:value:6]

This would extract whatever values you put as $views_arg_1 and $views_arg_2 in the #autocomplete_path from step #2 above.

IMPORTANT: The [current-page:url] token substitution values will be based on the #autocomplete_path URL, NOT the node edit form URL (node/123/edit).