The node reference field is not sortable in Views. I think that content.module might need some tweaking to make that happen, because the basis for that source would be the referenced node's title, not nid.

Same for user reference.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

KarenS’s picture

This is still an issue and we really should get this working, so I'm bumping it.

appel’s picture

Agreed, this should be fixed imho. I'm having a hard time explaining to my boss why the table can't be sorted by the referenced 'company' field, while at the same time it can be sorted by less important fields.

KarenS’s picture

Title: reference fields - not sortable in Views » Make nodereference fields sortable in Views
Status: Active » Needs review
FileSize
13.32 KB

Here's my Christmas present to everyone -- I think I have this working. I've created a patch that joins in another copy of the node table for each nodereference field and gives you all the standard node fields for the referenced node -- title, creation date, type, body, whatever field you want to pick. If you want a sortable field, use the referenced node's title field instead of the standard nodereference field. You only need the title field to do sorting, but I thought it might be useful to add the other fields in.

I'm sure this needs debugging, but here it is (the patch is for 4.7, but should work the same in 5.x).

The downside is that it adds a lot of fields to the list. If you only have one or two nodereference fields it's not too bad. If you have a lot, it might be. At least all the fields for each nodereference sort together in a group. We could get rid of some of the referenced fields but I can think of lots of use cases where you might want the other fields, so for now at least I put them all in.

KarenS’s picture

Assigned: moshe weitzman » KarenS

I guess I'll assign myself :-)

moshe weitzman’s picture

this is working for me as designed! i hear you about field creep, but i think this is reasonable for now.

i couldn't get the node ref argument to work. you should pass a single nid? if so, the name of the node ref argument should end in 'nid' i think. the help text for this argument isn't so helpful. i don't think this argument is added by this patch though. i'm not sure where it is defined.

i have a related feature request, but i'll save that for another day and another issue. you nailed this one.

BTW, all i did to get this looking right on 5.0 is replace %name with @name.

moshe weitzman’s picture

This looks good for Sorts. but for Fields, I think it would be even better to implement "second" idea at http://drupal.org/node/91792. the fields implemention here is a subset of that, if I am grokking this properly.

KarenS’s picture

@moshe, The reason for including fields as well as sorts here is that you need the fields set up this way to do table sorts, the sort fields won't work for that. This is a really simple, lightweight solution that makes it possible to sort and filter on basic node info for the referenced nodes. The link you refer to and the solution you mention there is going to be much more complicated. It's theoretically doable, but there are a number of hurdles to getting it working. So I see this as a simple 1.1 method for getting this functionality working, and the ideas in http://drupal.org/node/91792 are probably not going to get solved until the next version (or at least the next release).

And this patch does nothing with arguments, so that is another issue :-)

I still need to try all these fields and filters out. I just brought in all the fields and filters that are already defined for nodes, but some of them may not work right or make sense for referenced nodes (like the node: distinct filter). So we could hack some of them out and reduce the size of the list.

maherg’s picture

The referenced node's link is pointing to the actual row node returned from the views query.

Is anyone experiencing this too ?

mcreature’s picture

Subscribing
+1 Sounds very cool

biohabit’s picture

Subscribing

appel’s picture

Subscribing.

biohabit’s picture

Hmm, the version 14 patch isn't working with CCK 1.5 and my stock versions of 1.5 don't appear to allow sorting on nodereference columns. Will future versions of this patch get incorporated into CCK?

joshk’s picture

+1 for this functionality; I'll see if I can roll a new patch for 5.x-1.5

dan3h’s picture

I have fixed a bug in this, and added a feature:

Bug fixed: Links went to wrong node. If you added a field of type "@name Referenced Node: Title", the clicking the link took you to the referring node, not the referenced node. That is fixed here, in nodereference.patch. (Note that this is a patch on top the the existing patch, provided by Karen, above in this thread.)

Feature added: Usernames are clickable. I basically cloned for users what Karen had done for nodes, but pared way back (ie. just added one field-- no sorts or filters.) This is done in userreference.patch.

