Skip to content

Commit

Permalink
Merge pull request #64 from bolt/feature/split-forms-in-multiple-files
Browse files Browse the repository at this point in the history
Split forms config in a `config/extensions/bolt-boltforms/` folder
  • Loading branch information
bobdenotter authored Apr 20, 2021
2 parents 608dc48 + a39b101 commit f3df793
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 1 deletion.
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
csfix:
vendor/bin/ecs check src --fix
make stancheck


stancheck:
vendor/bin/phpstan --memory-limit=1G analyse -c phpstan.neon src
64 changes: 64 additions & 0 deletions docs/creating_new_forms.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,67 @@ your very own Bolt form is located in `config/extensions/bolt-boltforms.yaml`.

The first time you open it, you'll see a few options are already defined for you.
The default configuration file includes a lot of comments to help you get started.

If you have a few forms and managing the `config/extensions/bolt-boltforms.yaml`
file becomes a chore, you can split each form into its own file in `config/extensions/bolt-boltforms/`.

For example, `config/extensions/bolt-boltforms/contact.yaml` will create a form called `contact`.
Note, you should **only** put the configuration for this form, for example:

```yaml
notification:
enabled: true
debug: false
debug_address: [email protected] # Email address used when debug mode is enabled
debug_smtp: true
subject: Your message was submitted
subject_prefix: '[Boltforms]'
replyto_name: '{NAME}' # Email addresses and names can be either the
replyto_email: '{EMAIL}' # name of a field below or valid text.
to_name: 'WebsiteName'
to_email: '[email protected]'
from_name: 'WebsiteName'
from_email: '[email protected]'
feedback:
success: Message submission successful
error: There are errors in the form, please fix before trying to resubmit
fields:
name:
type: text
options:
required: true
label: Name
attr:
placeholder: Your name...
constraints: [ NotBlank, { Length: { 'min': 3, 'max': 128 } } ]
email:
type: email
options:
required: true
label: Email address
attr:
placeholder: Your email...
constraints: [ NotBlank, Email ]
message:
type: textarea
options:
required: true
label: Your message
attr:
placeholder: Your message...
class: myclass
needreply:
type: choice
options:
required: false
label: Do you want us to contact you back?
choices: { 'Yes': 'yes', 'No': 'no' }
multiple: false
submit:
type: submit
options:
label: Submit my message »
attr:
class: button primary
```
30 changes: 29 additions & 1 deletion src/BoltFormsConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Bolt\Configuration\Config;
use Bolt\Extension\ExtensionInterface;
use Bolt\Extension\ExtensionRegistry;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Yaml\Yaml;
use Tightenco\Collect\Support\Collection;

class BoltFormsConfig
Expand Down Expand Up @@ -34,7 +36,10 @@ public function getConfig(): Collection
if ($this->config === null) {
// We get the defaults as baseline, and merge (override) with all the
// configured Settings
$this->config = $this->getDefaults()->replaceRecursive($this->getExtension()->getConfig());

/** @var Extension $extension */
$extension = $this->getExtension();
$this->config = $this->getDefaults()->replaceRecursive($this->getAdditionalFormConfigs())->replaceRecursive($extension->getConfig());
}

return $this->config;
Expand All @@ -61,4 +66,27 @@ private function getDefaults(): Collection
'foo' => 'bar',
]);
}

private function getAdditionalFormConfigs(): array
{
$configPath = explode('.yaml', $this->getExtension()->getConfigFilenames()['main'])[0] . DIRECTORY_SEPARATOR;

$finder = new Finder();

$finder->files()->in($configPath)->name('*.yaml');

if (! $finder->hasResults()) {
return [];
}

$result = [];

foreach ($finder as $file) {
$formName = basename($file->getBasename(), '.yaml');

$result[$formName] = Yaml::parseFile($file->getRealPath());
}

return $result;
}
}

0 comments on commit f3df793

Please sign in to comment.