You are here

class HighContrastConfigurationForm in High contrast 8

Class HighContrastConfigurationForm.

This class provides the site-wide configuration for, for high contrast.

Hierarchy

Expanded class hierarchy of HighContrastConfigurationForm

1 string reference to 'HighContrastConfigurationForm'
high_contrast.routing.yml in ./high_contrast.routing.yml
high_contrast.routing.yml

File

src/Form/HighContrastConfigurationForm.php, line 18

Namespace

Drupal\high_contrast\Form
View 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

Namesort descending Modifiers Type Description Overrides
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
HighContrastConfigurationForm::$fileSystem protected property The file system service.
HighContrastConfigurationForm::$moduleHandler protected property The module handler.
HighContrastConfigurationForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
HighContrastConfigurationForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
HighContrastConfigurationForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
HighContrastConfigurationForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
HighContrastConfigurationForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
HighContrastConfigurationForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
HighContrastConfigurationForm::validatePath protected function Helper function for the HighContrastConfigurationForm form.
HighContrastConfigurationForm::__construct public function Constructs a HighContrastConfigurationForm object. Overrides ConfigFormBase::__construct
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.