murph-doc/docs/settings/global.md

139 lines
3.5 KiB
Markdown
Raw Permalink Normal View History

2021-05-30 23:00:08 +02:00
# Global settings
## Create settings
The creation of settings is based on events.
Using an event subscriber, you can create settings and define how to edit them.
A setting's value is stored in json so a value could be a string, a boolean, an array, etc.
See the example below.
2023-02-09 21:50:06 +01:00
```php-inline title="src/EventSubscriber/SettingEventSubscriber.php"
2022-09-19 22:19:13 +02:00
namespace App\EventSubscriber;
2021-05-30 23:00:08 +02:00
use App\Core\Event\Setting\SettingEvent;
2022-12-02 19:32:08 +01:00
use App\Core\EventSubscriber\SettingEventSubscriber as EventSubscriber;
2021-05-30 23:00:08 +02:00
use App\Core\Setting\SettingManager;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\ColorType;
class SettingEventSubscriber extends EventSubscriber
{
protected SettingManager $manager;
public function __construct(SettingManager $manager)
{
$this->manager = $manager;
}
public function onInit(SettingEvent $event)
{
2021-06-04 10:48:41 +02:00
$this->manager->init('app_font_color', 'Design', 'Font color', , '#fff');
$this->manager->init('app_background_color', 'Design', 'Background color', '#333');
$this->manager->init('app_maintenance_enabled', 'System', 'Maintenance', false);
2021-05-30 23:00:08 +02:00
}
public function onFormInit(SettingEvent $event)
{
$data = $event->getData();
$builder = $data['builder'];
$entity = $data['entity'];
if (in_array($entity->getCode(), ['app_font_color', 'app_background_color'])) {
$builder->add(
'value',
ColorType::class,
[
'label' => $entity->getLabel(),
]
);
}
if (in_array($entity->getCode(), ['app_maintenance_enabled'])) {
$builder->add(
'value',
CheckboxType::class,
[
'label' => $entity->getLabel(),
]
);
}
}
}
```
2021-05-31 16:30:46 +02:00
Result:
![](/_static/img/settings/02.png)
2021-05-30 23:00:08 +02:00
## Access settings
Settings are accessible using `App\Core\Setting\SettingManager` which is a service.
2023-02-09 21:50:06 +01:00
```php-inline title="src/Controller/FooController.php"
2021-05-30 23:00:08 +02:00
namespace App\Controller;
use App\Core\Setting\SettingManager;
use Symfony\Component\HttpFoundation\Response;
class FooController
{
public function foo(SettingManager $settingManager): Response
{
$fontColor = $settingManager->get('app_font_color');
$backgroundColor = $settingManager->get('app_background_color');
$maintenanceEnabled = $settingManager->get('app_maintenance_enabled');
// ...
}
}
```
In a template, you can use the function `setting`:
2023-02-09 21:50:06 +01:00
```twig
2021-05-30 23:00:08 +02:00
Font color: {{ setting('app_font_color') }}<br>
Background color: {{ setting('app_background_color') }}<br>
Maintenance enabled: {{ setting('app_maintenance_enabled') ? 'Yes' : 'No' }}<br>
```
## Update settings
Settings are accessible using `App\Core\Setting\SettingManager` which is a service.
2023-02-09 21:50:06 +01:00
```php-inline title="src/Controller/FooController.php"
2021-05-30 23:00:08 +02:00
namespace App\Controller;
use App\Core\Setting\SettingManager;
use Symfony\Component\HttpFoundation\Response;
class FooController
{
public function foo(SettingManager $settingManager): Response
{
$settingManager->set('app_font_color', '#f00');
$settingManager->set('app_background_color', '#00f');
$settingManager->set('app_maintenance_enabled', true);
// ...
}
}
```
You can also edit them from UI:
![](/_static/img/settings/01.png)
2021-10-20 22:22:12 +02:00
## Options
You can add options using this way:
2023-02-09 21:50:06 +01:00
```php-inline
2021-10-20 22:22:12 +02:00
$event->setOption('view', 'large');
```
Available options:
* `view` (default: `false`): show a large modal