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

评论 (写第一个评论)