All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
3.5 KiB
3.5 KiB
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.
// src/EventSubscriber/NavigationSettingEventSubscriber.php
namespace App\EventSubscriber;
use App\Core\Event\Setting\NavigationSettingEvent;
use App\Core\EventSuscriber\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:
Access settings
Settings are accessible using App\Core\Setting\NavigationSettingManager which is a service.
// 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:
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.
// 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:

