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小节:

评论 (写第一个评论)