strapi-community / strapi-plugin-slugify

A plugin for Strapi Headless CMS that provides the ability to auto slugify a field for any content type.

Home Page:https://market.strapi.io/plugins/strapi-plugin-slugify

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

slugification: unique slug field respected

selected-pixel-jameson opened this issue · comments

I have this plugin setup like so.

enabled: true,
    config: {
      contentTypes: {
        jam: {
          field: 'slug',
          references: 'title',
        }
      },
    },

I do not want the title field to be unique. But I do want the slug fields to be so when I generate the slug field using the strapi content type builder I check the unique field.

This is not respected when using this plugin and allows duplicate slugs to be generated.

In order to allow for this situation I would recommend the following:

If the field designated for the slug is flagged as unique provide a configuration option that allows the user to decide if a validation error should be thrown or if slugifyWithCounter() should be used .

Looks like you are already working on this.

1463e67

Any idea when you'll have a new release with this in it?

Actually I just looked at slugifyWithCounter and I'm not actually sure this will solve the problem of having unique slugs across the api.

Looks like you are already working on this.

1463e67

Any idea when you'll have a new release with this in it?

V2.1.0 has been released with these changes.

Actually I just looked at slugifyWithCounter and I'm not actually sure this will solve the problem of having unique slugs across the api.

Across the API as in across models? I believe Strapis unique constraint is only within the model.

I do agree that the unique constraint should be accounted for, I will look into this.

@ComfortablyCoding Did this get addressed yet?

No, not yet. It looks lke their is some issue with filters while using entityService on 4.1.3 it will take some time to investigate further. I will test and see if the next release (which should be tomorrow) will fix them.

Any luck with the more recent release?

I have been thinking about this some more and i dont see a reason that this is the plugins responsibility.

If the slug should be unique then the reference field(s) should be set to unique to ensure this. Adding the code to check the slug uniqueness when the reference fields are not set as unique does not make sense to me. Its extra code that is not needed.

I agree. I think that was the problem though. I had set the field to unique and it isn't respecting that constraint set on the field. Is that not what you are seeing?

In case of your example the title field (which is the reference field) should be set to unique, This should enforce and ensure the slug will remain unique.

I will double check tomorrow. But I think that was the problem. That the unique constraint was not being respected.

Oh. Sorry just re-read. Let me think about this.

Ok. So it sounds like we are halfway to my solution.

I don't believe that title should have to be forced to be unique. There are definite use case were we want content to have the same name, but have a unique slug. Take Wordpress for example, you can save two "Posts" and give them the exact same title, but it will generate two unique URLs based-off the slug that is generated from the Post Title.

Here's an example of what I'm looking for.

Post 1
Title: This is an example title
Generated Slug this-is-an-example-title

Post 2
Title: This is an example title
Generated Slug: this-is-an-example-title-1

What you want can be accomplished by using slugifyWithCount or adding the entity id with compound reference once it has been implemented in #28.