strapi-community / strapi-plugin-slugify

A plugin for Strapi Headless CMS that provides the ability to auto slugify a field for any content type.
https://market.strapi.io/plugins/strapi-plugin-slugify
MIT License
45 stars 21 forks source link

slugification: unique slug field respected #24

Closed selected-pixel-jameson closed 2 years ago

selected-pixel-jameson commented 2 years ago

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 .

selected-pixel-jameson commented 2 years ago

Looks like you are already working on this.

https://github.com/ComfortablyCoding/strapi-plugin-slugify/commit/1463e672e6bd65af5f5ecc3e623427fa511a3dfb

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

selected-pixel-jameson commented 2 years ago

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

ComfortablyCoding commented 2 years ago

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.

ComfortablyCoding commented 2 years ago

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.

selected-pixel-jameson commented 2 years ago

Sorry for the confusion. I just meant unique for models.

Thank you.

Jameson W Parker

On Feb 28, 2022, at 6:51 PM, daedalus @.***> wrote:

 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.

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you authored the thread.

selected-pixel-jameson commented 2 years ago

@ComfortablyCoding Did this get addressed yet?

ComfortablyCoding commented 2 years ago

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.

selected-pixel-jameson commented 2 years ago

Any luck with the more recent release?

ComfortablyCoding commented 2 years ago

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.

selected-pixel-jameson commented 2 years ago

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?

ComfortablyCoding commented 2 years ago

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.

selected-pixel-jameson commented 2 years ago

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

selected-pixel-jameson commented 2 years ago

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

selected-pixel-jameson commented 2 years ago

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

ComfortablyCoding commented 2 years ago

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