1

I am creating a website in Drupal 8 that shows info about team members of a business.

I've created two new content types: - Department - Team Member

I've created an entity reference field in team member that refers to the department he/she works for. The relation is many to many so I allowed multiple values.

Now I want to create a menu item for each department and when clicked a page shows with the team members working for that department.

I am totally new to Drupal so please explain steps in details :)

I've tried creating a view and add "Entity Reference" but I don't know how to proceed after that. I also tried to add a filter criteria but didn't work as well.

1 Answer 1

0

Phase1:
You would need to construct a view using contextual filters, relationships and dynamic path (url) to display team members for a department.

  • First create a view with Content type of Department. Also enable Create a page and add path like for example, /department/%. Here % is used to make your path dynamic (to pass arguments such as nid using contextual filters). Then click continue & edit.
  • Now in your view under the advanced section, add a reverse relationship to your team member content type. You should select something like A bridge to the content entity that is referencing content via field_teammember. The field_teammember is an entity reference field from team member content type.
  • Next under fields, add all the required fields you need. If you add any of your team member content type fields, do not forget to add relationship in those fields settings.
  • Again under the advanced section, add contextual filters. Select Content:Nid and in the settings do not add relationship because this Nid will be of the department content type (base table of your view).
  • In the same contextual filter configuration, under When the filter value is NOT available section, select provide default value and then select type as Raw value from URL. The path component value will be the position of % in your url. In the path you specified /department/%, its in the second position. Select 2 for your path component value and click apply.

This should have already generated a view as you required.
Now to preview your view, find an Nid of any department node and paste it in the Preview with contextual filters and click update preview. This will display the team members of that department.

Phase2:
Now create a view of departments list for menu items.

  • Create a view and select Content type of Department. You do not require a page for this view. Click Continue & edit.
  • Under fields, if you already have a Title field as your department name then add only an Nid field. And in the Nid field settings enable Exclude from display.
    Note: If you don't have title field as your department name, then remove the title field and add a department name field.
  • Now if you are using title field, disable Link this field to the original piece of content option. Then expand Rewrite results section and enable Output this field as a link. In the link path, add your dynamic url created for previous view like www.xyz.com/department/[nid].
    Note: In the link, Nid is replaced with %. This could be some other value in your view, so you need to get this from Replacement Patterns in the field settings.

This will create another view for your departments list. Now install and enable Menu views module if you don't have it. Next in the Administrator/Structure/Menus, create a menu and in the settings select departments list view to display the list as menu items.
Hope this helps.

2
  • Thanks for the detailed answer, I've been trying phase1 so far and something is not right. Here are some settings that are not listed in your answer: - The view format is 'Unformatted list' - The Format:Show is 'fields' - The fields are: title field from department, and all fields from members with the reference from advanced listed as the used reference for each field.
    – Bishoy
    Commented Nov 20, 2015 at 5:23
  • - When creating the reference, I got two choices: Content referenced from, and content using. I chose the first one I actually don't know the difference. The status now is that the filter is working fine, I write the node id for the department in URL and it shows its title, but no members info is shown on the page, only the department title and an empty page
    – Bishoy
    Commented Nov 20, 2015 at 5:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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