DomainConfigOverride.php 4.3 KB

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