Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter by slug with different lang active #906

Closed
stefanKBL opened this issue Oct 28, 2018 · 4 comments
Closed

Filter by slug with different lang active #906

stefanKBL opened this issue Oct 28, 2018 · 4 comments

Comments

@stefanKBL
Copy link

Hi,
use case is 2 or more languages. Default is fr.
When query posts using lang EN, everything is ok, posts coming from api are using English translation if set, of default one.
Then, the logical is to set a href to each post using (in my case) the title_slug set in collection.
Next query will be to query post by slug.

  • if a use filter[title_slug], it's ok in default lang (fr) but not for others as title_slug seems to be always set to default.
  • so I use filter[title_$LANGUAGE_VAR_slug], and then it's ok, il can get eg. the english version
  • Problem with this is that if english version is not set, default one is not recover.

I surely can query by _id or check first if requested language is set and then, if not, request for the default language, but i don't want :-) (in fact that's what i do now)

So my question is, as it seems to be a good practice to keep a unique slug for a same item, even when having multilingual, what do you think about having an option like "multilingual_unique_slug=true" and set all languages slug to default one.

Thanks

@raffaelj
Copy link
Contributor

Unique slugs were discussed a few times before and the core only provides _id as unique identifier.

Do you use the inbuilt slug function {"slug": true}, which is not unique or do you use UniqueSlugs addon (or something similar)?

The language filter replaces all values with the translations after it found something. So you can't query against {"lang":"en","filter":{"title":"Hello"}} or {"lang":"en","filter":{"title_slug":"hello"}}.

{"lang":"en","filter":{"title":"Bonjour"}} works and you get your expected output:

"entries": [
        {
            "title": "Hello",
            "title_slug": "hello",
            "...": "..."
        }
    ],

Example with {"slug": true} and UniqueSlugs:

Query: {"lang":"en","filter":{"slug":"bonjour"}}

Output:

"entries": [
        {
            "title": "Welcome",
            "slug": "bonjour",
            "title_slug": "welcome",
            "...": "..."
        }
    ],

@stefanKBL
Copy link
Author

@raffaelj
Yes, using your UniqSlugs module.
But i still used "slug": true in the title field of my collection, and then i also used title_slug as parameter in UniqSlugs.slug_name config.
That was the mistake, as with this configuration i did not have "slug" field in the entries records but only title_slug, title_fr_slug and so on. (Perhaps add this in the doc 😃 even if now it seems logical for me)

Do not use "slug": true in the collection field AND Do not use a field name as UniqSlugs.slug_name

Now I let UniqSlugs.slug_name as "slug" in the config and remove "slug": true in my field collection, seems to be ok.
the entries is as you said for a single query on slug
"entries": [ { "title": "Welcome", "slug": "bonjour", "title_slug": "welcome", "...": "..." } ],
And when I request the entire collection, the new field "slug" is here, so i will use it to link my single entry
Thanks a lot @raffaelj , I really ❤️ Cockpit

@johnfoley
Copy link

johnfoley commented Oct 30, 2018 via email

@raffaelj
Copy link
Contributor

Ah, OK - I understand. So setting title_slug with "slug":true works, but if the unique slug == title_slug, the language filter replaces it with the auto-generated translation of the non-unique slug in your request.

Do not use "slug": true in the collection field

No problem per se, but may be useless...
Could be useful if you generate the unique slug on first creation and never change it to keep links consistent. The auto-generated and changing slug could be used e. g. for SEO urls like example.com/unique_identifier/unrecognized-title-slug-just-for-seo

AND Do not use a field name as UniqSlugs.slug_name

No problem at all, but don't use the same name as an auto-generated AND multilingual slug field.

I'll add it to the docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants