agentejo / cockpit

Add content management functionality to any site - plug & play / headless / api-first CMS

Home Page:http://getcockpit.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Filter by slug with different lang active

stefanKBL opened this issue · comments

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

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",
            "...": "..."
        }
    ],

@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

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.