Skip to content

Commit

Permalink
Prevent duplicate slugs (#76)
Browse files Browse the repository at this point in the history
* Prevent duplicate slugs

* Fix styling

---------

Co-authored-by: duncanmcclean <[email protected]>
  • Loading branch information
duncanmcclean and duncanmcclean authored Dec 13, 2024
1 parent a37e205 commit e3b27b6
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 3 deletions.
32 changes: 29 additions & 3 deletions src/Http/Controllers/GuestEntryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Statamic\Facades\Collection;
use Statamic\Facades\Entry;
use Statamic\Facades\Site as SiteFacade;
use Statamic\Facades\Stache;
use Statamic\Fields\Field;
use Statamic\Fieldtypes\Assets\Assets as AssetFieldtype;
use Statamic\Fieldtypes\Date as DateFieldtype;
Expand Down Expand Up @@ -72,9 +73,7 @@ public function store(StoreRequest $request)
if ($request->has('slug')) {
$entry->slug($request->get('slug'));
} elseif ($collection->entryBlueprint()->hasField('title')) {
$entry->slug(
Str::slug($request->get('title') ?? $entry->autoGeneratedTitle(), '-', $entry->site()->lang())
);
$entry->slug($this->generateEntrySlug($entry));
}

$entry->touch();
Expand Down Expand Up @@ -227,6 +226,33 @@ protected function processField(Field $field, $key, $value, $request): mixed
return $value;
}

protected function generateEntrySlug($entry): string
{
$iteration = 0;

$slug = $originalSlug = Str::slug($entry->get('title') ?? $entry->autoGeneratedTitle(), '-', $entry->site()->lang());

while (true) {
$query = Entry::query()
->where('collection', $entry->collectionHandle())
->where('site', $entry->site()->handle())
->where('slug', $slug);

if ($entry->collection()->structure()) {
$query->where('parent', $entry->parent());
}

$exists = $query->count() > 0;

if (! $exists) {
return $slug;
}

$iteration++;
$slug = $originalSlug.'-'.$iteration;
}
}

protected function uploadFile(string $key, Field $field, Request $request)
{
if (! isset($field->config()['container'])) {
Expand Down
52 changes: 52 additions & 0 deletions tests/Http/Controllers/GuestEntryControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@
use Statamic\Facades\Collection;
use Statamic\Facades\Entry;
use Statamic\Facades\Site;
use Statamic\Structures\CollectionStructure;

use function PHPUnit\Framework\assertCount;

beforeEach(function () {
File::deleteDirectory(app('stache')->store('entries')->directory());
File::deleteDirectory(app('stache')->store('collection-trees')->directory());

$this->app['config']->set('guest-entries.collections', [
'comments' => true,
Expand Down Expand Up @@ -85,6 +87,56 @@
$this->assertSame($entry->slug(), 'blah-so-i-was-sitting-there-and-somebody-came-up-to-me-and-i-asked-them-something');
});

it('can store entry with duplicate slug', function () {
Collection::make('comments')->save();
Entry::make()->collection('comments')->slug('this-is-fantastic')->data(['title' => 'This is fantastic'])->save();

$this
->post(route('statamic.guest-entries.store'), [
'_collection' => encrypt('comments'),
'title' => 'This is fantastic',
])
->assertRedirect();

$entry = Entry::all()->last();

$this->assertNotNull($entry);
$this->assertSame($entry->collectionHandle(), 'comments');
$this->assertSame($entry->get('title'), 'This is fantastic');
$this->assertSame($entry->slug(), 'this-is-fantastic-1');
});

it('can store entry with duplicate slug with different parent', function () {
$collection = tap(Collection::make('comments')->structure((new CollectionStructure)->expectsRoot(true)))->save();

$one = tap(Entry::make()->collection('comments')->id('one')->slug('one'))->save();
$two = tap(Entry::make()->collection('comments')->id('two')->slug('two'))->save();
Entry::make()->collection('comments')->id('fantastic-one')->slug('this-is-fantastic')->save();

$tree = $collection->structure()->in('default');

$tree->tree([
['entry' => 'one'],
['entry' => 'two', 'children' => [
['entry' => 'fantastic-one'],
]],
])->save(0);

$this
->post(route('statamic.guest-entries.store'), [
'_collection' => encrypt('comments'),
'title' => 'This is fantastic',
])
->assertRedirect();

$entry = Entry::all()->last();

$this->assertNotNull($entry);
$this->assertSame($entry->collectionHandle(), 'comments');
$this->assertSame($entry->get('title'), 'This is fantastic');
$this->assertSame($entry->slug(), 'this-is-fantastic');
});

it('can store entry with custom form request', function () {
Collection::make('comments')->save();

Expand Down

0 comments on commit e3b27b6

Please sign in to comment.