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

评论 (写第一个评论)