Skip to content

Commit

Permalink
Merge branch '2.x' into 3.x
Browse files Browse the repository at this point in the history
  • Loading branch information
duncanmcclean committed Apr 21, 2023
2 parents b61b41b + da0d213 commit 2562770
Show file tree
Hide file tree
Showing 11 changed files with 259 additions and 101 deletions.
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,21 @@
2. Then, change the addon's version constraint to `^3.0`
3. If you have any references to `DoubleThreeDigital` within your site in relation to this addon, replace it with `DuncanMcClean`

## v2.1.3 (2023-04-21)

### What's new

- Added `{{ guest-entries:success }}` tag #53 #48

### What's fixed

- Fixed `error_redirect` parameter #51 #49
- Fixed an issue when using title formats on collections #52 #50

### Breaking changes

The `success` key added in the last release has been reverted, in favour of the new `{{ guest-entries:success }}` tag. Apologies for any inconvenience.

## v2.1.2 (2023-04-17)

### What's improved
Expand Down
4 changes: 2 additions & 2 deletions docs/tags.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@ If you'd like to show any errors after a user has submitted the Guest Entries fo

### Success

If you'd like to show a success message after a user has submitted a Guest entry, you can test for the session success key.
If you'd like to show a success message after a user has submitted the Guest Entries form, you can use the `{{ guest-entries:success }}` tag, like shown below:

```antlers
{{ if {session:has key="success"} }}
{{ if {guest-entries:success} }}
Well done buddy!
{{ /if }}
```
42 changes: 13 additions & 29 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,13 +64,19 @@ 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));

return $this->withSuccess($request, [
'success' => true,
]);
return $this->withSuccess($request);
}

public function update(UpdateRequest $request)
Expand Down Expand Up @@ -149,9 +149,7 @@ public function update(UpdateRequest $request)

event(new GuestEntryUpdated($entry));

return $this->withSuccess($request, [
'success' => true,
]);
return $this->withSuccess($request);
}

public function destroy(DestroyRequest $request)
Expand All @@ -166,9 +164,7 @@ public function destroy(DestroyRequest $request)

event(new GuestEntryDeleted($entry));

return $this->withSuccess($request, [
'success' => true,
]);
return $this->withSuccess($request);
}

protected function processField(Field $field, $key, $value, $request): mixed
Expand Down Expand Up @@ -337,22 +333,10 @@ protected function withSuccess(Request $request, array $data = [])
return response()->json($data);
}

$request->session()->put('guest-entries.success', true);

return $request->_redirect ?
redirect($request->_redirect)->with($data)
: back()->with($data);
}

protected function withErrors(Request $request, string $errorMessage)
{
if ($request->wantsJson()) {
return response()->json([
'status' => 'error',
'message' => $errorMessage,
]);
}

return $request->_error_redirect
? redirect($request->_error_redirect)->withErrors($errorMessage, 'guest-entries')
: back()->withErrors($errorMessage, 'guest-entries');
}
}
20 changes: 20 additions & 0 deletions src/Http/Requests/Concerns/HandleFailedValidation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace DoubleThreeDigital\GuestEntries\Http\Requests\Concerns;

trait HandleFailedValidation
{
/**
* Get the URL to redirect to on a validation error.
*
* @return string
*/
protected function getRedirectUrl()
{
if ($this->has('_error_redirect')) {
return $this->get('_error_redirect');
}

return parent::getRedirectUrl();
}
}
3 changes: 2 additions & 1 deletion src/Http/Requests/DestroyRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

class DestroyRequest extends FormRequest
{
use Concerns\WhitelistedCollections;
use Concerns\WhitelistedCollections,
Concerns\HandleFailedValidation;

public function authorize()
{
Expand Down
10 changes: 8 additions & 2 deletions src/Http/Requests/StoreRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

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

class StoreRequest extends FormRequest
{
use Concerns\AcceptsFormRequests,
Concerns\WhitelistedCollections;
Concerns\WhitelistedCollections,
Concerns\HandleFailedValidation;

public function authorize()
{
Expand All @@ -24,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
3 changes: 2 additions & 1 deletion src/Http/Requests/UpdateRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
class UpdateRequest extends FormRequest
{
use Concerns\AcceptsFormRequests,
Concerns\WhitelistedCollections;
Concerns\WhitelistedCollections,
Concerns\HandleFailedValidation;

public function authorize()
{
Expand Down
13 changes: 11 additions & 2 deletions src/Tags/Concerns/FormBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ protected function createForm(string $action, array $data = [], string $method =
$html .= $this->redirectField();
}

if ($this->params->get('error_redirect') != null) {
$html .= $this->errorRedirectField();
}

if ($this->params->get('request') != null) {
$html .= $this->requestField();
}
Expand Down Expand Up @@ -61,6 +65,11 @@ private function redirectField()
return '<input type="hidden" name="_redirect" value="'.$this->params->get('redirect').'" />';
}

private function errorRedirectField()
{
return '<input type="hidden" name="_error_redirect" value="'.$this->params->get('error_redirect').'" />';
}

private function requestField()
{
return '<input type="hidden" name="_request" value="'.$this->params->get('request').'" />';
Expand All @@ -73,8 +82,8 @@ private function params(): array
return $params[$param] = $redirect;
}
})->filter()
->values()
->all();
->values()
->all();
}

/**
Expand Down
5 changes: 5 additions & 0 deletions src/Tags/GuestEntriesTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,9 @@ public function hasErrors()
{
return session()->has('errors');
}

public function success(): bool
{
return session()->get('guest-entries.success', false);
}
}
Loading

0 comments on commit 2562770

Please sign in to comment.