cms icon indicating copy to clipboard operation
cms copied to clipboard

[4.x]: Duplicate slug does not show error message

Open JeanLucEsser opened this issue 3 years ago • 1 comments

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

  1. Add "maxSlugIncrement" => 1 to general config
  2. Create 2 entries with the same slug
  3. 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

JeanLucEsser avatar May 02 '22 15:05 JeanLucEsser

This is the same behavior as Craft 3, fwiw. Agree it’s awkward though.

brandonkelly avatar May 02 '22 19:05 brandonkelly

@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 avatar Nov 07 '22 15:11 jesuismaxime

@jesuismaxime What is the section’s entry URI format?

brandonkelly avatar Nov 08 '22 15:11 brandonkelly

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 avatar Nov 08 '22 16:11 jesuismaxime

@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.

brandonkelly avatar Nov 09 '22 14:11 brandonkelly

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!

jesuismaxime avatar Nov 09 '22 16:11 jesuismaxime

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.

yoannisj avatar Nov 14 '23 15:11 yoannisj