Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
I have a nodereference field in a custom content type.
In Views: I want to be able to output a link like this:
node/[field_custom_noderef_nid]/my_custom_string
The problem is that instead of outputting the nid of the referenced node, the [field_custom_noderef_nid] token is outputting the title of the referenced node...
Same applies to userreference module.
But maybe I'm missing something here... ?
Comment | File | Size | Author |
---|---|---|---|
#16 | relationship-delta.JPG | 40.34 KB | Kimberley_p |
Comments
Comment #1
adam1 CreditAttribution: adam1 commentedI am struggeling with the same problem ...
Comment #2
markus_petrux CreditAttribution: markus_petrux commentedThis is because Views is giving you the rendered output of the field in the replacement pattern.
To solve this, instead of using "Rewrite the output of this field" option of the field in Views, you need to provide a new formatter for nodereference fields, and then you can choose it from the "Format" option of the field in Views.
To create more formatters for noderef fields, see these examples:
http://drupal.org/project/cck_formatters
http://drupal.org/project/nodereference_formatters
Another approach: use a Views custom field - PHP code:
http://drupal.org/project/views_customfield
Comment #3
adam1 CreditAttribution: adam1 commentedThank you very much, markus. i will try out the alternatives.
Comment #5
prabhakarsun CreditAttribution: prabhakarsun commentedEasy way;
1. Add a relationship of required node reference.
2. Add a Node:Nid field with this relationship,rewrite it's output as you wish.
Comment #6
dag_helstad CreditAttribution: dag_helstad commented@prabhakarsun: Thanks! I would *never* have figured out that by myself :)
/Dag
Comment #7
m4olivei@prabhakarsun: The only issue with your solution is when the node reference field can have multiple values. When you have multiple values, adding the relationship and adding the node id of the relationship will produce a row for every value of the node reference field. What then? This is where I find myself.
What I would like is to simply add the node reference field to my fields which would give me a list of the node id's referenced, which I ca then use and theme how ever I want in template files. Do I have to write a custom formatter in that case?
Thanks,
~Matt
Comment #8
m4oliveiHey,
I figured out what to do in my situation. What I ended up doing was to set the formatter to 'Teaser' on the node reference in the view, and then used a template file for that node type to render it how I wanted it.
~Matt
Comment #9
eheuvel CreditAttribution: eheuvel commented@prabhakarsun: Works like a charm, thanks.
Comment #10
johnnydarkko CreditAttribution: johnnydarkko commented@prabhakarsun: I would like to use your solution but for some reason the "Node" option is not available Groups selection field. Does anybody else have this problem?
Here's what I've done so far:
1. I clicked on the + sign on in the relationships section.
2. Opened the groups pull down but "Node" was not an option.
3. Kept Groups at "All" option, Node: NID is not listed.
Comment #11
Bartezz CreditAttribution: Bartezz commented@#5
Brilliant, thanx for sharing!
Comment #12
shrop CreditAttribution: shrop commented@prabhakarsun: Thanks much. Works great! Been banging my head over this one.
Comment #13
ErikU CreditAttribution: ErikU commentedAwesome and so simple!! Thx
Comment #14
devkinetic CreditAttribution: devkinetic commented@johnnydarkko and anyone else hitting this...
There is no 'node' in the relationship section. You find 'content' and then add the node reference field you are trying to use.
Then add node:nid to your fields and select that relationship from the dropdown on the field settings screen.
Comment #15
skyself CreditAttribution: skyself commented@devkinetic
Thanks a lot for the explanation for us newbies.
The prabhakarsun's solution suddenly became obvious.
Comment #16
Kimberley_p CreditAttribution: Kimberley_p commented@m4olivei I had the same issue as you - having a nodereference field with multiple values.
I got round it another way - just in case anyone else is stuck - that is useful only if you have 3 or 4 values, otherwise the view becomes a bit cumbersome:
I added the nodereference field as a relationship 4 times (or as many as the max number in your multiple values). Then you set the delta to 1 for the first relationship, 2 for the second and so on so that each relationship corresponds to each value (image attached). Then you need to add the node id field the same number of times and assign a relationship to each.
You get a row for each value this way.
Comment #17
sja1 CreditAttribution: sja1 commentedHey Kimberley, thanks for sharing! I wish I had seen this a couple hours ago.
I think you meant to say you get a column for each value, rather than a row.
In my case I want a comma separated list of nids. So I used your technique and then hid all but the last nid field from display. I then enabled the rewrite the output option on the last field and used a rewrite value like [nid_1],[nid_2],[nid_3].
I had been trying to get a combination of the reference_views and views_delimited_list modules working but that route dead-ended. Your solution is very simple and works perfectly.
Comment #18
frederickjhHi!
I wanted to add a comment regarding sja1's comment #17.
I am working on a export file that requires the fields to be separated by a pipe (|). However the number of multiple values varied from record to record. So that we did not end up with extra multiple pipe delimiter characters when values were empty. I did the following:
Create a Relationship for each Delta on the node reference.
Add a Node ID field for each Delta, Exclude from display, Hide if empty, Do not rewrite if empty.
For the 2nd through the last Node ID fields rewrite the display. In the rewrite text field put the delimiter (pipe in my case) followed by the replacement pattern for the current field.
Create a Global: Custom Text field. In the Text field put the replacement patterns for all your Node NID fields.
Because fields are not rendered if they are empty the delimiters before the 2nd to the last Node ID fields also do not render.
As more sites migrate away from Drupal 6 these type of exports will be needed.
Frederick