logo

Palette - Make it Colorful🎨

Palette - Visual Page Builder, no design degree required.

Live Demo Download Palette

滚动
04/10/2025, by Ivan

在我们的模块中添加一个 YAML 配置文件后,Drupal 会自动加载该 YAML 文件的内容,我们就可以访问它来提供默认配置。在模块的根目录下创建一个新文件夹并命名为「config」。在该文件夹内再创建一个名为「install」的子文件夹。最后,在 config/install 内创建一个新文件,并命名为 hello_world.settings.yml。

hello:
  name: 'Hank Williams'

请记住,YAML 对空格非常敏感。然而,为了在 Drupal 对象中使用已加载的值,我们需要在 HelloBlock 类中添加如下方法(参见 自定义区块教程):

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    $default_config = \Drupal::config('hello_world.settings');
    return [
      'hello_block_name' => $default_config->get('hello.name'),
    ];
  }

该值会在安装模块时使用。因此,为了测试,请卸载并重新安装你的模块。当你再次将区块添加到某个区域时,你应该会看到默认值。

在这里查看更多关于简单配置(\Drupal::config)的信息。