1

I have a news page set up as a page display, showing nodes of a specific Content Type. /news works as expected.

I now want to improve the view: When someone goes to /news/pancreas or /news/heart, for example, nodes are filtered for that particular topic.

I have an entity reference field (Groups audience) whose machine name is og_group_ref, which allows unlimited categories.

For my display mode, I am outputting that field as an ID (although I'm not sure this is correct). For results without a topic on the view page they look something like the following screenshot (with ID of category shown).

screenshot

However if I add a category to the URL, such as /news/heart, I do not get any results.

I have a contextual filter and a relationship set up from trying to follow a guide elsewhere, but I do not think it is correct and I'm sadly pretty fuzzy on how contextual filters and relationships work. I am not really sure I'm even on the right track.

screenshot

screenshot

screenshot

How can I set up categorization based on an entity reference field?

4
  • 1
    A contextual filter is a good way to do this. How did you configure the contextual filter? Commented Aug 29, 2022 at 7:20
  • Does your "Groups audience" entity reference point to a Group, or an Organic Group, or a taxonomy term? Details matter! It looks like your project may be a D7 -> D9 migration from Organic Groups to the newer Group module, but that isn't clear from your question. Commented Aug 29, 2022 at 13:57
  • 1
    @hotwebmatter YES it is a migration attempt!!! I didn't realize this made a difference. I didn't do the migration but I understand we have one field that is migrated from organic groups and one field that is new under d9. I think the migrated D7->D9 field is the preferred one we want to use. Thank you so much for putting this on my radar that it makes a difference. Any idea how to set up the context/relationship for the migrated info? Commented Aug 30, 2022 at 23:08
  • 1
    @PatrickKenny I actually attempted to replicate the D7 view configuration and found the d7 context relationships were not available in D9, meaning something significant has changed between d7 and d9. So I tried a bunch of different options for contextual filters and relationships and could not find any that would successfully pull term-specific related content :/ Commented Aug 30, 2022 at 23:15

1 Answer 1

1

Judging by the name of the "og group" field, the Drupal 7 site probably used Organic Groups and the Drupal 8 site probably uses Group.

These modules have completely different implementations, so it's important to know which one you're working with when you configure your View -- and not to mix the old way with the new way.

It seems likely that the "og group" field contains legacy data from Drupal 7 Organic Groups. (It's common in migrations to create fields to preserve important data from the old site for the historical record, even if it is not used anymore in the new site.)

The OG group reference for "heart" has ID 46, but this is likely just a taxonomy term reference rather than a full OG implementation. (OG for Drupal 9 is currently under heavy development; I would not recommend installing both OG and Group on the same site.)

The "Groups audience" field refers to the new Group implementation, where "Heart" has ID 13366. This is the Group which matters for your Views contextual filter. But your View also has a Relationship with og_group_ref so you may be mixing things here.

Your View may not need a Relationship at all, so to simplify debugging I would remove that and concentrate on your contextual filter.

You may find that it's relatively straightforward to set up a contextual filter on Groups Audience, as long as the URL contains the Group number rather than its label. (In other words, try news/13366 instead of news/Heart.)

If you are certain that all Groups will always have a corresponding og_group_ref, you could instead opt to use a contextual filter on that taxonomy term reference, as long as the URL contains the taxonomy term ID rather than its label. (In other words, try news/46 instead of news/heart.)

Once you get the contextual filter working, I recommend you post another question to see how you can make this filter work with the word heart in the URL segment rather than the number 13366. It's a separate issue.

5
  • 1
    Great!! That helped a bunch, I was indeed able to add a contextual filter to field_group (from organic groups) and now can filter by organic group category. I'll see if I can work out the aliasing (news/heart instead of news/46) and will post again if I can't figure it out. Thanks again! (PS. I was just recently brought on to this project and see they are indeed using both D9 groups and D9 organic groups). Just curious why you recommend not mixing the two, just for the inherent confusion in using two overlapping modules? Commented Sep 17, 2022 at 21:02
  • PS. Bonus points if you can point me to an article documenting how to show the entity's text/alias instead of number. :) Commented Sep 17, 2022 at 21:14
  • 1
    For anyone else struggling with this, I found some valuable information here: drupal.org/project/drupal/issues/2915934 Specically: " But if you add the Relationship for the taxonomy reference field you are making all the term data available to this View! So after you have added the Relationship for the taxonomy term into your Content Views, the Name Contextual filter becomes available it's in the Taxonomy term Category." Long story short, after I added a relationship to my organic group some additional contextual filter options showed up. Commented Sep 19, 2022 at 4:54
  • @PatrickMan Glad you got it working! Feel free to post another question about using the entity label instead of the ID number, and I'll see if I can help with that as well (the answer is too long for a comment!) Commented Sep 20, 2022 at 15:39
  • @PatrickMan In case you did not figure out how to use news/heart instead of news/46, you might find that aliasing the numerical path gets your what you want. Anyway, that's one quick and dirty approach. Drupal hides this configuration because it might not work -- different ID numbers may be labeled with the same string. Commented Sep 29, 2022 at 18:09

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.