要從配置對象中讀取數據,請使用\Drupal::config()->get('system.site')->get('name')
。
要將數據設置爲配置對象,首先需要加載可編輯配置實體,然後設置數據並最終保存它。
$site_settings = \Drupal::configFactory()->getEditable('system.site');
$site_settings->set('name', 'Foo site');
$site_settings->save();
保存自定義配置的數據庫,創建一個配置文件YAML:
模塊/ MODULE_NAME /配置/安裝/ custom.configuration.yml
custom-variable: 'hello world'
你也應該驗證您的配置的模式文件:
modules/MODULE_NAME/config/schema/cus tom.configuration.schema.yml
custom-variable:
type: 'string'
label: 'Custom variable'
description: 'A custom variable to store information in the database'
與一個變化很好地工作。 GET應該看起來像這樣:$ system = \ Drupal :: config('system.site');''$ value = $ system-> get('variable');' –
@WalterWhite get()用於imutable配置只要。 getEditable()用於可變配置。 – Christian