0

I have a datastructure where "Courses" has many "Activities". I've accomplished this by using entity reference to add a course_field to the activity content type.

I am pretty conused, however, as to how to set up a view listing all activities belonging to a certain course.

I've created a view that displays activities and I've set the relationship on the view to "Entity Reference: Referenced Entity". I am a bit confused though, as to whether I should set it to "Entity Reference: Referencing Entity", but I figured the first one made more sense as activities are referencing courses with a field on the activity model.

Now I set the contextual filter to "Content: Course", but when I preview the contextual filter, typing the name of one of the courses does not filter the activities.

What is the correct way to filter activities according to what course they belong to?

1 Answer 1

0

You are correct in setting your relationship as "Entity Reference: Referenced Entity". To recap: you have a view of nodes filtered by Activity. Activity has the entity relationship field that references Course. The direction is from Activity to Course and you are building a bridge from activities to course.

Normally one would set the contextual filter up as node:nid. As you want to list all activities associated with a course, you therefore need to pass in a context that uniquely identifies the Course. Nid fits this perfectly. The nid coming in as the contextual filter needs to be of type Course. So how do you tell Views. You already have a bridge from activities to course (your relationship), and to indicate to views that the nid contextual filter is of type Course you set the relationship that you added on it. Using nid as contextual filter makes more sense in most cases, as it is the unique identifier. It could be taken from the url as a default, or could be sent in via a panel etc.

You can if you wish add another field as the contextual filter, as in the title (it may or may not be unique). But always you will need to add the relationship to the contextual filter to identify the context as belonging to Course.

enter image description here

Now to explain what you did with setting the contextual filter to 'Content Course'. You added the field that appears on the Activity type and which is your entity relationship field to Course, as the contextual filter. This field does not exist in the Course type, so you will never get a result from it. As you want to show a view of activities associated with a Course type, you have to add the relationship that links activities with courses (which you did), and then your contextual filter has to be of a field that appears in Course type. So you must first select a field that does indeed appear in Course type, and then you have to tell Views to use the relationship on that field (remember, your relationship takes you from an Activity type, which is the begin-point, to the Course type, which is an end-point, and your context needs to be on the end-type). Imagine using the nid or the title as the contextual filter. Both course and activity have both of these fields, so which type's should Views use?

The relationship is also considered when adding Fields, Filter Criteria, and Sort Criteria on your view. You have to decide whether the field, filter, or sort belongs to your base type, or to a type that you are bringing in with a relationship.

For a brilliant video tutorial series on Views, have a look at Johan Falk's Taming the Beast: Learn Views with NodeOne

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.