127 lines
3.5 KiB
Markdown
127 lines
3.5 KiB
Markdown
# Navigation 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.
|
|
|
|
```php-inline title="src/EventSubscriber/NavigationSettingEventSubscriber.php"
|
|
namespace App\EventSubscriber;
|
|
|
|
use App\Core\Event\Setting\NavigationSettingEvent;
|
|
use App\Core\EventSubscriber\NavigationSettingEventSubscriber as EventSubscriber;
|
|
use App\Core\Setting\NavigationSettingManager;
|
|
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
|
use Symfony\Component\Form\Extension\Core\Type\EmailType;
|
|
|
|
class NavigationSettingEventSubscriber extends EventSubscriber
|
|
{
|
|
protected NavigationSettingManager $manager;
|
|
|
|
public function __construct(NavigationSettingManager $manager)
|
|
{
|
|
$this->manager = $manager;
|
|
}
|
|
|
|
public function onInit(NavigationSettingEvent $event)
|
|
{
|
|
$data = $event->getData();
|
|
$navigation = $data['navigation'];
|
|
|
|
$this->manager->init($navigation, 'nav_tracker_code', 'Stats', 'Tracker', '');
|
|
$this->manager->init($navigation, 'nav_contact_email', 'Contact', 'Email', 'foo@example.com');
|
|
}
|
|
|
|
public function onFormInit(NavigationSettingEvent $event)
|
|
{
|
|
$data = $event->getData();
|
|
$builder = $data['builder'];
|
|
$entity = $data['entity'];
|
|
|
|
if (in_array($entity->getCode(), ['nav_tracker_code'])) {
|
|
$builder->add(
|
|
'value',
|
|
TextType::class,
|
|
[
|
|
'label' => $entity->getLabel(),
|
|
]
|
|
);
|
|
}
|
|
|
|
if (in_array($entity->getCode(), ['nav_contact_email'])) {
|
|
$builder->add(
|
|
'value',
|
|
EmailType::class,
|
|
[
|
|
'label' => $entity->getLabel(),
|
|
]
|
|
);
|
|
}
|
|
}
|
|
}
|
|
```
|
|
Result:
|
|
|
|
![](/_static/img/settings/03.png)
|
|
|
|
## Access settings
|
|
|
|
Settings are accessible using `App\Core\Setting\NavigationSettingManager` which is a service.
|
|
|
|
```php-inline title="src/Controller/FooController.php"
|
|
namespace App\Controller;
|
|
|
|
use App\Core\Setting\NavigationSettingManager;
|
|
use App\Core\Site\SiteRequest;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
class FooController
|
|
{
|
|
public function foo(NavigationSettingManager $settingManager, SiteRequest $siteRequest): Response
|
|
{
|
|
$trackerCode = $settingManager->get($siteRequest->getNavigation(), 'nav_tracker_code');
|
|
$contactEmail = $settingManager->get('my_nav', 'nav_contact_email');
|
|
|
|
// ...
|
|
}
|
|
}
|
|
```
|
|
|
|
In a template, you can use the function `navigation_setting`:
|
|
|
|
```twig
|
|
Tracker code: {{ navigation_setting(_navigation, 'nav_tracker_code') }}<br>
|
|
Contact email: {{ navigation_setting('my_nav', 'nav_contact_email') }}<br>
|
|
```
|
|
|
|
## Update settings
|
|
|
|
Settings are accessible using `App\Core\Setting\NavigationSettingManager` which is a service.
|
|
|
|
```php-inline title="src/Controller/FooController.php"
|
|
namespace App\Controller;
|
|
|
|
use App\Core\Setting\NavigationSettingManager;
|
|
use App\Core\Site\SiteRequest;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
class FooController
|
|
{
|
|
public function foo(NavigationSettingManager $settingManager, SiteRequest $siteRequest): Response
|
|
{
|
|
$settingManager->set($siteRequest->getNavigation(), 'nav_tracker_code', '...');
|
|
$settingManager->set('my_nav', 'nav_contact_email', '...');
|
|
|
|
// ...
|
|
}
|
|
}
|
|
```
|
|
|
|
You can also edit them from UI:
|
|
|
|
![](/_static/img/settings/04.png)
|