statamic / cms

The core Laravel CMS Composer package
https://statamic.com
Other
4.05k stars 530 forks source link

Term slug wrong with mounted/linked taxonomies #4469

Closed robdekort closed 3 years ago

robdekort commented 3 years ago

Bug description

When you have a taxonomy assigned to a collection and that collection mounted to an entry you should get the following taxonomy route:

{collection_mount_handle}/{taxonomy_handle}/{term_slug}

On the index template right here:

collection_handle/taxonomy_name/index

Which is then reachable under the following url:

{mount_handle}/{taxonomy_handle}/

You can loop through your terms using:

{{ taxonomy:handle }}
    {{ url }}
{{ /taxonomy:handle }}

This however returns the wrong URL for terms. It doesn't include the slug for the entry you mounted your collection.

Notice the URL in the address bar + the term URL's in the loop here:

Screenshot 2021-10-13 at 10 28 38

It should be prefixed with /blog.

How to reproduce

  1. Add taxonomy
  2. Add some terms
  3. Add collection
  4. Mount collection
  5. Link taxonomy to collection
  6. Make taxonomy index template (in the collection folder)
  7. Loop through taxonomies
  8. See URL is wrong for terms

Logs

No response

Versions

Statamic 3.2.15 Pro Laravel 8.64.0 PHP 7.4.13 No addons installed

Installation

Starter Kit using via CLI

Additional details

No response

jasonvarga commented 3 years ago

Loop with {{ terms }} instead of the {{ taxonomy }} tag.

robdekort commented 3 years ago

Hey Jason. Yeah I tried that but {{ terms }} doesn't return anything in my case. Hmmm.

jasonvarga commented 3 years ago

I notice in your screenshot that your URL is categories but the contents show tags. How many taxonomies do you have? Tags and Categories?

If you're on /blog/categories, {{ terms }} would be outputting terms within the categories taxonomy. Do you have any categories terms?

robdekort commented 3 years ago

I've got two taxonomies here. I must've mixed and matched those examples wrong for you.

But unfortunately terms didn't output terms belonging to the taxonomy. Will verify again in a few.

robdekort commented 3 years ago

Verified, check this with the {{ taxonomy }} tag:

Screenshot 2021-10-13 at 17 49 02

And this with the {{ terms }} tag:

Screenshot 2021-10-13 at 17 48 50

Hope it makes sense.

jasonvarga commented 3 years ago

Do you have any category terms?

robdekort commented 3 years ago

Yep, see that first screenshot. One term, which is called Category 2. That name doesn't make any sense whatsoever so sorry for that. I guess there were multiple terms during my testing at some point ;-). So it's being fetched when using the {{ taxonomy }} tag. However in the bottom one you can see using {{ terms }} doesn't return anything in this case.

You can check out this PR if you want: https://github.com/studio1902/statamic-peak/tree/feature/update-seo

I was playing with taxonomies to improve the SEO features and stumbled upon it.

If it helps, a copy of the site: taxonomies.zip

jasonvarga commented 3 years ago

Ah it filters by terms that are actually used in that collection.

You don't have any entries that use any categories, so you get no results.

You're seeing "Categories" and "/blog/categories" because it's falling back to the current page. You can use a no_results condition to output something nicer.

{{ terms }}
  {{ if no_results }}
    No terms.
  {{ else }}
    {{ title }} {{ url }}
  {{ /if }}
{{ /terms }}
robdekort commented 3 years ago

Ah that's how the term tag works exactly. The no_results thing is the way indeed. Just playing here so no need to make it pretty :-).

But then the original issue/post still stands right? That the taxonomy tag should return the url for each term including the collection slug since it's mounted and attached to that collection?

jasonvarga commented 3 years ago

The taxonomy tag doesn't do anything special with the collection unless you tell it to. You could do {{ taxonomy:categories collection="blog" }} if you wanted. But that's what {{ terms }} is doing for you automatically.

robdekort commented 3 years ago

Not sure if I explained the issue right. Ping me if you want. Happy to explain.

robdekort commented 3 years ago

Jason explained it to me. I get it now, for anyone running into this, it's not a bug, it's a feature :-)

{{ terms }} will list the taxonomies with the collection handle included the URL since it lists taxonomies tied to entries in this collection. {{ taxonomy }} will list all terms without any collection handle in the URL. And this does make sense since you could attach taxonomies to multiple collections for instance. The tag knows nothing about any collection you might be currently visiting.

jasonvarga commented 3 years ago

You can force it to know about the collection with {{ taxonomy:handle collection="blog" }}, but it will still only show terms that are used in that collection. Same as {{ terms }}.