There must be a good reason why nodereferences in a table view can't be sorted? Is it because all the view really has is the nid to sort on? This would be a nice enhancement if someone could figure out how to alpha sort on nodereference in a table view.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

merlinofchaos’s picture

Project: Views (for Drupal 7) » Content Construction Kit (CCK)
Version: 5.x-1.6 » 5.x-1.x-dev
Component: Code » Views Integration
gubilla’s picture

Subscribing

mkalbere’s picture

Version: 5.x-1.x-dev » 6.x-2.x-dev

In the Latest cvs 6.x the feature is still not available, any chance that it will ?

igor.ro’s picture

Status: Active » Needs review
FileSize
1.19 KB

I was need ability to sort nodes by nodereference's delta.

So I make changes in cck views integration allow to sort by delta all multivalue field.

Here is a patch.

Thanks.

mkalbere’s picture

Thanks for the hint !
It gave me a direction .. but now i'm stuck again. Actually, I'm not very confortable with views structure so ...i dont really "know" what I'm doing .. I just guess.

Seems that views change since you realise the patch. So I modified it. Now the cols are marked as sortable in the view->edit->array. But the view of the view (the output) is still not sortable . Any idea of what I'm missing ?
Marc

if ( $field['type']=='nodereference') {
      $field_name = $field['field_name'];
      $label_truncated = truncate_utf8(t($field['widget']['label']), 10, TRUE);
      $title = t('@label (!name) delta sort', array('@label' => t($field['widget']['label']), '!name' => $field['field_name']));
      $title_short = check_plain($label_truncated);
      $data[$field_name."_nid"]['sort'] = array(
          'field' => $field_name."_nid",
          'table' => $db_info['table'],
          'handler' => 'content_handler_sort',
          'additional fields' => $additional_fields,
          'content_field_name' => $field_name."_nid",
       );
       $data[$field_name .'_sort'] = array(
        'group' => t('Content'),
        'title' => $title,
        'title short' => $title_short,
        'help' =>  t($field_types[$field['type']]['label']) .' - '. t('Appears in: @types', array('@types' => implode(', ', $types))),      
      );
       $data[$field_name."_nid"]['field']['click sortable']=1;
       if (is_array($data[$field_name."_nid"]['field']['columns']))
        foreach($data[$field_name."_nid"]['field']['columns'] as $key=>$column)
	 $data[$field_name."_nid"]['field']['columns'][$key]['sortable']=1;
      }
igor.ro’s picture

We speak about different things.
I made patch that will add sort item for sort in view administration.
You speak about field that will be delta and we will have ability click sort on table style of this view.
Then we need to create not sort but a field.
This is simple.
Give me some time :)

igor.ro’s picture

Here is a patch.

mkalbere’s picture

Whaouh ! Great work.
It .. allmost did the trick !
Now node_ref fields are exposed to sort, and it works.
Just a detail remains, when the view output a table, header are supposed to be "ajax sortable", but it's not the case. Any idea why it doesn't ?

Aren Cambre’s picture

Subscribe. This is an excellent new feature.

Aren Cambre’s picture

Status: Needs review » Needs work

Updating status since clickable table columns are not working.

igor.ro’s picture

I fix it. Could you please review the patch?

igor.ro’s picture

Status: Needs work » Needs review
Daniel A. Beilinson’s picture

tryed to use #11 with 3x-dev, not work for me

mkalbere’s picture

Sorry for the late answer (busy time ..) & thanks for all your efforts.
... but unfortunately it doesn't work.
1) I don't know why you the for multiple, node_ref can be single
2) Why creating extra-field (delta), I understand that you don't want to mess up the "original cck". But since people intragrating your patch are supposed to know what they are doing, it complicate (mess up) the field selection.

feel free to ask for more info / test
++
Marc

Aren Cambre’s picture

Isn't the ultimate goal a SQL query that A. has a JOIN to include the nodereference target node's title and B. sorting on that target node's title? Are we furthering that goal with this patch?

igor.ro’s picture

I did not made patch for 3x-dev branch. Should I adapt this patch to 3x-dev branch to include it in release?

