View source
<?php
namespace Drupal\social_event_addtocal\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\social_event_addtocal\Plugin\SocialAddToCalendarManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
class SocialAddToCalendarSettingsForm extends ConfigFormBase {
protected $addToCalendarManager;
public function __construct(ConfigFactoryInterface $config_factory, SocialAddToCalendarManager $add_to_calendar_manager) {
parent::__construct($config_factory);
$this->addToCalendarManager = $add_to_calendar_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('plugin.manager.social_add_to_calendar'));
}
protected function getEditableConfigNames() {
return [
'social_event_addtocal.settings',
];
}
public function getFormId() {
return 'social_add_to_calendar_settings_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config('social_event_addtocal.settings');
$form['enable_add_to_calendar'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Enable the <em>Add to calendar</em> button'),
'#description' => $this
->t('If enabled, logged-in users are allowed to add the event to own calendar'),
'#default_value' => $config
->get('enable_add_to_calendar'),
];
$addtocal_options = [];
$addtocal_definitions = $this->addToCalendarManager
->getDefinitions();
foreach ($addtocal_definitions as $addtocal_definition) {
$addtocal_options[$addtocal_definition['id']] = $addtocal_definition['label'];
}
$form['allowed_calendars'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Allowed calendars'),
'#description' => $this
->t('Enable calendars you want to allow users to use'),
'#options' => $addtocal_options,
'#states' => [
'visible' => [
':input[name="enable_add_to_calendar"]' => [
'checked' => TRUE,
],
],
],
'#default_value' => $config
->get('allowed_calendars') ?: [],
];
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$this
->config('social_event_addtocal.settings')
->set('enable_add_to_calendar', $form_state
->getValue('enable_add_to_calendar'))
->set('allowed_calendars', $form_state
->getValue('allowed_calendars'))
->save();
}
}