You are here

class ConfigFactory in Domain Access 8

Extends core ConfigFactory class to save domain specific configuration.

Hierarchy

Expanded class hierarchy of ConfigFactory

1 string reference to 'ConfigFactory'
domain_config_ui.services.yml in domain_config_ui/domain_config_ui.services.yml
domain_config_ui/domain_config_ui.services.yml
1 service uses ConfigFactory
domain_config_ui.factory in domain_config_ui/domain_config_ui.services.yml
Drupal\domain_config_ui\Config\ConfigFactory

File

domain_config_ui/src/Config/ConfigFactory.php, line 14

Namespace

Drupal\domain_config_ui\Config
View source
class ConfigFactory extends CoreConfigFactory {

  /**
   * The Domain config UI manager.
   *
   * @var Drupal\domain_config_ui\DomainConfigUIManager
   */
  protected $domainConfigUIManager;

  /**
   * Constructs the Config factory.
   *
   * @param \Drupal\Core\Config\StorageInterface $storage
   *   The configuration storage engine.
   * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
   *   An event dispatcher instance to use for configuration events.
   * @param \Drupal\Core\Config\TypedConfigManagerInterface $typed_config
   *   The typed configuration manager.
   * @param \Drupal\domain_config_ui\DomainConfigUIManager $domain_config_ui_manager
   *   The domain config UI manager.
   */
  public function __construct(StorageInterface $storage, EventDispatcherInterface $event_dispatcher, TypedConfigManagerInterface $typed_config, DomainConfigUIManager $domain_config_ui_manager) {
    parent::__construct($storage, $event_dispatcher, $typed_config);
    $this->domainConfigUIManager = $domain_config_ui_manager;
  }

  /**
   * {@inheritdoc}
   */
  protected function createConfigObject($name, $immutable) {
    if (!$immutable) {
      $config = new Config($name, $this->storage, $this->eventDispatcher, $this->typedConfigManager);

      // Pass the UI manager to the Config object.
      $config
        ->setDomainConfigUiManager($this->domainConfigUIManager);
      return $config;
    }
    return parent::createConfigObject($name, $immutable);
  }

  /**
   * Set the Domain config UI manager.
   *
   * @param \Drupal\domain_config_ui\DomainConfigUIManager $domain_config_ui_manager
   *   The Domain config UI manager.
   */
  public function setDomainConfigUiManager(DomainConfigUIManager $domain_config_ui_manager) {
    $this->domainConfigUIManager = $domain_config_ui_manager;
  }

  /**
   * {@inheritdoc}
   */
  protected function doLoadMultiple(array $names, $immutable = TRUE) {

    // Let parent load multiple load as usual.
    $list = parent::doLoadMultiple($names, $immutable);

    // Do not override if configuring 'all' domains or config is immutable.
    // @TODO: This will need to change if we allow saving for 'all allowed domains'
    if (empty($this->domainConfigUIManager) || !$this->domainConfigUIManager
      ->getSelectedDomainId()) {
      return $list;
    }

    // Pre-load remaining configuration files.
    if (!empty($names)) {

      // Initialise override information.
      $module_overrides = [];
      $storage_data = $this->storage
        ->readMultiple($names);

      // Load module overrides so that domain config is loaded in admin forms.
      if (!empty($storage_data)) {

        // Only get domain overrides if we have configuration to override.
        $module_overrides = $this
          ->loadDomainOverrides($names);
      }
      foreach ($storage_data as $name => $data) {
        $cache_key = $this
          ->getConfigCacheKey($name, $immutable);
        if (!empty($module_overrides[$name])) {
          $this->cache[$cache_key]
            ->setModuleOverride($module_overrides[$name]);
          $list[$name] = $this->cache[$cache_key];
          $this
            ->propagateConfigOverrideCacheability($cache_key, $name);
        }
      }
    }
    return $list;
  }

