You are here

class HighContrastConfigOverride in High contrast 8

Configuration override class for high contrast.

Overrides the site logo if high contrast is enabled.

Hierarchy

Expanded class hierarchy of HighContrastConfigOverride

2 string references to 'HighContrastConfigOverride'
HighContrastConfigOverride::getCacheSuffix in src/HighContrastConfigOverride.php
The string to append to the configuration static cache name.
high_contrast.services.yml in ./high_contrast.services.yml
high_contrast.services.yml
1 service uses HighContrastConfigOverride
high_contrast.overrider in ./high_contrast.services.yml
\Drupal\high_contrast\HighContrastConfigOverride

File

src/HighContrastConfigOverride.php, line 17

Namespace

Drupal\high_contrast
View source
class HighContrastConfigOverride implements ConfigFactoryOverrideInterface {

  /**
   * Config factory interface.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  private $configFactory;

  /**
   * The file system interface.
   *
   * @var \Drupal\Core\File\FileSystemInterface
   */
  private $fileSystem;

  /**
   * Construct a new HighContrastConfigOverride object.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   * @param \Drupal\Core\File\FileSystemInterface $file_system
   *   The file system.
   */
  public function __construct(ConfigFactoryInterface $config_factory, FileSystemInterface $file_system) {
    $this->configFactory = $config_factory;
    $this->fileSystem = $file_system;
  }

  /**
   * {@inheritdoc}
   *
   * @todo Override the right logo. See https://www.drupal.org/node/2866194
   */
  public function loadOverrides($names) {
    $overrides = [];
    if (in_array('system.theme.global', $names) && HighContrastTrait::highContrastEnabled() && ($logo = $this
      ->getHighContrastLogo())) {
      $overrides['system.theme.global']['logo']['path'] = $logo;
      $overrides['system.theme.global']['logo']['url'] = '';
      $overrides['system.theme.global']['logo']['use_default'] = FALSE;
    }
    return $overrides;
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheSuffix() {
    return 'HighContrastConfigOverride';
  }

  /**
   * {@inheritdoc}
   *
   * @todo Check the right $name. See https://www.drupal.org/node/2866194
   */
  public function getCacheableMetadata($name) {
    $metadata = new CacheableMetadata();
    if ($name === 'system.theme.global') {
      $config = $this->configFactory
        ->get('high_contrast.settings');

      // Cache depends on enabled state and configuration.
      $metadata
        ->addCacheContexts([
        'high_contrast',
      ]);
      $metadata
        ->addCacheableDependency($config);
    }
    return $metadata;
  }

  /**
   * {@inheritdoc}
   */
  public function createConfigObject($name, $collection = StorageInterface::DEFAULT_COLLECTION) {
    return NULL;
  }

  /**
   * Returns the configured logo, either from theme dir of configured path.
   */
  private function getHighContrastLogo() {
    $logo = NULL;
    $config = $this->configFactory
      ->get('high_contrast.settings');
    if ($config
      ->get('default_logo')) {

      // If the default logo is desired, scan the theme dir for a logo-hg file.
      // Not using dependency injection to prevent circular references.
      $theme = \Drupal::theme()
        ->getActiveTheme()
        ->getName();
      $theme_path = drupal_get_path('theme', $theme);
      $candidates = [];
      try {
        if (is_dir($theme_path)) {
          $candidates = $this->fileSystem
            ->scanDirectory($theme_path, "/logo_hg\\.(svg|png|jpg|gif)\$/");
        }
        if (!empty($candidates)) {
          $logo = reset($candidates)->uri;
        }
      } catch (FileException $e) {

        // Ignore and return empty array for BC.
      }
    }
    elseif ($config
      ->get('logo_path')) {

      // No default logo, return the custom logo instead.
      $logo = $config
        ->get('logo_path');
    }
    return $logo;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
HighContrastConfigOverride::$configFactory private property Config factory interface.
HighContrastConfigOverride::$fileSystem private property The file system interface.
HighContrastConfigOverride::createConfigObject public function Creates a configuration object for use during install and synchronization. Overrides ConfigFactoryOverrideInterface::createConfigObject
HighContrastConfigOverride::getCacheableMetadata public function @todo Check the right $name. See https://www.drupal.org/node/2866194 Overrides ConfigFactoryOverrideInterface::getCacheableMetadata
HighContrastConfigOverride::getCacheSuffix public function The string to append to the configuration static cache name. Overrides ConfigFactoryOverrideInterface::getCacheSuffix
HighContrastConfigOverride::getHighContrastLogo private function Returns the configured logo, either from theme dir of configured path.
HighContrastConfigOverride::loadOverrides public function @todo Override the right logo. See https://www.drupal.org/node/2866194 Overrides ConfigFactoryOverrideInterface::loadOverrides
HighContrastConfigOverride::__construct public function Construct a new HighContrastConfigOverride object.