[4.x]: Duplicate slug does not show error message
What happened?
Description
If you disable automatic slug increment via "maxSlugIncrement" => 1, then creating a second entry with the same slug does not show an error message. Entry stays (or becomes) inactive but there is no visual indication as to why.
Steps to reproduce
- Add
"maxSlugIncrement" => 1to general config - Create 2 entries with the same slug
- Try to save the second entry as active
Expected behavior
Should generate an error message and set the entry as inactive.
Actual behavior
Set the entry as inactive without any error message.
Craft CMS version
4.0 RC2
PHP version
No response
Operating system and version
No response
Database type and version
No response
Image driver and version
No response
Installed plugins and versions
This is the same behavior as Craft 3, fwiw. Agree it’s awkward though.
@brandonkelly is there any update on that matter? A client duplicates the same entry 20+ times and we got 20+ entries with the same slug (maxSlugIncrement isn't override tho; so it should display at least -1,-2,-3,...,-19 suffixes)
@jesuismaxime What is the section’s entry URI format?
There wasn't one, this section is not "expose" and called with GraphQL, so I add one base on {id}-{slug} and make the api call using uri instead of slug to avoid any issues. Shouldn't slugs be unique even if there's no URI format?
@jesuismaxime Got it, so this is actually working as expected. Craft will only automatically increment a slug if it’s needed to achieve a unique URI for the element.
{id}-{slug} isn’t going to work since each element has a unique ID already.
The uri format {id}-{slug} allows me to avoid any further issues. 50 entries with the same slug is actually quite an issue when you fetch entries by slug using GraphQL, that is why I added an uri format based on the id, because all the slugs of the 50 entries are the same, now their URI is not.
Anyway, I still think this is an issue - at least hide the 'Slug' input when the URI is not in use or add an CLI command/option to resave entries and reformat the slug based on the title could be a nice feature!
Anyway, I still think this is an issue - at least hide the 'Slug' input when the URI is not in use or add an CLI command/option to resave entries and reformat the slug based on the title could be a nice feature!
Hmm, from looking at other similar issues here in the craftcms/cms repository, I understand there is some confusion as to when Craft cares about unique slugs and increments them to achieve this uniqueness. However, I often use slugs for entries that don't have URLs. Not for querying, but for example for deep-linking or to populate the id attribute on HTML tags in templates (useful for example, to point to these tags for a11y with aria-labelledby).
Therefore, I don't think Craft should go the route of hiding the slug field for elements without URIs. Maybe adding a setting to entry sections where admins can explicitly choose whether slugs should be unique or not, or maybe just improving the documentation about the built-in incrementing mechanism of slugs.