View source
<?php
namespace Drupal\high_contrast\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StreamWrapper\StreamWrapperManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
class HighContrastConfigurationForm extends ConfigFormBase {
protected $moduleHandler;
protected $fileSystem;
public function __construct(ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler, FileSystemInterface $file_system) {
parent::__construct($config_factory);
$this->moduleHandler = $module_handler;
$this->fileSystem = $file_system;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('module_handler'), $container
->get('file_system'));
}
public function getFormId() {
return 'high_contrast_configuration';
}
protected function getEditableConfigNames() {
return [
'high_contrast.settings',
];
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
$config = $this
->config('high_contrast.settings');
$form['colors'] = [
'#type' => 'details',
'#title' => $this
->t('High contrast colors'),
'#open' => TRUE,
'#tree' => FALSE,
];
$form['colors']['colors_background'] = [
'#type' => 'color',
'#title' => $this
->t('Background'),
'#default_value' => $config
->get('colors_background'),
];
$form['colors']['colors_text'] = [
'#type' => 'color',
'#title' => $this
->t('Text'),
'#default_value' => $config
->get('colors_text'),
];
$form['colors']['colors_hyperlinks'] = [
'#type' => 'color',
'#title' => $this
->t('Hyperlinks'),
'#default_value' => $config
->get('colors_hyperlinks'),
];
if ($this->moduleHandler
->moduleExists('file')) {
$form['logo'] = [
'#type' => 'fieldset',
'#title' => $this
->t('High contrast logo image settings'),
];
$form['logo']['default_logo'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Use the default logo (file named logo-hg in your theme folder if it exists)'),
'#default_value' => $config
->get('default_logo'),
'#tree' => FALSE,
'#description' => $this
->t('Check here if you want the theme to use the logo supplied with it.'),
];
$form['logo']['settings'] = [
'#type' => 'container',
'#states' => [
'invisible' => [
'input[name="default_logo"]' => [
'checked' => TRUE,
],
],
],
];
$form['logo']['settings']['logo_path'] = [
'#type' => 'textfield',
'#title' => $this
->t('Path to custom high contrast logo'),
'#description' => $this
->t('The path to the file you would like to use as your logo file instead of the default logo.'),
'#default_value' => $config
->get('logo_path'),
];
$form['logo']['settings']['logo_upload'] = [
'#type' => 'file',
'#title' => $this
->t('Upload high contrast logo image'),
'#maxlength' => 40,
'#description' => $this
->t("If you don't have direct file access to the server, use this field to upload your logo."),
];
}
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
if ($this->moduleHandler
->moduleExists('file')) {
$validators = [
'file_validate_is_image' => [],
];
$file = file_save_upload('logo_upload', $validators, FALSE, 0);
if (isset($file)) {
if ($file) {
$form_state
->setValue('logo_upload', $file);
}
else {
$form_state
->setErrorByName('logo_upload', $this
->t('The logo could not be uploaded.'));
}
}
if ($form_state
->getValue('default_logo')) {
$form_state
->unsetValue('logo_path');
}
if ($form_state
->getValue('logo_path')) {
$path = $this
->validatePath($form_state
->getValue('logo_path'));
if (!$path) {
$form_state
->setErrorByName('logo_path', $this
->t('The custom logo path is invalid.'));
}
}
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$config = $this
->config('high_contrast.settings');
$config
->set('colors_background', $form_state
->getValue('colors_background'));
$config
->set('colors_text', $form_state
->getValue('colors_text'));
$config
->set('colors_hyperlinks', $form_state
->getValue('colors_hyperlinks'));
$config
->set('default_logo', $form_state
->getValue('default_logo'));
$config
->set('logo_path', $form_state
->getValue('logo_path'));
if (!empty($form_state
->getValue('logo_upload'))) {
$source = $form_state
->getValue('logo_upload')
->getFileUri();
$destination = file_build_uri($this->fileSystem
->basename($source));
$filename = $this->fileSystem
->copy($source, $destination);
$config
->set('default_logo', 0);
$config
->set('logo_path', $filename);
}
$config
->save();
}
protected function validatePath($path) {
if ($this->fileSystem
->realpath($path) == $path) {
return FALSE;
}
if (is_file($path)) {
return $path;
}
if (StreamWrapperManager::getScheme($path) === FALSE) {
$path = 'public://' . $path;
}
if (is_file($path)) {
return $path;
}
return FALSE;
}
}