In the Dec 10 dev the option to use a view to select a node reference does not work:

1. Setting the widget to Radio/Checkboxes; Autocomplete; or Select List has no effect. The node add/edit form always displays a select list

2. Specifying a view does work, but only the top 10 items are pulled in, regardless of the setting in the view itself.

I've created a new view espeically for noderef selection to test the problem. Attached is the view.

Are there other troubleshooting assets I can provide?

Thanks in advance for any insight and guidance...

Comments

dawehner’s picture

Project: Views (for Drupal 7) » Content Construction Kit (CCK)
Version: 6.x-3.x-dev » 6.x-2.x-dev
Component: Views Data » Views Integration

Move to noderef, there is no reason in views that it doesn't work, from my perspective. CCK might do something wired here.

nicksanta’s picture

Title: CCK Noderef: View Used to Select the Nodes: fails to use selected view and form type » Node Reference field always displaying 10 results when using the Advanced View option. [views-6.x-3.x]

I've been trying to debug this issue for @boabjohn. It took a while, but I think I've shed a little light on the issue (although haven't found the root cause).

Currently using the following versions of modules:
Views: 6.x-3.x-dev [11 Dec 2010]
Node Reference: 6.x-3.x-dev [6 Dev 2010]

OK, so the problem was that no matter how many items the view was configured to display, it always showed 10 in the nodereference widget. No matter what widget was selected, how many values the field was configured to save etc..

I traced the issue down to the views_plugin_query_default::execute() function. Here are the bits that I was interested in.

<?php
class views_plugin_query_default extends views_plugin_query{
...
  $replacements = module_invoke_all('views_query_substitutions', $view);
...
  if ($this->pager->use_count_query() || !empty($view->get_total_rows)) {
      // Let the pager modify the query to add limits.
      $this->pager->pre_execute($query, $args);

      if (!empty($this->limit) || !empty($this->offset)) {
        // We can't have an offset without a limit, so provide a very large limit instead.
        $limit  = intval(!empty($this->limit) ? $this->limit : 999999);
        $offset = intval(!empty($this->offset) ? $this->offset : 0);
        $result = $this->db_query_range($query, $args, $offset, $limit);
      }
      else {
        $result = $this->db_query($query, $args);
      }

      $view->result = array();
      while ($item = db_fetch_object($result)) {
        $view->result[] = $item;
      }
...
}
?>

So, for whatever reason, the $this->limit was being assigned the value of 10 at some point after: $replacements = module_invoke_all('views_query_substitutions', $view); But before this: if (!empty($this->limit) || !empty($this->offset)) {

Anywho, I added a hook in the site's custom module to get around the issue temporarily. See below if anyone else is stuck:

<?php
/**
 * Implement hook_views_query_substitutions
 *
 * Making sure our nodereference views display the number of results we want.
 * We're using this hook because it's the latest we can change the value before
 * the query is executed.
 *
 * See drupal.org/node/998494#comment-3853320 for more info
 */
function hook_views_query_substitutions(&$view) {
  if ($view->name == 'view_name') {
    $view->query->limit = $view->display[$view->current_display]->display_options['items_per_page'];
  }
}
?>
dbkern’s picture

I have the exact same problem. I've tried the code from #2 in a custom module but I can't get it to work. Don't know what I am doing wrong. Changed view_name to the name of my view. Anything else I should be doing?

dbkern’s picture

Well, I'm an idiot--or sleep-deprived. I had a typo in my function name.

Nicksanta, thanks so much for the code--it works splendidly and puts to rest a couple of days of agony trying to pinpoint this problem. My problem was actually with a Webform and the Webform View Reference Component which is based on CCK Nodereference. I couldn't control the number of nodes displayed in my select list--always ten. Traced it back to CCK but don't know php well enough and couldn't solve it.

Again, big thanks!

nbecher’s picture

what was the typo you had? I am trying to get this to work as well and changed the view name. It's still not working. did you change anything else?

thanks so much.

monotaga’s picture

Has anyone figured out anymore about this issue? Any better workarounds beside adding code to a custom module?

monotaga’s picture

This does seem to have something to do with the version of Views in use since downgrading to Views 6.x-3.0-alpha3 doesn't cause this problem.

rp7’s picture

#2 wasn't working for me either. Following code however, does work on my installation. You might want to give it a try:

/*
 * Implementation of hook_views_query_alter().
 */
function mymodule_views_query_alter(&$view, &$query) {
	if($view->name == 'name_of_view') {
		$view->set_items_per_page(0);
	}
}
raveman’s picture

hook_views_query_alter helped me with the same issue

btown’s picture

The solution in #2 worked for me.

916Designs’s picture

#8 Worked for me.Thank you both.

Renee S’s picture

Confirm #8 worked. (#2 did not.)

Thanks all :)

VTM’s picture

can anyone help me with more clear instructions?
Where do I add the above code (#2, #8) ?
thanks.

rp7’s picture

You should put it in a custom module. (http://drupal.org/developing/modules)
Change 'mymodule' in function mymodule_views_query_alter with the name of your module.
Change 'name_of_view' in if($view->name == 'name_of_view') with the name of your view.

Hope that helps?

hixster’s picture

Is there a more permanent solution to this on the horizon ? Grateful for the solution, but having to maintain a separate module to fix this issue alongside all the other code on a site seems like a pain.

nicksanta’s picture

@hixster: Comes with the territory of using alpha / dev versions of code on your site.

cossme’s picture

StatusFileSize
new882 bytes

i put together a small module using #2 which does the trick for me. I attached it so it might be of any help for somebody. after installation just go to admin/settings/cossme_fix_noderef and enter the ids of views to be "fixed".

mirabuck’s picture

It looks like cossme and I were working on this in parallel. I've put together a module to address this as well. Took a different route though, creating a CCK widget that allows the view limit and arguments to be overridden when adding new nodes. It can be found at here: http://drupal.org/project/limited_nodereference

bkno’s picture

@cossme

Just trying the module in #17. I've added two views, one on each line, but only the last one seems to get fixed. What I've put in the box:

My_View_Name_One
My_View_Name_Two

Assuming by ID it's the 'machine' name. I also tried the numerical ID but this doesn't seem to do anything.

UPDATE:

I've amended the cossme's module line 25 to:
$select_views = split(",", variable_get('cossme_fix_noderef/views', ''));

Splitting on comma instead of new line now makes the views show more than 10 items in the select list. I'm now entering like:

My_View_Name_One,My_View_Name_Two

Hope CCK module can resolve the underlying issue though. I'm using latest CCK stable with Views dev.

fourmi4x’s picture

#8 worked for me, thanks!

However, I wanted to mention that I went on this thread because of another "problem" : I didn't know that one had to put a "nid" field in the target view in order for the "Advanced - Nodes that can be referenced (View)" option to work (Without the nid, I always got only 1 node!).

I stuck on this for about 2h...couldn't find the info anywhere! I think it would be a good idea to add this info ("nid required") in the help text of the "Advanced - Nodes that can be referenced (View)" section, like:

Choose the "Views module" view that selects the nodes that can be referenced.
Note:
* Only views that have fields will work for this purpose.
* This will discard the "Content types" settings above. Use the view's "filters" section instead.
* Use the view's "fields" section to display additional informations about candidate nodes on node creation/edition form.
* Use the view's "sort criteria" section to determine the order in which candidate nodes will be displayed.

* The target view must have a "Node: Nid" field.

for instance... Or at least put this in the advanced help...

zhangtaihao’s picture

StatusFileSize
new1.47 KB

This issue applies to both Node Reference and User Reference. I understand my post is probably not going anywhere, but here's the idea anyway. Hopefully, this gets around the "potential instability" of Views 6.x-3.x-dev transparently.

VTM’s picture

@zhangtaihao - thanks.
It did help.

travist’s picture

Status: Active » Reviewed & tested by the community

I just ran into the same issue... debugged it on my own, and practically came up with the same solution here #1015810: nodereference select shows only the first 10 rows from view .

So, this patch is reviewed and tested by the community. Good fix.

travist’s picture

Also created a patch for Views that should fix this particular case, but the patch above is still needed. #1243436: Advanced View with "none" pager limits view results

monotaga’s picture

The patch in #21 is also working for me. Can we get it rolled in?

travist, I'm not completely understanding the purpose of your patch at #1243436: Advanced View with "none" pager limits view results; that's an independent issue, right? Ie. I don't necessarily need to use your patch there if the patch here in #21 fixes my issue, right?

travist’s picture

monotaga,

The patch I did for views is not necessary to fix this particular case since the node reference module explicitly sets the number of items to 0 (infinite). So, all you really need is #21 and it will fix this issue. I included the work I did on #24 because it was related and was the cause of the original issue.

liquidcms’s picture

Priority: Normal » Major

#21 worked for me (once i got rid of the crud that git adds to patch file)

not sure why this hasn't been committed yet.

christianchristensen’s picture

StatusFileSize
new1.45 KB

Second what @liquidcms said, the patch does not apply cleanly, a quick removal of the a/ and b/ directories seems to help.

xpersonas’s picture

After uploading to Views 6.x-3.x-dev (2011-Nov-10) this patch is no longer working for me. Anyone else have that issue with this version of Views? I know it was this version because it worked fine until upgrading just this module.

hillaryneaf’s picture

@phaedo5 Try upgrading to Views 6.x-3.0-rc3 (2011-Nov-14). The patch worked for me with the release candidate of Views.

Is there a reason the patch hasn't been committed?

iamsixstringz’s picture

THANK YOU for this comment, it just saved my day!!

I accomplished this by adding a node id by creating a page display for the view, and defining it's path. Didn't see a variable named NID as you mentioned above.

jparets’s picture

I have the same problem when upgrading from Views 6.x-2.16 to views 6.x-3.0
I experienced the same problem in the past and it was solved setting appropiate validators for arguments in Views. But this solution do not work now.
The views works fine when are displayed, but the number of items is restricted to 10 in editing forms. The main drawback is that nodes can not be edited or created when they contain nodereference fields.
Thanks!

slybud’s picture

+1 for patch in #28, it solved my issue with 3.0-rc3 version of views

Thanks everyone

monotaga’s picture

This patch has been RTBC for a while now. Could we please have this applied?

Azol’s picture

Version: 6.x-2.x-dev » 6.x-3.x-dev

Patch in #28 applies to 6.x-3.x-dev cleanly.
+1 RTBC

terbs’s picture

Patch has also worked for me..

danny englander’s picture

Just getting back to a site that needed some attention and this fix in particular. The patch in #28 worked thankfully but as others have reported there were issues using git apply. I had to remove the git stuff and use the old patch -p0 < method, Thanks to all who worked on this and again not sure why this has not been applied to the dev yet. At any rate I used the latest dev 3.x from 2012-Jan-12 and applied cleanly.

deggertsen’s picture

Thanks for the patch. Fixed the problem for me as well. I vote that this be committed.

Azol’s picture

Maybe someone would reach KarenS via e-mail or IRC, I tried to PM her a while ago to no avail.

jeebsuk’s picture

We are also suffering with this problem - any chance of getting this patch committed?

yched’s picture

Status: Reviewed & tested by the community » Fixed

Added a comment, and committed to both 2.x and 3.x branches.
Thks folks !

jeebsuk’s picture

@yched: Have already expressed my gratitude on this via Twitter, but many thanks for your help on this after I got in touch, very grateful indeed.

Azol’s picture

Many thanks!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

dpatte’s picture

This issue seems very closely related to #1166234: Field values aren't displayed when using Views to filter node reference select list (/w Fix) but the latest dev of cck 6.2 don't resolve this issue for me.

pvasener’s picture

Status: Closed (fixed) » Reviewed & tested by the community

I'm using patch #28 successfully for a few months now, as a few of us, but it doesn't seem to be into views-6.x-3.x yet.

damienmckenna’s picture

Issue summary: View changes
Status: Reviewed & tested by the community » Closed (fixed)