class HighContrastConfigurationForm in High contrast 8
Class HighContrastConfigurationForm.
This class provides the site-wide configuration for, for high contrast.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
- class \Drupal\high_contrast\Form\HighContrastConfigurationForm
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
Expanded class hierarchy of HighContrastConfigurationForm
1 string reference to 'HighContrastConfigurationForm'
File
- src/
Form/ HighContrastConfigurationForm.php, line 18
Namespace
Drupal\high_contrast\FormView source
class HighContrastConfigurationForm extends ConfigFormBase {
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* The file system service.
*
* @var \Drupal\Core\File\FileSystemInterface
*/
protected $fileSystem;
/**
* Constructs a HighContrastConfigurationForm object.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The factory for configuration objects.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler instance to use.
* @param \Drupal\Core\File\FileSystemInterface $file_system
* The file system service.
*/
public function __construct(ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler, FileSystemInterface $file_system) {
parent::__construct($config_factory);
$this->moduleHandler = $module_handler;
$this->fileSystem = $file_system;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('module_handler'), $container
->get('file_system'));
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'high_contrast_configuration';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
'high_contrast.settings',
];
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
// Form constructor.
$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' => [
// Hide the logo settings when using the default logo.
'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;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
if ($this->moduleHandler
->moduleExists('file')) {
// Handle file uploads.
$validators = [
'file_validate_is_image' => [],
];
// Check for a new uploaded logo.
$file = file_save_upload('logo_upload', $validators, FALSE, 0);
if (isset($file)) {
// File upload was attempted.
if ($file) {
// Put the temporary file in form_values so we can save it on submit.
$form_state
->setValue('logo_upload', $file);
}
else {
// File upload failed.
$form_state
->setErrorByName('logo_upload', $this
->t('The logo could not be uploaded.'));
}
}
// When intending to use the default logo, unset the logo_path.
if ($form_state
->getValue('default_logo')) {
$form_state
->unsetValue('logo_path');
}
// If the user provided a path for a logo, make sure a file exists at that
// 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.'));
}
}
}
}
/**
* {@inheritdoc}
*/
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 the user uploaded a new logo, save it to a permanent location and use
// it in place of the default theme-provided file.
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();
}
/**
* Helper function for the HighContrastConfigurationForm form.
*
* Attempts to validate normal system paths, paths relative to the public
* files directory, or stream wrapper URIs. If the given path is any of the
* above, returns a valid path or URI that the theme system can display.
*
* @param string $path
* A path relative to the Drupal root or to the public files directory, or
* a stream wrapper URI.
*
* @return mixed
* A valid path that can be displayed through the theme system, or FALSE if
* the path could not be validated.
*/
protected function validatePath($path) {
// Absolute local file paths are invalid.
if ($this->fileSystem
->realpath($path) == $path) {
return FALSE;
}
// A path relative to the Drupal root or a fully qualified URI is valid.
if (is_file($path)) {
return $path;
}
// Prepend 'public://' for relative file paths within public filesystem.
if (StreamWrapperManager::getScheme($path) === FALSE) {
$path = 'public://' . $path;
}
if (is_file($path)) {
return $path;
}
return FALSE;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConfigFormBaseTrait:: |
protected | function | Retrieves a configuration object. | |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
HighContrastConfigurationForm:: |
protected | property | The file system service. | |
HighContrastConfigurationForm:: |
protected | property | The module handler. | |
HighContrastConfigurationForm:: |
public | function |
Form constructor. Overrides ConfigFormBase:: |
|
HighContrastConfigurationForm:: |
public static | function |
Instantiates a new instance of this class. Overrides ConfigFormBase:: |
|
HighContrastConfigurationForm:: |
protected | function |
Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait:: |
|
HighContrastConfigurationForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
HighContrastConfigurationForm:: |
public | function |
Form submission handler. Overrides ConfigFormBase:: |
|
HighContrastConfigurationForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
HighContrastConfigurationForm:: |
protected | function | Helper function for the HighContrastConfigurationForm form. | |
HighContrastConfigurationForm:: |
public | function |
Constructs a HighContrastConfigurationForm object. Overrides ConfigFormBase:: |
|
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |