0

I'm working with Drupal 7, and trying to get a view set up that will display blog categories and articles based on their category.

The category is a Taxonomy - Blog Categories - let's assume it's:

  • rock-and-roll
  • pop
  • jazz
  • other

I'm hoping for the following path behavior:

  1. /blog/ should list all articles.
  2. /blog/rock-and-roll/ should list articles that have the rock-and-roll taxonomy set.
  3. /blog/non-existant-term/ should show page not found
  4. /blog/rock-and-roll/real-article-title should show the relevant article
  5. /blog/rock-and-roll/fake-article-title should show page not found

I've tried a ton of different combinations of relationships and contextual filters, but I just can't get it to work. The closest I can get is where all of the 'proper' urls work... I get category lists, or I get article displays... but I also still get a page when I go to a path with either an invalid taxonomy term or an invalid article name.

It seems like it should be something with the Contextual Filter validation stuff... but anything I've tried with like validation based on a taxonomy or whatever just stops anything from working. I no longer get listings or articles properly.

I'm not sure what other information to include - I'm trying to avoid adding any modules (because I've basically been told not to) and I'm trying to avoid making it multiple views.

-------- EDIT --------

I've tried some of the stuff suggested by prkos but I'm not having any more success. I've attached some screenshots of my configs... with a little bit of censored stuff. It probably doesn't matter, but I'm expected to be circumspect. The original URL/Taxonomy I used isn't actually the final stuff, it was just example... so there might be some mismatch there.

Main view config Relationship config Contextual Filter config

So, with these changes in place... now if I go to url/news2/ I get a Page not found. url/news2/all seems to work, but then url/news2/validterm doesn't work. validterm doesn't get any results in the view preview thing either... where it did before.

1 Answer 1

1

You can make the Validation through the Contextual filter.

When dealing with 2 Contextual filters it's always best to first fully configure each independently, make sure they work before you add both to work together.

  1. First create the Category Contextual. Since you want to use term names, and not term IDs (only term IDs are stored in ER fields), you have to add the Relationship first for the ER field (require it if you want to show only the results that have this field filled in).

  2. Once you add the Relationship you can add the Term Name Contextual fiter, it will use the Relationship, and configure it:
    When the filter value is NOT available
    Provide default value
    Taxonomy term ID from URL
    Load default filter from node page, that's good for related taxonomy blocks
    Limit terms by vocabulary
    Category
    Filter to items that share any term
    When the filter value IS available or a default is provided
    Specify validation criteria
    Taxonomy term name
    Category
    Transform dashes in URL to spaces in term name filter values
    Action to take if filter value does not validate
    Hide view

    Adjust the Case options, and any other option to your needs.

  3. Test that it's working by entering a name of your category into the View Preview field jazz and checking the results only show nodes with that term. (Preview only tests that Views filtering is working correctly, but it doesn't test passing of the argument to Views because you're feeding them manually, that needs to be tested on a live page.)

    Test single word categories first, if they work test complex ones with dashes and adjust the settings if necessary until you get the desired result.

    Test that you can bypass it by entering all into Preview, it should show all Views results (you can change it to something else in Contextual settings).

    Entering a wrong word (notmycategory) should display No query was run in Views, which means you would get Page not found in live conditions.

  4. Now create the second Contextual, for the Title. You don't have to remove the first one to test the second one without interference.

    Bypass the first filter by always entering the all as the first argument into Preview. Once you add the second Contextual you will have to enter two arguments into Preview to test it (all/some-title, or jazz/all, or all/all to bypass both contextual filters).

    When the filter value is NOT available
    Provide default value
    Raw value from URL
    3
    When the filter value IS available or a default is provided
    Specify validation criteria
    Basic validation
    Action to take if filter value does not validate
    Show "Page not found"

    Adjust the Case and other options to your needs.

  5. Test the second Contextual by entering various titles in your appropriate Case in Preview: all/my-title and entering the wrong title all/notmytitle to get the "No query was run".

  6. Test that the two Contextual filters are working together, although if they work on their own there shouldn't be any problems combining them (jazz/some-title, notcategory/title, jazz/not-title, etc.)

  7. Once it's working in Views Preview, test everything on the live page to make sure arguments are passed through correctly.

Once all this is said, if all you need is path control in your site you can do that with Pathauto module to create aliases for your nodes based on categories and their titles.

Views only creates lists of content, it doesn't affect the core paths. You will still have the node/ID paths active on your system, and any path aliases you might have. You should take care not to create duplicate paths for the same content, as it can be considered duplicate content by search engines (it dilutes in any case), and it's confusing to users.

8
  • I've tried following your suggestions, but even at only step 3, everything seems to stop working at all.
    – Mir
    Commented May 11, 2021 at 0:38
  • 1
    Looking at your screenshots you've added the wrong Relationship, and the wrong Contextual filter. The Relationship needs to be the one for the ER field, not the generic catch-all list of terms. You need to recognize the name of the field you have on your content type that references taxonomy Category. Once you have that Relationship you will be able to add the Name Contextual from Taxonomy term Category, not the one from your screenshots. If category_url is your field, then adding a Contextual for it can work directly, but only with term IDs, not term names, without Relationships.
    – prkos
    Commented May 11, 2021 at 1:46
  • 1
    Ah , I forgot to mention the Path! You need to add % for each contextual filter that takes argument from URL, so your path should be news2/%/%, or news/%taxonomy_term/%title. What Drupal version are you using?
    – prkos
    Commented May 11, 2021 at 16:35
  • 1
    And if you want the /blog/ or the /news2/ links to work I think you'll need to select to display all results when the filter isn't present in the URL, instead of the default value ID from URL.
    – prkos
    Commented May 11, 2021 at 16:38
  • 1
    You can pull in information about your additional Taxonomy Field into Views with the Relationship, just like you do for ER fields, only you need the taxonomy field Relationship first. Content stores reference to Taxonomy term ID through an ER field, and Taxonomy term stores the extra field info. So in Content Views you first add the Relationship for the Taxonomy field, then you are able to add the Relationship for the extra field (category_url). And then you can do what you like with that info because now it's available in your Content Views.
    – prkos
    Commented May 12, 2021 at 18:58

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.