0

Stumped. This should be Drupal 101, but the dots aren't connecting.

Scenario: from among many, I have two content types:

  • employee
  • department

Each content type displays on its own but we will also run a list of employee teasers as a list within a views block on the department pages. This is easy to create: I use views to render the list, i.e.,

  • View uses filter criteria of "department" for a teaser list of employees.

Set the views block to display on the appropriate department node page on the blocks admin page and all is good. However, I have 7 department pages. Sure I could create the first view, clone that view per required filter type, but, better, is reusable code. Both content types have the same "department" field list (uses the core list module) where I've identified the department.

How can I use that "Department" list field to create the link that joins the employee list to the department when rendering the department page?

2

Using Drupal 101, and assuming that you have one Department page per department, and one Employee page per employee, and assuming that each employee can be associated with more than one department ...

First add a node reference field to the Employee content type (field_department). Set it to reference the Department content type and set the number of values to unlimited.

Then create a view of Content type:Employee.

Add a Relationship on Content:Department (field_department) and make it Required.

Then add a Contextual Filter on Content:NID. Have it use the Relationship you have just created and configure it to Provide Default Value: Content ID from URL when the filter is not available.

Under Advanced > Query Settings check Distinct. This type of query might produce duplicates.

Place the View block on the Department page, and this will restrict the list to employees related to the department currently being viewed.

A similar approach can be used with term reference fields, but you would require two relationships (first to the term, then from the term to the other content type). I cannot speak about the List module as it appears to be discontinued on D.O.

|improve this answer|||||
  • Thanks, Triskelion I'm giving it a try now. FYI: list is a core module in D7. – Screenack Jun 8 '13 at 19:36
  • 1
    Thanks for the clarification. I have changed my edit to reflect that you meant the core module. – Triskelion Jun 8 '13 at 20:12
  • Your first suggestion is working well BUT, from a purely abstract data model perspective, I'm thinking the taxonomy-based method is a better model, where data is related by an attribute, not by a page assignment. Though the one-to-one relationship of the department page to department makes this a quibble? – Screenack Jun 8 '13 at 20:13
1

You add a contextual filter to the view block that grabs the department ID or title as a filter for your employee list.

Then you can re-use the 1 block at essentially any path with a department in the url you setup as the path in the Blocks UI, or if your using Context, etc.

|improve this answer|||||
  • I don't what you mean by understand "contextual field" Is this available in core or what module does it require? – Screenack Jun 8 '13 at 19:30
  • 1
    See the answer from @Triskelion. When I said "field" i ment "filter", apologies. His answer is more thorough. – tenken Jun 8 '13 at 20:12

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.