Skip to content

Commit

Permalink
Forms integration
Browse files Browse the repository at this point in the history
  • Loading branch information
royduin committed May 25, 2023
1 parent 1929cb3 commit 0b6eca3
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 10 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ This package helps you integrate Statamic within your Rapidez project by adding
- Products, categories and brands are integrated through [Runway](https://github.com/duncanmcclean/runway) as read only resources so you can link them to the content
- Products, categories and pages collections as starting point so you can extend this however you'd like
- Route merging so Statamic routes work as fallback
- Page builder fieldset with a product slider, content and image component
- Page builder fieldset with a product slider, content, image and form component
- Responsive images with [spatie/statamic-responsive-images](https://github.com/spatie/statamic-responsive-images)
- Breadcrumbs for pages
- Globals available in all views
Expand Down Expand Up @@ -93,6 +93,10 @@ By default you'll get the configured content on categories and products availabl
- Product: `resources/views/vendor/rapidez/product/overview.blade.php`
- Category: `resources/views/vendor/rapidez/category/overview.blade.php`

### Forms

When you create a form you could use `rapidez-statamic::emails.form` as HTML template which uses the [Laravel mail template](https://laravel.com/docs/master/mail#customizing-the-components) with all fields in a table, make sure you enable markdown!

## License

GNU General Public License v3. Please see [License File](LICENSE) for more information.
13 changes: 13 additions & 0 deletions resources/fieldsets/form.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
title: 'Form'
fields:
-
handle: form
field:
max_items: 1
display: Form
type: form
icon: form
localizable: false
listable: hidden
instructions_position: above
visibility: visible
5 changes: 5 additions & 0 deletions resources/fieldsets/page_builder.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,9 @@ fields:
fields:
-
import: image
form:
display: Form
fields:
-
import: form
localizable: true
11 changes: 11 additions & 0 deletions resources/views/emails/form.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<x-mail::message>
# {{ $form->value()->title() }}

<x-mail::table>
| | |
| --- | --- |
@foreach($fields as $field)
| **{{ $field['display'] }}** | {{ $field['value'] }} |
@endforeach
</x-mail::table>
</x-mail::message>
13 changes: 13 additions & 0 deletions resources/views/form_fields/text.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<x-rapidez::input
:name="$field['handle']"
:type="$field['input_type']"
:label="$field['display']"
:value="$field['old'] ?: $field['value']"
:placeholder="$field['placeholder'] ?? false"
:class="$field['error'] ? 'border-red-500' : ''"
:maxlength="$field['character_limit'] ?? false"
:required="in_array('required', $field['validate'])"
/>
@if($field['error'])
<div class="text-red-500">{{ $field['error'] }}</div>
@endif
12 changes: 12 additions & 0 deletions resources/views/form_fields/textarea.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<x-rapidez::textarea
:name="$field['handle']"
:label="$field['display']"
:value="$field['value']"
:placeholder="$field['placeholder'] ?? false"
:class="$field['error'] ? 'border-red-500' : ''"
:maxlength="$field['character_limit'] ?? false"
:required="in_array('required', $field['validate'])"
/>
@if($field['error'])
<div class="text-red-500">{{ $field['error'] }}</div>
@endif
8 changes: 1 addition & 7 deletions resources/views/page_builder.blade.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
@foreach ($content as $set)
@if(!View::exists('page_builder.' . $set['type']) && !View::exists('rapidez-statamic::page_builder.' . $set['type']))
@if(!app()->environment('production'))
<hr>View for set {{ $set['type'] }} not found.<hr>
@endif
@else
@includeFirst(['page_builder.' . $set['type'], 'rapidez-statamic::page_builder.' . $set['type']], $set)
@endif
@includeFirstSafe(['page_builder.' . $set['type'], 'rapidez-statamic::page_builder.' . $set['type']], $set)
@endforeach
31 changes: 31 additions & 0 deletions resources/views/page_builder/forms.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
@php($form = Statamic::tag('form:create')->param('in', $form->value()->handle())->fetch())

@if($form['success'])
<div>@lang('The form was submitted successfully')</div>
@return
@endif

<form @attributes($form['attrs'])>
@foreach($form['params'] as $name => $value)
<input type="hidden" name="{{ $name }}" value="{{ $value }}">
@endforeach

<div class="grid grid-cols-12 gap-3">
@foreach($form['fields'] as $field)
<div @class([
'col-span-3' => $field['width'] == '25',
'col-span-4' => $field['width'] == '33',
'col-span-6' => $field['width'] == '50',
'col-span-8' => $field['width'] == '66',
'col-span-9' => $field['width'] == '75',
'col-span-12' => $field['width'] == '100',
])>
@includeFirstSafe(['form_fields.' . $field['type'], 'rapidez-statamic::form_fields.' . $field['type']], $set)
</div>
@endforeach
</div>

<x-rapidez::button type="submit">
@lang('Submit')
</x-rapidez::button>
</form>
25 changes: 23 additions & 2 deletions src/RapidezStatamicServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Rapidez\Statamic;

use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\View;
Expand All @@ -26,8 +27,9 @@ public function boot()
->bootViews()
->bootListeners()
->bootRunway()
->bootPublishables()
->bootComposers();
->bootComposers()
->bootBladeDirectives()
->bootPublishables();
}

public function bootConfig() : self
Expand Down Expand Up @@ -106,6 +108,25 @@ public function bootComposers() : self
return $this;
}

public function bootBladeDirectives() : self
{
Blade::directive('attributes', function (string $expression) {
return "<?php echo (new \Illuminate\View\ComponentAttributeBag)($expression); ?>";
});

Blade::directive('return', function () {
return "<?php return; ?>";
});

Blade::directive('includeFirstSafe', function (string $expression) {
$expression = Blade::stripParentheses($expression);

return "<?php try { echo \$__env->first({$expression}, \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); } catch (\InvalidArgumentException \$e) { if (!app()->environment('production')) { echo '<hr>'.__('View not found: :view', ['view' => implode(', ', [{$expression}][0])]).'<hr>'; } } ?>";
});

return $this;
}

public function bootPublishables() : self
{
$this->publishes([
Expand Down

0 comments on commit 0b6eca3

Please sign in to comment.