fox’s picture

I would be happy with a simple sort without clickable headers.

markus_petrux’s picture

Status: Needs review » Needs work

@igor.ro: Nope. This would have to be made for CCK2, and we'll port to CCK3.

I'm not really sure what this patch do. It would have to include inline code documentation. Marking as this needs work for that reason.

If it does what I think it does, I have one question: do we really want to expose deltas for all multiple value fields to views?

Maybe yched has more suggestions here, though.

markus_petrux’s picture

Title: Sort Nodereferences in Table View » Expose delta columns for multiple value fields to Views

Better title?

igor.ro’s picture

Title: Expose delta columns for multiple value fields to Views » Sort Nodereferences in Table View
Status: Needs work » Needs review

Marc thanks for you review and sorry for low description of my patch. Every part of the code needs documentation. :)

Final version of the patch is #11.
The goal of this patch is to provide a view field, an view argument, a view filter and a view sort for delta (number of item that we have in multiple fields).
When we set default cck fields as multiple, cck creates a new table in DB with fields (nid, delta, field_{field_name}_value), where {field_name} means machine-readeble name of cck field.

Example of using:
1) Create a node type "master"
2) Create a node type "slave"
3) Add multivalue nodereference field {field_slaves} on "master" node type and set is reference to nodes of "slave" node type.
4) Create a view that shows teasers of "slave" nodesSome illustration of this.

we have slave nodes that are related to "master" node. Nid of "master" we get from argument node-nid. (Use relationship by nodereference {field_slaves}.)
5) Add sort by nodereference {field_slaves} delta sort. And you will see that salve nodes will be sorted by order we select on master node add/edit page.

An other example:

Slave nodes have type "Letter". For example we have 6 letters. Administrator import them from english alphabet csv by node import module.
So we have

| nid | title | type |
| 34 | A | letter |
| 35 | D | letter |
| 36 | L | letter |
| 37 | P | letter |
| 38 | R | letter |
| 39 | U | letter |

Master nodes will have type "Word". This nodes have field {letters}. We use not standart widget for {letters} nodereference field, for example CCK Node Reference ASM Select http://drupal.org/project/nodereference_asmselect or Node Reference Views Select http://drupal.org/project/nodereference_views_select or some other custom widget that provide ability to set order of nodes (letters).

We create a node "word" and set order of "letter" nodes as DRUPAL. So values of {letters} field = DRUPAL.
Then we have

| nid | title | type |
| 34 | A | letter |
| 35 | D | letter |
| 36 | L | letter |
| 37 | P | letter |
| 38 | R | letter |
| 39 | U | letter |
| 40 | title1| word |

and for {letter} field we have separeted table

content_field_letters

| nid | delta | field_letters_value |
| 40 | 0 | 35 | D
| 40 | 1 | 38 | R
| 40 | 2 | 39 | U
| 40 | 3 | 37 | P
| 40 | 4 | 34 | A
| 40 | 5 | 36 | L

We need to create a view that shows all letters that are selected for some word node.
But problem is that with out this patch we have not sort by delta for view.
So view will sort it by nids or by creation data and we will get result of this view as
ADLPRU.

If you want to know more real using of this feature - it is some type of polls like "Select 10 films and order them by quality (from your point of view)".

I think mshaver and merlinofchaos have more real examples for this code.

And as mkalbere ask I provide field to show delta and also argumet and filter to filter by delta.
http://drupal.org/comment/reply/227129/1814274#comment-1674926

About this post http://drupal.org/comment/reply/227129/1814274#comment-1814502
Aren you are right that join same table twice is not good from SQL point of view. But I think that checking if we already have joined this table is not very good idea. It will need to add more custom code, but i think we need to have maximum reuse of code we have. We can add this checking in future if this feature will be added to release.
What do you think?

P.S.: Please give more detailed description of problems you have with this patch.
Thanks.

igor.ro’s picture

