-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #66 from maximehuran/feature/form-extension-types
Be able to extends settings form
- Loading branch information
Showing
6 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |