0

I searched for this for some time now but couldnt find an anwser. I am using drupal 9.1.5 and pathauto. I have content types department and depratment news. The news are linked to the department. So whenever you create a department news you link it to a department. Now I want to display all depratment news linked to the department when i am on the department page (all children) and when I am on a page of a children (department news) I want to display all siblings, i.e. all departments news linked with the same department.

My url for departments is [node:title] and for department news is [node:field_department:entity]/[node:title]

I thought that using a contextual filter in my view using the raw value from the url would work. So I used path component 1 and checked to use path alias. However this does not display anything. enter image description here

When using path component 2 and disable to use path alias it works for the department news, which makes sence as I am internally on page node/xxx. I was so frustrated that I tried every other combination of path component and use path alias but nothing works. When using a fixed value for a department everything works as expected for that specific department

4
  • Pathauto aliases always resolve to the node you build the alias for. Even if you use a referenced entity to build the alias this doesn't mean you can filter it. If you want to filter a referenced content type you have to use relationships.
    – 4uk4
    Apr 15, 2021 at 12:11
  • Okay, so its just not possible to use contextual filters. I am quite new to drupal, can you tell me how to use that relationship?
    – michael
    Apr 15, 2021 at 13:18
  • It sounds like you have an entity reference on your news referencing the department.. Use that reference to build this display (you don't even need to add relationships for that). E.g. contextual filter where field_dept_ref = current dept. Don't rely on paths for this.
    – sonfd
    Apr 15, 2021 at 14:35
  • Though you will need an additional context filter to EXCLUDE the current news article
    – sonfd
    Apr 15, 2021 at 14:36

1 Answer 1

0

You can search for examples for each of your questions with "backwards views" and "sibling views". In short:

Show children on parent page

Create a Views that lists Children, then add the Entity reference field as the Contextual filter (not Content ID). Configure it to Provide default value Content ID from URL.

No Relationships needed in this solution. Place the Block on Parent pages and don't forget to configure the Block contextual option, it won't work without it.

Show siblings on child page

Create a Views that lists Children. This time you'll need 2 Relationships and 1 Contextual filter, all sequentially linked.

The first Relationship is for your ER field (the Field on Child that references a Parent), this one introduces the Parent info.

The second Relationship is the "backwards" Relationship for the same field, it is named a bit differently that the "forwards" one. Make the second Relationship use the first Relationship. This will trace path "backwards" from Parent to all their Children.

Add the Content ID Contextual filter and make it use the second Relationship. Configure it to Provide default value Content ID from URL. This will tell Views which Child's page you're currently viewing and will match to the Parent and then Parent's children.

Exclude current Child from Views

Your current Child will also be listed in Views because it satisfies the same condition! (It's also Parent's Child, just like other Siblings.)

You can exclude it by adding the second Contextual Content ID Filter that doesn't use any Relationships, Provides default value Content ID from URL, but under the last section More also check Exclude.

2
  • Thanks a lot. Id had to use the same relation twice, but the second one used the first relation. It kinda makes sence but I think I would not come up with that.
    – michael
    Apr 16, 2021 at 10:08
  • The two Relationships are for the same ER Field, but they are not named exactly the same, one is something like "Content referenced from" and the other "Content referencing …". Very similar wording but not the same. It's easy once you learn the logic behind it, but yes it's nice to have a cheatsheet to refresh your memory.
    – prkos
    Apr 16, 2021 at 10:46

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.