I have two content types: students and chemistry labs. Students content type has a cck field that lists student type (field_student_type) with Chemistry.Biology, Math, and etc. And Chemistry labs content type has a cck field (field_chemsitry_students) that node references to "students" content type as a list widget. Since it's a chemistry content type, the list needs to pull only chemistry students. With the current setup, it's pulling every student. I can't set the filter in views as it's a drop down list (widget). Views won't let me put operators to be ield_student_type = "Chemistry Student". How would I filter this out? Any suggestions? php filter? The question is more about field to field reference using node reference.
2 Answers
Views can most certainly filter on CCK field values. In the view filters, select the content group, and then select whatever field you are wanting and then for the operator select "is equal to" and put in whatever you want.
So, in this specific case, you could create view as follows:
filter on field_student_type equal to "Chemistry" filter on node type "Student"
sort by node title (so the students are alphabetical perhaps?)
return node title (without a label so the dropdown doesn't say Title: FOO) as the field
Then, in your content type of Chemistry Labs, you would select this view in the "Advanced - Nodes that can be referenced (View)" section and it should return only node references to students with student_type equal to chemistry.
(Now, if you find it returns only 10 students no matter what you are doing, don't pull your hair out, that's an issue between versions of cck and views, and a solution for another day :)
ADDITION:
Since you seem to be hitting an issue between Views and CCK here as well with respect to how limits are set, I resolved this issue locally by replacing a line of code in the nodereference.module with these lines of code :
if (method_exists($view, 'set_items_per_page')) { // views is defaulting to 10 now
$view->set_items_per_page($limit); // and has changed how to set it
} else { // so this does it the new way
$view->display_handler->set_option('items_per_page', $limit); // if new views and the old way
} // if old views....
after
// TODO : for consistency, a fair amount of what's below
// should be moved to content_plugin_display_references
// Limit result set size.
$limit = isset($limit) ? $limit : 0;
$view->display_handler->set_option('items_per_page', $limit); // REPLACE THIS LINE WITH THE ABOVE
in the _nodereference_potential_references_views()
function.
Whether or not you want to hack up a module or wait for an official fix is up to you.
-
This was my initial step. However, since field_chemistry_students is of list widget type, views does not let me put the operator value. It wants me to choose the value from the list next to it, that is the field_chemistry_students which is a list of every Student. Does it make sense?– d6dJan 30, 2012 at 17:03
-
Correct IF you are only using cck's "Content types that can be referenced:" options, then yes, by selecting "student" there, it will list all students. But, if you are using the "Advanced - Nodes that can be referenced (View)" options, and selecting a View you've created, then, sorry, no, you've lost me. Jan 30, 2012 at 17:15
-
It makes sense! Apologies for not reading it thoroughly.:) Let me try this. Hopefully it will work– d6dJan 30, 2012 at 17:17
-
Ok, managed it to work slightly. But somehow it's displaying only newly created chemistry "Students" going forward. The previous ones are not getting displayed. Cleared the cach, ran the cron... Any thoughts?– d6dJan 30, 2012 at 23:40
-
I have no idea why the maintainers haven't fixed this glaring oversight, but after hours searching and testing I figured his out and want to share.
Total results for views with multiple displays and pager
Inline with the view, drop this in view header:
<?php
print $view->query->pager->total_items;
?>
Outside the view:
<?php
$view = views_get_view('MY_VIEW_NAME');
$view->set_display('MY_DISPLAY'); // like 'block_1'
$view->execute();
print $view->query->pager->total_items;
?>
That freakin' easy...