0

My web site has three content types ( Event, Course, Video) and 2 taxonomies(city and schools) All of them have same taxonomy referenced field. Eg:New York City events or New York City courses will be listed seperately.

Problem: How can I make term links dynamic based on content type .

All new york city contents are listing(3 content type contents) if I clicked newyork city term on event content.

I need only event type contents to be listed if I came from an event type content. It should be changed according to event,course or video content type. Any help with this would be awesome . Thanks

enter image description here enter image description here enter image description here

1

The easiest way to achieve this is by using Views.

The links rendered in Taxonomy fields (Entity reference fields) are set to go to Taxonomy term pages. The links are constructed with Taxonomy term ID in the URL: taxonomy/term/[TID].

Taxonomy term pages are created using Views, so you can easily Edit the relevant Views to adjust it to your needs. The modification you're after is adding a Contextual filter that filters out only one content type.

With that modification you could type, for example, taxonomy/term/10/article in the browser address bar, and the usual Term page for term 10 would list only results of the Article Content Type.

The other part of your question is how to make the Taxonomy reference link on the node View be sensitive to the Content type it's on, so when it's clicked it goes to the filtered list, and not the general term page that shows all results.

The easiest way to do that is to hide that field from displaying and replace it with a Views Block that would output a similar link, only Contextually sensitive to the term, and to the Content type.

Here are the steps on how to achieve this:

Make term pages sensitive to Content types

  1. Duplicate the Taxonomy term View (it's better to keep the default ones as they are, so you don't mess up the original features and it makes updates more straightforward). Rename it to make sense (Taxonomy term-Content type)
  2. Add a new Contextual Filter Content type from the Content Category (keep the one sensitive to the taxonomy term that is already there, we need it) and configure it with these options:
    When the filter value is NOT in the URL
    Display all results for the specified field
    When the filter value IS in the URL or a default is provided
    Override title
    Content tagged with {{ arguments.tid }} of type {{ arguments.type }} Specify validation criteria
    Validator Content type
    Validate user has access to the Content type
    More
    Transform spaces to dashes in URL
  3. Edit the Path to add another % to react to the Contextual filter we just added, so you end up with the Path: taxonomy/term/%/%
  4. Go to the other Display in the same View called Feed and edit the Path there to read taxonomy/term/%/%/feed
  5. Save the View. You can test it by entering the relevant arguments into Preview inside Views (for example 10/article) or by typing the URL into your browser addressbar (example.com/taxonomy/term/10/article).
  6. Optional but good UX: add No results behavior Text area No content of type {{ arguments.type }} tagged with {{ arguments.tid }} has been found. on your Page display and override for that Display only This page (override).

Create taxonomy links on Nodes sensitive to CT

To create links sensitive to the context we need Views Display Block of type Content. We can add it to the Views we created above (because that View is of type Content), or create a completely new View for this. Since this Block Views will be sufficiently different it's better to create a new View for this. Here are the steps for adding a new Views to accomplish this:

  1. Create a new Views of type Content showing Fields, add a Block Display to the View, change its name to something meaningful you can recognize later on Blocks page: Taxonomy term link sensitive to CT
  2. Add a Filter to limit to your desired Content types (event, course, video)
  3. Under Format option Show settings choose to Hide empty fields
  4. Add a new Field City (or whatever your field is called), Exclude from display, and don't tick Link label to the referenced entity
  5. Add a new Field Content type, Exclude from display and set Formatter to Entity ID (so you get CT machine name so the contextual filter later works correctly)
  6. Add a new Relationship Taxonomy term referenced from field_city and Require this relationship
  7. Add a new Field Term ID (it will use the Relationship from previous step), Exclude from display and set Formatter to Unformatted
  8. Add a new Field Custom text from Global Category and configure it:
    Create a label just tomake it easier to build, enter Taxonomy link sensitive to CT
    In Text area enter {{ field_city }} (type { type }})
    Rewrite results
    Output this field as a custom link
    Link path: taxonomy/term/{{ tid }}/{{ type }}
    Replace spaces with dashes
    No results behavior
    Hide if empty
  9. Add a new Contextual filter ID from Content Category and configure:
    When the filter value is NOT available
    Provide default value
    Content ID from URL
    When the filter value IS available or a default is provided
    Specify validation criteria
    Content
    [your CTs video course event]

    Now when you enter a node ID into Preview it will show the link to taxonomy term page that has content type argument added to it at the end.

  10. Set Pager to Display a specified number of items to 1 item
  11. Remove the Title field so you end up with only the Custom text visible and Save the View
  12. Place the Block in Content region, don't Display title, set Content ID to Node from URL, limit which Content type to show the Block on
  13. Edit your Content types (all three) Manage Display to hide the City Taxonomy Field (drag to Disabled)

And now you have the relavant Taxonomy term link that has the added Content type at the end to call on Views term page that will filter out only that content type.

  • You seem to be using D7 while I wrote the examples for D8. Instead of the Entity ID format simply choose Output machine name, it's the same thing. Set the City field Formatter to Plain text, same this as unticking the option in D8 I mentioned. The Contextual filters on the first View shouldn't be with depth, there should only be one of those, and one of Content type, change the Path... There are still steps you need to do, it's all equivalent in D7. – prkos Aug 24 '18 at 15:30
  • I mean, there's no formatter option for content type field.(5.item you mentioned) – iberx Aug 24 '18 at 15:49
  • Yes, I understood you, and I gave you an alternative for D7 in the comment, it's a standalone checkbox option, not a Formatter option. – prkos Aug 24 '18 at 16:06
  • I tried many combination since afternoon but it doesn't work or I could not make it... I have to find another way or give up.. Thanks for your support,really appreciated. – iberx Aug 24 '18 at 20:10
  • I fixed it for ya, Drupal 7 has slightly different syntax for tokens and arguments. You also hadn't selected all content types where you should had, you only included Event. But now the problem is the bock appears only on Events, because you haven't used the same City field on all relevant content types, you created new fields with new machine names. You should remove the extra City fields from video and course CTs and add existing City field to both CTs (watch for the machine name field_city). Then it will work as expected. – prkos Aug 24 '18 at 21:34

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.