Yes this title is better.
I thought about "does we need expose delta to all multiple fields?".
My point of view is:
If we provide a cck core that allow to create a custom field.
We make set this fields as multivalue by simple setting ['muiltivalue'] => TRUE, in field_info hook. And also we provide integration with views. So we need to provide not only intergation of field that contains value but all field that are created automatic by cck.
So this feature will make cck more common and flex and user friendly.

For example:
I create a field that will contains user's messangers numbers. (messanger type, messanger number)
User can have several messangers like (Skype, ICQ, Yahoo, MSN and etc.)
Admin provide list of messangers on cck field settings page and set this field as multivalue.
Then user can add many values of this field
For example

|Skype | test1 |
| ICQ |12345|
| MSN | test2 |

and he can to reorder them by importance. the simplest way is to use default drag and drop widget. :)
it will be userfrendly.

But I all ideas are welcome. I think community have top priority minds. :)
Thanks.

markus_petrux’s picture

I see the use-case, but the code added by the patch itself would have to be documented. However, it seems to me that this feature could be implemented by the modules you've mentioned.

This would add meaning to deltas, and I think this was not desired in CCK (otherwise if would have been here already). So I cannot say further than this. I just barely recall something on this direction was mentioned when working on the multigroup module.

If it needs to get in, then this needs work:

- The code needs to be documented using inline code comments // foo bar goes here.
- The issue title now states it's related to noderefs, but this is not reflected in the code, as far as I can see. Although, it seems to expose delta columns to views, and I think this would have to be optional to avoid adding more clutter to the views ui.
- If this is about exposing delta columns to views, then I would suggest adding them to the $db_fields array, or something on that direction, so that we don't duplicate code. We already have a loop for columns that generates the relevant views items.

markus_petrux’s picture

markus_petrux’s picture

Title: Sort Nodereferences in Table View » Expose "delta" column in multiple value fields to Views

Better title.

markus_petrux’s picture

Status: Needs work » Needs review
FileSize
2.42 KB

Attached is a slight variation of the patch in #11. It is against CCK2, but it should also work for CCK3.

Changes are:
- Added a small inline code comment on top to describe what the code is about.
- Cleaned empty white spaces at end of lines.
- Using 'delta' for the Views item as well as for the db field.
- Reviewed title, title short and help texts to be a bit more consistent with other multiple column fields.

Tested here and worked like a charm.

igor.ro’s picture

Thanks. For your fixes.
I was realy bisy now to fihish work on this patch.

markus_petrux’s picture

Sorry, I just noticed we only need to append the delta definitions on the first pass of the columns loop. Note that a field may expose more than one column, and we're doing delta stuff in a loop per each column, so we just need to proceed when if ($i == 0 && $field['multiple']) {.

Patch re-rolled.

igor.ro’s picture

marcus if you will just add delta as additional db field for multiple fields, you will need to change a lot of the code in loop.

First of all we would not get field because code in loop makes onle one field for first db field.
Second we need to use views_handler_field_numeric for delta, but code in loop allows us to use just content_handler_x handlers.

From my point of view patch we did with you it the best we can :)

What do you think about this?

markus_petrux’s picture

Yes, before rolling a patch here, I tried to include the delta in the actual loop, and I ended up adding too much if/elses, that I went back to your approach to add them later.

markus_petrux’s picture

Status: Needs review » Fixed

Well, I have finally moved the block that adds the delta column to views off the loop.

Committed to CVS, branches CCK2 (patch) and CCK3 (patch).

igor.ro’s picture

Perfect.
Thanks Markus.

Status: Fixed » Closed (fixed)

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

andrewsuth’s picture

Just to get some clarification of this new filter:

If I expose a filter to my users (eg. vid) for multivalue videos for example.

If I then use it like: vid=2

This filter will filter out the nodes which do not contain a multigroup video field with a vid=2, right?

It cannot be used to display the vid=2 for each of the nodes - I guess I should use Fields: Group Multivalues (delta) for that.

igor.ro’s picture

To: andrewsuth
That's true. (about group Multivalues).
Why do you use "vid" to select number of item in multiple field? I think it is correct to call it "delta".
But this feature is on field config level. You can not sort it and filter it, you can not use it as argument and can not show delta as field.
See what I mean?