Skip to content

Commit

Permalink
Merge pull request #66 from maximehuran/feature/form-extension-types
Browse files Browse the repository at this point in the history
Be able to extends settings form
  • Loading branch information
delyriand authored Oct 9, 2023
2 parents 132c7e8 + fc71bdb commit 556a975
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ It's also possible to run test fixtures with a local suite in development: `make
By default, a fixture will replace the value of a setting if it already exists.
If you want to keep a value as it is in the database when running this fixture, you can use the flag `ignore_if_exists: true` for each element that you want to be kept.

### Extends existing settings form

If you want to add a field to an existing settings form (like in another plugin), you can check [the example in the test app](dist/src/Form/SettingsExtensionType.php)

### Use CLI

You can use a CLI command to set a value for a setting directly from the console:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ monsieurbiz_sylius_settings:
form: App\Form\SettingsType
default_values:
demo_message: My amazing message
demo_title: My amazing title
enabled: true

sylius_ui:
Expand Down
55 changes: 55 additions & 0 deletions dist/src/Form/SettingsExtensionType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

/*
* This file is part of Monsieur Biz' Settings plugin for Sylius.
*
* (c) Monsieur Biz <[email protected]>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace App\Form;

use MonsieurBiz\SyliusSettingsPlugin\Form\AbstractSettingsExtension;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Validator\Constraints as Assert;

class SettingsExtensionType extends AbstractSettingsExtension
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
if ($this->isDefaultForm($builder)) {
$this->addWithDefaultCheckbox(
$builder,
'demo_title',
TextType::class,
[
'required' => true,
'constraints' => [
new Assert\NotBlank(),
],
]
);
} else {
$this->addWithDefaultCheckbox(
$builder,
'demo_title',
TextType::class,
[
'required' => false,
]
);
}
}

public static function getExtendedTypes(): array
{
return [
SettingsType::class,
];
}
}
1 change: 1 addition & 0 deletions dist/templates/views/message.html.twig
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<p>
{% if setting('app.default', 'enabled') %}
{{ setting('app.default', 'demo_title') }}
{{ setting('app.default', 'demo_message') }}
{% else %}
{{ 'Message is disabled'|trans }}
Expand Down
42 changes: 42 additions & 0 deletions src/Form/AbstractSettingsExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/*
* This file is part of Monsieur Biz' Settings plugin for Sylius.
*
* (c) Monsieur Biz <[email protected]>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace MonsieurBiz\SyliusSettingsPlugin\Form;

use MonsieurBiz\SyliusSettingsPlugin\Settings\Settings;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\FormBuilderInterface;

abstract class AbstractSettingsExtension extends AbstractTypeExtension implements SettingsExtensionInterface
{
public function addWithDefaultCheckbox(FormBuilderInterface $builder, string $child, string $type = null, array $options = []): self
{
$data = (array) $builder->getData();
$builder->add($child, $type, $options);
if (!$this->isDefaultForm($builder)) {
$builder->add($child . '___' . Settings::DEFAULT_KEY, DefaultCheckboxType::class, [
'label' => 'monsieurbiz.settings.ui.use_default_value',
'related_form_child' => $builder->get($child),
'data' => !\array_key_exists($child, $data),
'required' => true,
]);
}

return $this;
}

protected function isDefaultForm(FormBuilderInterface $builder): bool
{
return !$builder->getOption('show_default_checkboxes', true);
}
}
27 changes: 27 additions & 0 deletions src/Form/SettingsExtensionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/*
* This file is part of Monsieur Biz' Settings plugin for Sylius.
*
* (c) Monsieur Biz <[email protected]>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace MonsieurBiz\SyliusSettingsPlugin\Form;

use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormTypeExtensionInterface;

interface SettingsExtensionInterface extends FormTypeExtensionInterface
{
/**
* This method should add a checkbox field to the form when adding a new setting's field.
*
* @return $this
*/
public function addWithDefaultCheckbox(FormBuilderInterface $builder, string $child, string $type = null, array $options = []): self;
}

0 comments on commit 556a975

Please sign in to comment.