  /**
   * {@inheritdoc}
   */
  protected function doGet($name, $immutable = TRUE) {

    // If config for 'all' domains or immutable then don't override config.
    if (empty($this->domainConfigUIManager) || !$this->domainConfigUIManager
      ->getSelectedDomainId()) {
      return parent::doGet($name, $immutable);
    }
    if ($config = $this
      ->doLoadMultiple([
      $name,
    ], $immutable)) {
      return $config[$name];
    }
    else {

      // If the configuration object does not exist in the configuration
      // storage, create a new object.
      $config = $this
        ->createConfigObject($name, $immutable);

      // Load domain overrides so domain config is loaded in admin forms.
      $overrides = $this
        ->loadDomainOverrides([
        $name,
      ]);
      if (isset($overrides[$name])) {
        $config
          ->setModuleOverride($overrides[$name]);
      }
      foreach ($this->configFactoryOverrides as $override) {
        $config
          ->addCacheableDependency($override
          ->getCacheableMetadata($name));
      }
      return $config;
    }
  }

  /**
   * Get Domain module overrides for the named configuration objects.
   *
   * @param array $names
   *   The names of the configuration objects to get overrides for.
   *
   * @return array
   *   An array of overrides keyed by the configuration object name.
   */
  protected function loadDomainOverrides(array $names) {
    $overrides = [];
    foreach ($names as $name) {

      // Try to load the language-specific domain override.
      $config_name = $this->domainConfigUIManager
        ->getSelectedConfigName($name);
      if ($override = $this->storage
        ->read($config_name)) {
        $overrides[$name] = $override;
      }
      elseif ($this->domainConfigUIManager
        ->getSelectedLanguageId()) {
        $omit_language = TRUE;
        $config_name = $this->domainConfigUIManager
          ->getSelectedConfigName($name, $omit_language);
        if ($override = $this->storage
          ->read($config_name)) {
          $overrides[$name] = $override;
        }
      }
    }
    return $overrides;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFactory::$cache protected property Cached configuration objects.
ConfigFactory::$configFactoryOverrides protected property An array of config factory override objects ordered by priority.
ConfigFactory::$domainConfigUIManager protected property The Domain config UI manager.
ConfigFactory::$eventDispatcher protected property An event dispatcher instance to use for configuration events.
ConfigFactory::$storage protected property A storage instance for reading and writing configuration data.
ConfigFactory::$typedConfigManager protected property The typed config manager.
ConfigFactory::addOverride public function Adds config factory override services. Overrides ConfigFactoryInterface::addOverride
ConfigFactory::clearStaticCache public function Clears the config factory static cache. Overrides ConfigFactoryInterface::clearStaticCache
ConfigFactory::createConfigObject protected function Creates a configuration object. Overrides ConfigFactory::createConfigObject
ConfigFactory::doGet protected function Returns a configuration object for a given name. Overrides ConfigFactory::doGet
ConfigFactory::doLoadMultiple protected function Returns a list of configuration objects for the given names. Overrides ConfigFactory::doLoadMultiple
ConfigFactory::get public function Returns an immutable configuration object for a given name. Overrides ConfigFactoryInterface::get
ConfigFactory::getCacheKeys public function The cache keys associated with the state of the config factory. Overrides ConfigFactoryInterface::getCacheKeys
ConfigFactory::getConfigCacheKey protected function Gets the static cache key for a given config name.
ConfigFactory::getConfigCacheKeys protected function Gets all the cache keys that match the provided config name.
ConfigFactory::getEditable public function Returns an mutable configuration object for a given name. Overrides ConfigFactoryInterface::getEditable
ConfigFactory::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
ConfigFactory::listAll public function Gets configuration object names starting with a given prefix. Overrides ConfigFactoryInterface::listAll
ConfigFactory::loadDomainOverrides protected function Get Domain module overrides for the named configuration objects.
ConfigFactory::loadMultiple public function Returns a list of configuration objects for the given names. Overrides ConfigFactoryInterface::loadMultiple
ConfigFactory::loadOverrides protected function Get arbitrary overrides for the named configuration objects from modules.
ConfigFactory::onConfigDelete public function Removes stale static cache entries when configuration is deleted.
ConfigFactory::onConfigSave public function Updates stale static cache entries when configuration is saved.
ConfigFactory::propagateConfigOverrideCacheability protected function Propagates cacheability of config overrides to cached config objects.
ConfigFactory::rename public function Renames a configuration object using the storage. Overrides ConfigFactoryInterface::rename
ConfigFactory::reset public function Resets and re-initializes configuration objects. Internal use only. Overrides ConfigFactoryInterface::reset
ConfigFactory::setDomainConfigUiManager public function Set the Domain config UI manager.
ConfigFactory::__construct public function Constructs the Config factory. Overrides ConfigFactory::__construct