9.6 配置API

1龵龴龲龯龫

// Immutable Config (Read Only)
$config = \Drupal::config('system.performance');
// Mutable Config (Read / Write)
$config = \Drupal::service('config.factory')->getEditable('system.performance');

齊齆齃齀龴鼼齊鼹齃鼵鼳鼱龴鼮鼭

龯龫龴龲齆鼫鼩鼦get鼵鼳鼭鼤鼠

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

鼞鼱鼚鼖鼕鼓鼒鼮鼳鼠

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

龴龲齆齃鼑鼎龯龫鼼鼩鼦.鼊鼇鼆鼅鼁鼭鼤鼠

$enabled = \Drupal::config('system.performance')->get('cache.page.enabled');

2龵鼮黹龯龫

黵黳鼩鼦getEditable黰齃鼵鼳黬\Drupal\Core\Config\Config黫黧龲鼭 鼤鼠

\Drupal::service('config.factory')->getEditable('system.performance');

黥鼮黢齃龯龫

齆鼫鼩鼦set黡save鼵鼳鼁黟黜龯龫鼭鼤鼠

$config = \Drupal::service('config.factory')->getEditable('system.performance');

// Set a scalar value.
$config->set('cache.page.enabled', 1);

// Set an array of values.
$page_cache_data = array('enabled' => 1, 'max_age' => 5);
$config->set('cache.page', $page_cache_data);

// Save your data to the file system.
$config->save();

鼞鼱鼚鼮黎鼓鼒鼠

\Drupal::service('config.factory')->getEditable('system.performance')->set('cache.page.enabled', 1)->save();

黥鼮黍黉龯龫

鼤黇黆黥黂黍黉黁龯龫麾麽鼼鼱鼚鼩鼦setData鼵鼳鼼麼麺鼤麶:

// Set all values.
\Drupal::service('config.factory')->getEditable('system.performance')->setData(array(
    'cache' => array(
      'page' => array(
        'enabled' => '0',
        'max_age' => '0',
      ),
    ),
    'preprocess' => array(
      'css' => '0',
      'js' => '0',
    ),
    'response' => array(
      'gzip' => '0',
    ),
  ))
  ->save();

麫麨鼠setData麤麣麡鼖黟龯龫麾麽麟麞鼼麜麘黜鼖黁麗麔麑麏麌麋麉鼭

3龵麇麅龯龫

麇麅齆齃黢麄黁龯龫

麼麺鼤麶鼠

$config = \Drupal::service('config.factory')->getEditable('system.performance');
$config->clear('cache.page.max_age')->save();
$page_cache_data = $config->get('cache.page');

黰麗麔麁鹾$page_cache_data鼼麤鹼鹹max_age鹶鹴鹲麇麅鹱鼭

麇麅黍黉龯龫

黵黳鼩鼦delete鼵鼳鼼麼麺鼤麶鼠

\Drupal::service('config.factory')->getEditable('system.performance')->delete();

4龵鹪鹧鹥鹣

鼤黇黆鹡齆齃龯龫鹠鹞鹛鹚齃鹖鹓鼼鹑鹐鼱鹍黆鼮鹚齃save鼼鼤麶鼠

\Drupal::service('config.factory')->getEditable('foo.bar')->set('foo', 'foo')->save();
\Drupal::service('config.factory')->getEditable('foo.bar')->set('bar', 'bar')->save();

黰鹊麼麺鹇鹅鹂鹀黁鼼麏鸾鸺黆鼁鼮鼠

$config = \Drupal::service('config.factory')->getEditable('foo.bar');
$config
  ->set('foo', 'foo')
  ->set('bar', 'bar')
  ->save();

本书共79小节:

评论 (写第一个评论)