DomainConfigOverride.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace Drupal\domain_site_settings\Configuration;
  3. use Drupal\Core\Cache\CacheableMetadata;
  4. use Drupal\Core\Config\ConfigFactoryOverrideInterface;
  5. use Drupal\Core\Config\StorageInterface;
  6. use Drupal\domain\DomainNegotiatorInterface;
  7. use Drupal\Core\Config\ConfigFactoryInterface;
  8. /**
  9. * Class DomainConfigOverride.
  10. *
  11. * @package Drupal\domain_site_settings
  12. */
  13. class DomainConfigOverride implements ConfigFactoryOverrideInterface {
  14. /**
  15. * The Domain negotiator.
  16. *
  17. * @var \Drupal\domain\DomainNegotiatorInterface
  18. */
  19. protected $negotiator;
  20. /**
  21. * The config factory.
  22. *
  23. * @var \Drupal\Core\Config\ConfigFactoryInterface
  24. */
  25. protected $configFactory;
  26. /**
  27. * Constructs a DomainSourcePathProcessor object.
  28. *
  29. * @param \Drupal\domain\DomainNegotiatorInterface $negotiator
  30. * The domain negotiator.
  31. * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
  32. * The module handler service.
  33. */
  34. public function __construct(
  35. DomainNegotiatorInterface $negotiator,
  36. ConfigFactoryInterface $config_factory) {
  37. $this->negotiator = $negotiator;
  38. $this->configFactory = $config_factory;
  39. }
  40. /**
  41. * Returns config overrides.
  42. *
  43. * @param array $names
  44. * A list of configuration names that are being loaded.
  45. *
  46. * @return array
  47. * An array keyed by configuration name of override data. Override data
  48. * contains a nested array structure of overrides.
  49. */
  50. public function loadOverrides($names = []) {
  51. $overrides = [];
  52. if (in_array('system.site', $names)) {
  53. $domain = $this->negotiator->getActiveDomain();
  54. if (!empty($domain)) {
  55. $domain_key = $domain->id();
  56. $configFactory = $this->configFactory->get('domain_site_settings.domainconfigsettings');
  57. if ($configFactory->get($domain_key) !== NULL) {
  58. $site_name = $configFactory->get($domain_key . '.site_name');
  59. $site_slogan = $configFactory->get($domain_key . '.site_slogan');
  60. $site_mail = $configFactory->get($domain_key . '.site_mail');
  61. $site_403 = $configFactory->get($domain_key . '.site_403');
  62. $site_404 = $configFactory->get($domain_key . '.site_404');
  63. $site_front = $configFactory->get($domain_key . '.site_frontpage');
  64. $front = ($site_front !== \NULL) ? $site_front : '/node';
  65. // Create the new settings array to override the configuration.
  66. $overrides['system.site'] = [
  67. 'name' => $site_name,
  68. 'slogan' => $site_slogan,
  69. 'mail' => $site_mail,
  70. 'page' => [
  71. '403' => $site_403,
  72. '404' => $site_404,
  73. 'front' => $front,
  74. ],
  75. ];
  76. }
  77. }
  78. }
  79. return $overrides;
  80. }
  81. /**
  82. * The string to append to the configuration static cache name.
  83. *
  84. * @return string
  85. * A string to append to the configuration static cache name.
  86. */
  87. public function getCacheSuffix() {
  88. return 'MultiSiteConfigurationOverrider';
  89. }
  90. /**
  91. * Gets the cacheability metadata associated with the config factory override.
  92. *
  93. * @param string $name
  94. * The name of the configuration override to get metadata for.
  95. *
  96. * @return \Drupal\Core\Cache\CacheableMetadata
  97. * A cacheable metadata object.
  98. */
  99. public function getCacheableMetadata($name) {
  100. return new CacheableMetadata();
  101. }
  102. /**
  103. * Creates a configuration object for use during install and synchronization.
  104. *
  105. * If the overrider stores its overrides in configuration collections then
  106. * it can have its own implementation of
  107. * \Drupal\Core\Config\StorableConfigBase. Configuration overriders can link
  108. * themselves to a configuration collection by listening to the
  109. * \Drupal\Core\Config\ConfigEvents::COLLECTION_INFO event and adding the
  110. * collections they are responsible for. Doing this will allow installation
  111. * and synchronization to use the overrider's implementation of
  112. * StorableConfigBase.
  113. *
  114. * @see \Drupal\Core\Config\ConfigCollectionInfo
  115. * @see \Drupal\Core\Config\ConfigImporter::importConfig()
  116. * @see \Drupal\Core\Config\ConfigInstaller::createConfiguration()
  117. *
  118. * @param string $name
  119. * The configuration object name.
  120. * @param string $collection
  121. * The configuration collection.
  122. *
  123. * @return \Drupal\Core\Config\StorableConfigBase
  124. * The configuration object for the provided name and collection.
  125. */
  126. public function createConfigObject($name, $collection = StorageInterface::DEFAULT_COLLECTION) {
  127. return NULL;
  128. }
  129. }