SettingsForm.php in FullCalendar 8
File
fullcalendar_options/src/Form/SettingsForm.php
View source
<?php
namespace Drupal\fullcalendar_options\Form;
use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class SettingsForm extends ConfigFormBase {
protected $options;
public function __construct(ConfigFactoryInterface $config_factory, PluginManagerInterface $manager) {
parent::__construct($config_factory);
$instance = $manager
->createInstance('fullcalendar_options');
$this->options = $instance
->optionsList();
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('plugin.manager.fullcalendar'));
}
public function getFormID() {
return 'fullcalendar_options_admin_settings';
}
protected function getEditableConfigNames() {
return [
'fullcalendar_options.settings',
];
}
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config('fullcalendar_options.settings');
$form['fullcalendar_options'] = [
'#type' => 'details',
'#title' => $this
->t('Options'),
'#description' => $this
->t('Each setting can be exposed for all views.'),
'#open' => TRUE,
];
foreach ($this->options as $key => $info) {
$form['fullcalendar_options'][$key] = [
'#type' => 'checkbox',
'#default_value' => $config
->get($key),
] + $info;
}
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = $this
->config('fullcalendar_options.settings');
foreach ($this->options as $key => $info) {
$config
->set($key, $form_state
->getValue($key));
}
$config
->save();
parent::submitForm($form, $form_state);
}
}