I am using:
Drupal 5.1
CCK "5--1.3"
Views 5.x-1.6-beta5

BTW, this is my first-ever attempted patch-submission to any project. I hope it is in a format that is useful-- please let me know if not.

dan3h’s picture

Oops, filename in attachment on previous posting got mangled a bit. Here it is again.

dan3h’s picture

Oh, yikes-- it's even more mangled now. Please fix the filename to ".tar.gz" before opening.

discursives’s picture

+1

webchick’s picture

Thanks, dan3h for taking this up again. However...

a) Please don't upload .tar.gz/.zip/etc. files... the form shouldn't let you upload those 'by design'... cos people have no idea what they're getting until they've downloaded it.
b) Please either open a separate issue for making userreference fields sortable in Views, and put the userreference patch there, or else roll both changes into a single patch. I'm not sure if they belong together (since the changes are similar in each module) or if they belong as separate issues (because they're about two different modules), but keeping track of two patches in the same issue is a bit confusing. :)

Will try and test this later though; looks like a nice improvement!

wylbur’s picture

Is this still alive, it looks promising.

xxvelcrar’s picture

Version: 6.x-1.x-dev » 5.x-1.6-1
Component: content.module » nodereference.module
FileSize
13.51 KB

I've attached that combines KarenS and dan3h's contributions to the nodereference module and updated it to work with CCK 1.6.

Unfortunately, this patch only works with a few select fields. I do not yet(!) know enough about Drupal to make it work with any field and welcome any assistance on that part :-)

tsega’s picture

Title: Make nodereference fields sortable in Views » How can I add the non-standard node fields?

This has been a great help! Now I can see some of the referenced fields as view filters, great! One more thing though I need to expose the none standard field types of the referenced node type, meaning the once that were created using CCK as view filters. How do I do that? Here's the code I believe that does it but it doesn't work for the field type I've added, i.e. the field_accessability. Need help please!

...'fields' => array(
         'title' => array(
           'name' => t('@name Referenced Node: Title', array('@name' => $name)),
           'handler' => 'views_handler_nodereference_field_nodelink',
           'option' => array(
              '#type' => 'select',
              '#options' => array(
                'link' => 'As link',
                'nolink' => 'Without link'
               ),
           ),
           'sortable' => true,
           'addlfields' => array('changed', 'nid'),
           'help' => t('Display the title of the node.'),
         ),
         'field_accessablility' => array(
           'name' => t('@name Referenced Node: Accessablility', array('@name' => $name)),
           'handler' => 'views_handler_content',
           'option' => array(
              '#type' => 'select',
              '#options' => array(
                'link' => 'As link',
                'nolink' => 'Without link'
               ),
           ),
           'sortable' => true,
           'addlfields' => array('changed', 'nid'),
           'help' => t('Display the accessibility of the node.'),
         ),
...

It seems like from the patch provided by dan3h, in the fields array you just add all the fields to be provided as filters, now I believe the problem I'm facing is giving the handler for my field. Please help me to solve this!

lelizondo’s picture

Category: feature » support

I have been looking for something like this and this patch works great, but I need some other fields, the ones I create, not only the Title, ID, Created time, updated time....

I've found some other patches for the nodereference module (including the ones in this post) but for some reason the only one I can patch it's this one, the cck_nodereference-1.6_sort.patch, so I don't really know if there's another patch that does what I need.

Anyway, why can I apply this patch but not the other ones (always get Hunk Failed errors)? am I doing something wrong?

Thanks in advance.

Luis

llribas’s picture

Hi,

I need to order by title of referenced node, and I found this patch that looks the solution.

I have:

  • Drupal 5.3
  • CCK 5.x-1.6-1
  • Views 5.x-1.6

I downloaded the patch cck_nodereference-1.6_sort.patch, and applied succesfully, but no new options appears in "fields", "filters" or "Sort Criteria" in Views as I expected. I want the "Sort Criteria" -> "Referenced Node: Title".

Do I need to make something more before?
Is drupal 5.3 the problem?
Do I need to change %name for @name as mentioned above?

Getting this functionality working would be rewarding for me!

thx

Lluís

llribas’s picture

Forget this post... now it works... is it possible I must "Clear views cache" before the new options appear after applying the patch?? it looks like this, or I don't understand.

butler’s picture

Something funky with this patch:

cck_noderefe…1.6_sort.patch

... please see issue I created here:

http://drupal.org/node/237855

Thanks...

lelizondo’s picture

To show a non-standard node field from a node reference in a view I had to add manually every single field, maybe this is not elegant but it works.

I created 2 content types:

1. Member. With fields like first name, last name and a node reference field 'event'
2. Event. Just a name and a date field 'field_m_date'

You could show in a view the Member First Name, Last Name and the Event which is a Node Reference to Event, but you can't show the Event's date.

To do this and thanks to the patch subbmitted by xxvelcrar in comment 20 I had to do this:

/**
 * Join in non-standard fields from Content Type Event
*/
function nodereference_views_tables() {
  $tables['content_type_member'] = array(
    'join' => array(
      'left' => array(
	    'table' => 'node',
	    'field' => 'nid'
	  ),
	  'right' => array(
	    'field' => 'nid'
	  ),
    ),
    $tables['content_type_event'] = array(
      'join' => array(
        'left' => array(
	      'table' => 'content_type_member',
	      'field' => 'field_m_event_nid'
	    ),
	      'right' => array(
	      'field' => 'nid'
	    ),
      ),
      'fields' => array(
        'field_m_date_value' => array(
	    'name' => t('Date from Event using Node reference'),
	    'help' => t('Use this field to show the date of the event being referenced by the Event in the Member'),
            'handler' => views_handler_field_dates_iso(),
            'option' => 'string',
	    'sortable' => TRUE,
	    ),
      ),
      'sorts' => array(
	    'field_m_date_value' => array (
	    'name' => t('Date from Event using Node Reference'),
	    'handler' => views_handler_field_dates_iso(),
	    ),
      ),
    ),
  );   
return $tables;
}

The most important thing to understand is how to create the neccesary joins (see http://drupal.org/node/99564) in my case, I had 3 related tables, node content_type_member and

content_type_event. content_type_event joins like this:

content_type_event.nid = content_type_member.field_m_event_nid

and content_type_member joins like this:

content_type_member.nid = node.nid

I hope someone with more experience could clean this code to have a more elegant solution, perhaps a more general solution. I understand that this is included in Views 2 and it works great but I had to make it work for drupal 5.x

Two more things, maybe they are related to each other.

After I created my view I found out that using views_handler_field_dates as a handler for the dates would show the date 12/31/1969 7:33 for every record I don't know why but I found an excelent solution adding one more function:

/**
* Provide a list of all standard supported iso date output handlers.
*/
function views_handler_field_dates_iso() {
  return array(
    'views_handler_field_date_small_iso'  => t('As Corta Date'),
    'views_handler_field_date_iso'        => t('As Medium Date'),
    'views_handler_field_date_large_iso'  => t('As Long Date'),
    'views_handler_field_date_custom_iso' => t('As Custom Date'),
    'views_handler_field_since_iso'       => t('As Time Ago')
  );
}

Thanks to westwesterson in http://drupal.org/node/147563 for that.

After I changed the handler views_handler_field_dates to views_handler_field_dates_iso everything works fine, BUT (there's always a but) I can't filter (I know the code I'm posting doesn't include filters) using the date and the date field shows like this: 2008-05-10T00:00:00. I don't know how to remove the T00:00:00 and could someone please confirm if this is why I can't filter? This is actually the real date of the event so that's ok except for the T00:00:00

Thanks

Luis

lelizondo’s picture

I solved the issues I had, see this link to see how I did it: http://drupal.org/node/261408. However I must insist that I haven't found a general solution, maybe this is possible only with views 2, but I'm using 5.x and this is working for me.

lelizondo’s picture

There is a great solution for this using computed field:

http://drupal.org/node/290443

KarenS’s picture

Status: Needs review » Closed (won't fix)

Closing old issues. None of us is using the D5 version any more, so hard to provide any support. Sorry.