View source
<?php
namespace Drupal\content_kanban\Form;
use Drupal\content_kanban\KanbanService;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\ConfigFormBase;
use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class SettingsForm extends ConfigFormBase {
const CONFIG_NAME = 'content_kanban.settings';
const DEFAULT_DATE_RANGE_VALUE = 30;
protected $config;
protected $kanbanService;
public function __construct(KanbanService $kanbanService, ConfigFactoryInterface $config_factory) {
parent::__construct($config_factory);
$this->kanbanService = $kanbanService;
$this->config = $this
->config(self::CONFIG_NAME);
}
public static function create(ContainerInterface $container) {
return new static($container
->get('content_kanban.kanban_service'), $container
->get('config.factory'));
}
public function getFormId() {
return 'content_kanban_settings';
}
protected function getEditableConfigNames() {
return [
'content_kanban.settings',
];
}
public function buildForm(array $form, FormStateInterface $form_state, Request $request = NULL) {
if (!$this->kanbanService
->contentCalendarModuleIsEnabled()) {
$this
->saveColorSetting(0);
}
$config = $this
->config(self::CONFIG_NAME);
$form['advice'] = [
'#title' => 'Disclaimer',
'#type' => 'fieldset',
'#weight' => 78,
'#description' => $this
->t('Ensure all workflow state are set as a Default Revision in order to allow kanban table to work properly'),
];
$form['options'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Options'),
'#collapsible' => FALSE,
'#collapsed' => FALSE,
];
$form['options']['use_content_calendar_colors'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Use the defined colors from Content Calendar'),
'#description' => $this
->t('This setting is only available if the Content Calendar is enabled and configured properly.'),
'#default_value' => $config
->get('use_content_calendar_colors'),
'#disabled' => !$this->kanbanService
->contentCalendarModuleIsEnabled(),
];
$user_picture_field_exists = !$this
->config('field.field.user.user.user_picture')
->isNew();
$form['options']['show_user_thumb'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Show thumbnail image of User image'),
'#description' => $this
->t('This option is only available, if the User account has the "user_picture" field. See Account configuration.'),
'#disabled' => !$user_picture_field_exists,
'#default_value' => $this->config
->get('show_user_thumb'),
];
$default_date_range_value = self::DEFAULT_DATE_RANGE_VALUE;
if ($this->config
->get('default_filter_date_range')) {
$default_date_range_value = $this->config
->get('default_filter_date_range');
}
$form['options']['default_filter_date_range'] = [
'#type' => 'select',
'#title' => $this
->t('Date range'),
'#options' => \Drupal\content_kanban\Form\KanbanFilterForm::getDateRangeOptions(),
'#required' => FALSE,
'#empty_value' => '',
'#empty_option' => $this
->t('All'),
'#default_value' => $default_date_range_value,
];
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$values = $form_state
->getValues();
$use_content_calendar_colors = $values['use_content_calendar_colors'];
if (!$this->kanbanService
->contentCalendarModuleIsEnabled()) {
$use_content_calendar_colors = 0;
}
$this
->saveColorSetting($use_content_calendar_colors);
$this
->config(self::CONFIG_NAME)
->set('show_user_thumb', $values['show_user_thumb'])
->set('default_filter_date_range', $values['default_filter_date_range'])
->save();
}
protected function saveColorSetting($value) {
$this
->config(self::CONFIG_NAME)
->set('use_content_calendar_colors', $value)
->save();
}
}