Skip to content

Commit

Permalink
Fix issue creating entries when collection has a title format set (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
duncanmcclean authored Apr 21, 2023
1 parent 8e0b07d commit 36f3f9f
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 7 deletions.
14 changes: 8 additions & 6 deletions src/Http/Controllers/GuestEntryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,6 @@ public function store(StoreRequest $request)
->locale($this->guessSiteFromRequest($request))
->published(false);

if ($request->has('slug')) {
$entry->slug($request->get('slug'));
} else {
$entry->slug(Str::slug($request->get('title')));
}

if ($collection->dated()) {
$this->ignoredParameters[] = 'date';
$entry->date($request->get('date') ?? now());
Expand All @@ -70,6 +64,14 @@ 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->touch();

event(new GuestEntryCreated($entry));
Expand Down
7 changes: 6 additions & 1 deletion src/Http/Requests/StoreRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use DoubleThreeDigital\GuestEntries\Rules\CollectionExists;
use Illuminate\Foundation\Http\FormRequest;
use Statamic\Facades\Collection;

class StoreRequest extends FormRequest
{
Expand All @@ -25,7 +26,11 @@ public function rules()
'_redirect' => ['nullable', 'string'],
'_error_redirect' => ['nullable', 'string'],
'_request' => ['nullable', 'string'],
'slug' => ['required_without:title'],
'slug' => [
Collection::find($this->get('_collection'))->autoGeneratesTitles()
? null
: 'required_without:title',
],
];

if ($formRequest = $this->get('_request')) {
Expand Down
50 changes: 50 additions & 0 deletions tests/Http/Controllers/GuestEntryControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,25 @@
$this->assertSame($entry->slug(), 'this-is-fantastic');
});

it('can store entry when collection has title format', function () {
Collection::make('comments')->titleFormats(['default' => 'BLAH {{ name }}'])->save();

$this
->post(route('statamic.guest-entries.store'), [
'_collection' => 'comments',
'name' => 'So, I was sitting there and somebody came up to me and I asked them something.',
])
->assertRedirect()
->assertSessionHas('success');

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

$this->assertNotNull($entry);
$this->assertSame($entry->collectionHandle(), 'comments');
$this->assertSame($entry->get('title'), 'BLAH So, I was sitting there and somebody came up to me and I asked them something.');
$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 custom form request', function () {
Collection::make('comments')->save();

Expand Down Expand Up @@ -999,6 +1018,37 @@
$this->assertSame($entry->slug(), 'allo-mate');
});

it('can update entry if collection has title format', function () {
Collection::make('albums')->titleFormats(['default' => '{{ artist }} - {{ name }}'])->save();

Entry::make()
->id('allo-mate-idee')
->collection('albums')
->slug('allo-mate')
->data([
'title' => 'Guvna B - Allo Mate!',
'name' => 'Allo Mate!',
'artist' => 'Guvna B',
])
->save();

$this
->post(route('statamic.guest-entries.update'), [
'_collection' => 'albums',
'_id' => 'allo-mate-idee',
'record_label' => 'Unknown',
'name' => 'Allo Mate',
])
->assertRedirect()
->assertSessionHas('success');

$entry = Entry::find('allo-mate-idee');

$this->assertNotNull($entry);
$this->assertSame($entry->collectionHandle(), 'albums');
$this->assertSame($entry->get('title'), 'Guvna B - Allo Mate');
});

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

Expand Down

0 comments on commit 36f3f9f

Please sign in to comment.