I have 2 content types: location and maintenance.

I would like a view that shows all the locations with a search box at the top so the user can search the right location; when users click on a location, they are redirected to the location details and the maintenance nodes listed under the location details.

So the workflow is like this:

  • Search the location
  • Click on the location
  • See a view with location and maintenance nodes

I have already installed entity reference and made references from maintenance to locations, but I just can't get the view settings right; a list of all the locations is what I am getting, so far.

  • 1
    did you create a relationship in your views? – Tritof Jul 17 '14 at 12:13
  • No i created a relationship with entity reference – GoempieK Jul 19 '14 at 9:47
  • The entity reference between the nodes in the system is half the work. It makes the system know they are related. You need to create a relationship in your views to display only the ones related to the one chosen. You can find it under advanced on the right side of your views, then add a relationship with the entity reference and provide a nid in the page (url for example). If you are new to Drupal it can be tricky to catch but it is pretty handy and you won't regret the time spent on it. – Tritof Jul 22 '14 at 14:26

Your existing view listing all locations is fine for now (assuming a path like "/locations"). Create another display in that view (or a complete different view if you want). This second display/view should first get a relationship to the (location) entity referenced by the field "location". Then the view should have a contextual filter on a node id using that relationship; argument should be in the URL, e.g. by using something like "/locations/%/maintenance" as path.

So if you have two locations (nids: 1 + 2) and 4 maintenance nodes (nids: 3-6), you should see all locations at "/locations" and all maintenance for the first location at "/locations/1/maintenance". Now you just need to link from the first view to the second one by using the correct path "/locations/[nid]/maintenance" and it should be the way you want it. You can take it a step further by displaying all locations along with the maintenance on a single page. You'll need an additional module (if not yet installed): views_field_view.

If that is installed, you can proceed to the next step and include the maintenance view in the location view. Add two new fields, first of type "Content: Nid" and second of type "Global: View". For the latter field choose the maintenance view in the dropdown and enter "!nid" in the argument textbox. By doing this, the current nid of the location is send as an argument to the maintenance view and thus only displaying maintenance nodes that are connected to that current location.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.