9.3 覆写系统配置

1龵龳龰Global

Drupal8龙龖龕龑$config龅龁龀齿齻齸齴齲齰齮齭齫

齪齨齪齥Drupal\Core\Config\ConfigFactory::get()龅龁API齴齃齁齮齭鼾鼺齫

鼷鼵鼴鼰鼮齃齁鼪鼩鼦鼢鼾message:

$message = \Drupal::config('system.maintenance')->get('message');

龙龖鼉setting.php鼄鼀黿齴龳龰龅龁齻齸黻

$config['system.maintenance']['message'] = 'Sorry, our site is down now.';

鼉黧黥-> css黡黠龅點鼴黛黙黗龖黔Drupal8黓黏黋黈黆鼾鼴黂黁麽麹鼵麸黻

$config['system.performance']['css']['preprocess'] = 0;

麰黔齪齥drush麭麫麧麦黓麣龳龰麠麝齫

drush config-list
drush config-get system.performance

2龵齃齁麘鼺

齮齭麗龳龰麔龖黔鼴麒麎齃齁麘鼺麌麈龖麸黓麇麅麽麹黻

// Get the site name, with overrides.
$site_name = \Drupal::config('system.site')->get('name');

// Get the site name without overrides.
$site_name = \Drupal::config('system.site')->getOriginal('name', FALSE);

// Note that mutable config is always override free.
$site_name = \Drupal::configFactory()->getEditable('system.site')->get('name');

3龵龳龰鹨鹤齮齭

鹡鹨鹤鼩鹞鼄鼴鹜鹛龑鹗鹔鹐鹌鹋鼴龅鹇鹅鹌鹋鹨鹤齁鹂鹀龑鹗鼾鹨鹤齮齭齫麽麹鼵麸黻

// Load the language_manager service
$language_manager = \Drupal::service('language_manager');

// Get the target language object
$langcode = $account->getPreferredLangcode();
$language = $language_manager->getLanguage($langcode);

// Remember original language before this operation.
$original_language = $language_manager->getConfigOverrideLanguage();
// Set the translation target language on the configuration factory.
$language_manager->setConfigOverrideLanguage($language);

$mail_config  = \Drupal::config('user.mail');

// Now send email based on $mail_config which is in the proper language.

// Set the configuration language back.
$language_manager->setConfigOverrideLanguage($original_language);

龅鸳麽麹鸰鹜黓鸬user.mail龅龁齮齭鸫鸩鸧language.config.$langcode.user.mail鼴鸰鹜鹔鹐鹌鹋鼾鹇鹅鸤鸠鸝鸚鸙龑鹗鸕齭鼾鹨鹤鼴鸒鸏鸙鸋鸉龑鹗鹨鹤鸅鸂黡鼾鹌鹋鸁鷿齫

4龵鸁鷺鼄龳龰齮齭

ConfigFactory鸠鷹鷸鷶鷲鸁鷺鼴黠鸒鼄鸙鸋鷯鷭麔config.factory.override鼾service齫 鼵鼉hello_world.services.yml鼄鷬鷩鼵麸黻

services:
  hello_world.config_example.overrider:
    class: \Drupal\hello_world\ConfigExampleOverrides
    tags:
      - {name: config.factory.override, priority: 5}

鼉src/ConfigExampleOverrides.php鷚龰鼵麸麽麹黻

namespace Drupal\hello_world;

use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Config\ConfigFactoryOverrideInterface;
use Drupal\Core\Config\StorageInterface;

/**
 * Example configuration override.
 */
class ConfigExampleOverrides implements ConfigFactoryOverrideInterface {

  public function loadOverrides($names) {
    $overrides = array();
    if (in_array('system.site', $names)) {
      $overrides['system.site'] = ['name' => 'Overridden site name!'];
    }
    return $overrides;
  }

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

  /**
   * {@inheritdoc}
   */
  public function getCacheableMetadata($name) {
    return new CacheableMetadata();
  }

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

}

鷇鷆麽麹鷯鷭麔ConfigFactoryOverrideInterface鼾鷄龁鷃鷂黻

  • loadOverrides: 鶿鶻齮齭龳龰鶹鶸
  • getCacheSuffix: 鶿鶻cache鼾鶶鶵鶲鶰
  • getCacheableMetadata: 鶿鶻麗龳龰黔鼾cache鶹鶮
  • createConfigObject: 黛黙鶭鶩鶥鼾鹇鹅鶢鶠鶝龁config鶚鶗鼴龅點黓龳龰鼴鷶鷲鶿鶻NULL

本书共79小节:

评论 (写第一个评论)