Skip to content

Commit 4532745

Browse files
authored
v1.0.1 (#1)
* enhance(console): Add make:form command * chore(docs): Add console command to docs * chore(docs): Add error message customization to docs * fix(component): Fix hidden slot * fix(ci): Fix CircleCI
1 parent 548c715 commit 4532745

File tree

7 files changed

+244
-26
lines changed

7 files changed

+244
-26
lines changed

.circleci/config.yml

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,52 @@
11
version: 2.1
22

33
orbs:
4+
php: circleci/[email protected]
5+
6+
# To be removed after https://github.com/CircleCI-Public/php-orb/pull/11
7+
executors:
48
default:
5-
executors:
6-
php-73:
7-
docker:
8-
- image: 'circleci/php:7.3-stretch'
9-
jobs:
10-
build-php:
11-
executor: php-73
12-
steps:
13-
- run: php -v
14-
- checkout
15-
- restore_cache:
16-
keys:
17-
- composer-v1-{{ checksum "composer.lock" }}
18-
- composer-v1-
19-
- run: composer install -n --prefer-dist --no-scripts --no-suggest
20-
- run: composer lint
21-
- save_cache:
22-
key: composer-v1-{{ checksum "composer.lock" }}
23-
paths:
24-
- vendor
9+
description: The official next-gen CircleCI PHP Docker image.
10+
parameters:
11+
tag:
12+
description: The `cimg/php` Docker image version tag.
13+
type: string
14+
docker:
15+
- image: 'cimg/php:<< parameters.tag >>'
16+
17+
# To be removed after https://github.com/CircleCI-Public/php-orb/issues/23
18+
jobs:
19+
build-php:
20+
parameters:
21+
version:
22+
default: '7.4'
23+
description: The `cimg/php` Docker image version tag.
24+
type: string
25+
executor:
26+
name: default
27+
tag: << parameters.version >>
28+
steps:
29+
# Because squizlabs/php_codesniffer requires ext-simplexml.
30+
# To be removed after https://github.com/CircleCI-Public/cimg-php/pull/51
31+
- run: sudo apt-get update
32+
- run: sudo apt-get install -y php<< parameters.version >>-xml
33+
- run: php -v
34+
- run: composer --version
35+
# To be removed after https://github.com/CircleCI-Public/cimg-php/pull/52
36+
- run: sudo chown $(whoami):$(whoami) ~/.composer
37+
- checkout
38+
- php/load-cache:
39+
key: v1
40+
- run: composer install -n --prefer-dist
41+
- run: composer lint
42+
- php/save-cache:
43+
key: v1
2544

2645
workflows:
2746
build:
2847
jobs:
29-
- default/build-php
48+
- build-php:
49+
name: build-php-<< matrix.version >>
50+
matrix:
51+
parameters:
52+
version: ['7.4', '7.3', '7.2']

README.md

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
![Build Status](https://img.shields.io/circleci/build/github/Log1x/sage-html-forms?style=flat-square)
55
![Total Downloads](https://img.shields.io/packagist/dt/log1x/sage-html-forms?style=flat-square)
66

7-
This is a simple package for [HTML Forms](https://github.com/ibericode/html-forms) that allows you to easily render forms using a corresponding Blade view (if one is present) with Sage 10.
7+
This is a simple package for the plugin [HTML Forms](https://github.com/ibericode/html-forms) that allows you to easily render forms using a corresponding Blade view (if one is present) with Sage 10.
88

99
A few additional opinionated tweaks include:
1010

@@ -35,7 +35,13 @@ You can leave the "Form code" blank as it will not be used if a corresponding Bl
3535

3636
### Creating a View
3737

38-
Once your form is created, create a Blade view in `views/forms/contact-us.blade.php` where `contact-us` is equal to your form's slug.
38+
Once your form is created, simply generate a view using the slug assigned to your form:
39+
40+
```bash
41+
$ wp acorn make:form contact-us
42+
```
43+
44+
You will find the generated form view in `resources/views/forms/contact-us.blade.php` containing a simple form component:
3945

4046
```php
4147
<x-html-forms :form="$form" class="my-form">
@@ -47,7 +53,7 @@ Once your form is created, create a Blade view in `views/forms/contact-us.blade.
4753
>
4854

4955
<input
50-
name="cool_email"
56+
name="emailAddress"
5157
type="email"
5258
placeholder="Email Address"
5359
required
@@ -60,7 +66,21 @@ Once your form is created, create a Blade view in `views/forms/contact-us.blade.
6066
</x-html-forms>
6167
```
6268

63-
...and you're done. Form action variables simply reference the input `name` so `[NAME]` and `[COOL_EMAIL]` will now be readily available.
69+
When HTML Forms processes "Form Actions" – it simply fetches each input name to create the usable variables.
70+
71+
That being said, the default view would provide `[NAME]` and `[EMAILADDRESS]`.
72+
73+
#### Error Messages
74+
75+
Outside of defining your error messages on the options page, you can optionally provide them to the `<x-html-forms />` component directly:
76+
77+
```php
78+
<x-html-forms
79+
:form="$form"
80+
:messages="['success' => 'Thank you!', 'error' => 'Yikes! Try again.']"
81+
class="my-form"
82+
/>
83+
```
6484

6585
## Bug Reports
6686

resources/views/components/html-forms.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<div style="display: none;">
33
<input type="hidden" name="_hf_form_id" value="{{ $form->ID }}" />
44
<input type="text" name="_hf_h{{ $form->ID }}" value="" />
5-
{!! $hiddenFields !!}
5+
{!! $hidden !!}
66
</div>
77

88
{!! $slot !!}

src/Console/FormMakeCommand.php

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
<?php
2+
3+
namespace Log1x\HtmlForms\Console;
4+
5+
use Illuminate\Support\Str;
6+
use Roots\Acorn\Console\Commands\GeneratorCommand;
7+
8+
class FormMakeCommand extends GeneratorCommand
9+
{
10+
/**
11+
* The console command signature.
12+
*
13+
* @var string
14+
*/
15+
protected $signature = 'make:form {name* : The form slug.}';
16+
17+
/**
18+
* The console command description.
19+
*
20+
* @var string
21+
*/
22+
protected $description = 'Create a new form view for the HTML Forms plugin.';
23+
24+
/**
25+
* The view stub used when generated.
26+
*
27+
* @var string|bool
28+
*/
29+
protected $view = 'default';
30+
31+
/**
32+
* Execute the console command.
33+
*
34+
* @return mixed
35+
*/
36+
public function handle()
37+
{
38+
$this->task("Generating {$this->getViewName()}", function () {
39+
if (! $this->files->exists($this->getViewPath())) {
40+
$this->files->makeDirectory($this->getViewPath());
41+
}
42+
43+
if ($this->files->exists($this->getView())) {
44+
return;
45+
}
46+
47+
$this->files->put($this->getView(), $this->files->get($this->getViewStub()));
48+
});
49+
50+
return $this->summary();
51+
}
52+
53+
/**
54+
* Return the full view destination.
55+
*
56+
* @return string
57+
*/
58+
public function getView()
59+
{
60+
return Str::finish($this->getViewPath(), $this->getViewName());
61+
}
62+
63+
/**
64+
* Return the view destination filename.
65+
*
66+
* @return string
67+
*/
68+
public function getViewName()
69+
{
70+
return Str::finish(
71+
str_replace('.', '/', Str::slug(Str::snake($this->getNameInput()))),
72+
'.blade.php'
73+
);
74+
}
75+
76+
/**
77+
* Return the view destination path.
78+
*
79+
* @return string
80+
*/
81+
public function getViewPath()
82+
{
83+
return Str::finish($this->getPaths(), '/forms/');
84+
}
85+
86+
/**
87+
* Get the view stub file for the generator.
88+
*
89+
* @return string
90+
*/
91+
protected function getViewStub()
92+
{
93+
return __DIR__ . "/stubs/views/{$this->view}.stub";
94+
}
95+
96+
/**
97+
* Return the applications view path.
98+
*
99+
* @param string $name
100+
* @return void
101+
*/
102+
protected function getPaths()
103+
{
104+
$paths = $this->app['view.finder']->getPaths();
105+
106+
if (count($paths) === 1) {
107+
return head($paths);
108+
}
109+
110+
return $this->choice('Where do you want to create the view(s)?', $paths, head($paths));
111+
}
112+
113+
/**
114+
* Return the block creation summary.
115+
*
116+
* @return void
117+
*/
118+
protected function summary()
119+
{
120+
$this->line('');
121+
$this->line('<fg=blue;options=bold>Form View Created</>');
122+
$this->line(" ⮑ <fg=blue>{$this->shortenPath($this->getView(), 4)}</>");
123+
}
124+
125+
/**
126+
* Returns a shortened path.
127+
*
128+
* @param string $path
129+
* @param int $i
130+
* @return string
131+
*/
132+
protected function shortenPath($path, $i = 3)
133+
{
134+
return collect(
135+
explode('/', $path)
136+
)->slice(-$i, $i)->implode('/');
137+
}
138+
139+
/**
140+
* Get the stub file for the generator.
141+
*
142+
* @return string
143+
*/
144+
protected function getStub()
145+
{
146+
//
147+
}
148+
}

src/Console/stubs/views/default.stub

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<x-html-forms :form="$form" class="my-form">
2+
<input
3+
name="name"
4+
type="text"
5+
placeholder="Full Name"
6+
required
7+
>
8+
9+
<input
10+
name="emailAddress"
11+
type="email"
12+
placeholder="Email Address"
13+
required
14+
>
15+
16+
<input
17+
type="submit"
18+
value="Submit"
19+
/>
20+
</x-html-forms>

src/HtmlFormsServiceProvider.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Roots\Acorn\ServiceProvider;
66
use Illuminate\View\Compilers\BladeCompiler;
77
use Log1x\HtmlForms\HtmlForms;
8+
use Log1x\HtmlForms\Console\FormMakeCommand;
89
use Log1x\HtmlForms\View\Components\HtmlForms as HtmlFormsComponent;
910

1011
class HtmlFormsServiceProvider extends ServiceProvider
@@ -28,6 +29,10 @@ public function register()
2829
*/
2930
public function boot()
3031
{
32+
$this->commands([
33+
FormMakeCommand::class,
34+
]);
35+
3136
$this->loadViewsFrom(__DIR__ . '/../resources/views', 'HtmlForms');
3237

3338
$this->callAfterResolving(BladeCompiler::class, function ($view) {

src/View/Components/HtmlForms.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public function __construct(
3636
$hidden = null
3737
) {
3838
$this->form = hf_get_form($form);
39+
$this->hidden = $hidden;
40+
3941
$this->form->attributes = collect($this->form->messages)->merge(
4042
collect($messages)->keyBy(function ($value, $key) {
4143
return Str::snake($key);

0 commit comments

Comments
 (0)