Others may have done this before and much better.
Setup
Drupal 7, Views, entity reference (multilingual)
In have contenttype A which contains an entity referemce field that can make a reference to contenttype B.
Specification
A. View of items referencing
I also want to display the list of all node C and D referring to the current project. Here I also want to show multiple fields from the referring node linking to the referring node itself.
I my case contenttype A is a project, and contenttype B are Institutes cooperating on these projects. Contenttypes C and D are articles and publications which are refer to the project in question.
B. View of referenced items
On displaying a contenttype A node I want to display some of the fields from the referenced node (contenttype B) in a sidebar.
How to A. View of items referencing
A.1 Create a new view
A.1.1 Call it f.i. 'related'
A.1.2 Choose fields for the format
A.2 Open the 'Advanced' tab on the right hand side
A.3 Click the Add button behind relationships
A.4 Select
(you have ond eof these for every entity reference field in your contenttype)
In my case I have A bridge to the Content entity that is referenced via field_project_institutes
A.5 Click ApplyIn my case I have A bridge to the Content entity that is referenced via field_project_institutes
A.6 On the next screen check Require this relationshop
You can also change the name of the identifier but I never see a reason for this.
In this example it is called:Content entity referenced from field_project_institutes
Now we need an argument to provide to the relationship, and the argument will be the nid of the current contenttype A node. To do this you have to provide a contextual filter
A.7 Click the Add button behind contextual filters
A.8 Check Content: Nid
A.9 Click Apply
A.10 A new screen: Choose the relationship you just made
In this example it is called:Content entity referenced from field_project_institutes
A.11 Select Provide a default value under When the filter value is not available
A.12 Under Type select Content id from url
A.13 Then, under When the filter value IS available or a default is provided check the Specify validation criteria box
A.14 Under Validator choose content and then check the contenttype b. The contenttype that is referred to, not the contenttype that contains the reference field.
A.15 Under Filter value format choose Node ID
A.16 Under Action to take if filter does not validate choose Hide view
A.17 Apply
A.18 Save your block view
How to B: View of referenced items
B.1 Create a new view
B.1.1 Call it f.i. 'related'
B.1.2 Choose fields for the format
B.2 Open the 'Advanced' tab on the right hand side
B.3 Click the Add button behind relationships
B.4 Select
B.5 Click Apply
B.6 On the next screen check Require this relationshop
You can also change the name of the identifier but I never see a reason for this.
B.6.1 and click Apply
In this example it is called:Content referencing Content from field_project_institutes
Now we need an argument to provide to the relationship, and the argument will be the nid of the current contenttype A node. To do this you have to provide a contextual filter
B.7 Click the Add button behind contextual filters
B.8 Check Content: Nid
B.9 Click Apply
B.10 A new screen: Choose the relationship you just made
In this example it is called:Content referencing Content from field_project_institutes
B.11 Select Provide a default value under When the filter value is not available
B.12 Under Type select Content id from url
B.13 Save your viws, and place ypur block in the correct region...
Extra
Suppose you want to show an image field from the node that is referred to in the sidebar, linking to this orihinal node. Seems an easy task doesn't it?
No. The problem is that you can easily ad the field to the view, select its viewmode, but wehn you want to link this field to the containing node, all html cleaners protest:
Adding the link aroudn the field does not work in this case, the a tag contains the div tag. Rewriting the output presents me with the samen problem.
The way i figured it out finally was to
a. Add the content path, as a field (use absoklute and hide from display)
b. Add the image, set its view mode but then strip all html tags except <img>
c. Create a global text field, and there rewrite the output to
something like <a href="[path]">[field_institute_logo]</a>
d. But you still have a problem than because the field_field_institute_logo value containes stripped text from html that is normally hidden to users. So then just create a field template for this global text field removing everything from there except the a and im tags and inner attributes.
$output = preg_replace('#.*?(<(img|a).+?>).*?#is', '$1', $output);
Node reference
On trying the same with nodereference I got really stuck but solved it eventually....
A.
1. add relationships
2. search for the noderefence field you want (NB NOT with ' - reverse')
(this is the node reference field in the node referring to the node type entity B)
(In my case, Inhoud: Verwijzing (field_verwijzing) )
3. Click apply (remember the name of the relationship*)
4. Select 'Require this relationship'
5. click apply
5. Add contextual filter
6. Chose Content: Nid
7. Click apply
8. Choose the relationship you just made (*)
9. Under When the filter value is not available choose 'provide default value'
9.1 Choose 'Content id from Url'
10. Under when the filter value is avaialable or a default is providedselect 'Specify validation citeria'
10. 1 (Depending on the install there should be an extra box , but in some installs the box wont show up, and you have to click apply and then click on the contextual filter again before you can add these criteria)
Under Validator choose Content
10.2 Under contenttypes choose your contenttype B
10.3 Under filter value format choose 'Node ID'
10.4 Under action to take when filter value does not validat Choose 'Hide view'
Oh this is so familiar. I think I have done this a hundred times but after some time I forget the specifics and I "seem to have to start all over again." :D
ReplyDeleteThank you for writing this down. Much appreciated!