如何在Drupal 8中覆盖system.mail.yml?

问题描述 投票:1回答:1

我在我的文件中有以下代码:core\modules\system\config\install\system.mail.yml

interface:
 default: 'php_mail'

我想将代码更改为:

interface:
 default: 'SMTPMailSystem'

为了让我的SMTP模块工作。在更改核心文件中的代码时,我的模块可以工作。由于直接更改核心文件并不好,我想知道如何覆盖这些文件。我对Drupal 8相当新,因此无法通过。

smtp drupal-8
1个回答
0
投票

Drupal有一篇关于Configuration override system的文章,它提供了一个概述和入门代码来覆盖* .yml中定义的配置。您可以立即跳转到“从模块提供覆盖”部分。

简而言之:

  1. 创建一个模块(config_example用作示例)
  2. 创建一个config_example.services.yml,并放入:
services:
  config_example.overrider:
    class: \Drupal\config_example\ConfigExampleOverrides
    tags:
      - {name: config.factory.override, priority: 5}

config.factory.override在这里很重要,其他人则由你来改变。

  1. 定义实现ConfigFactoryOverrideInterface的类:
namespace Drupal\config_example;

use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Config\ConfigFactoryOverrideInterface;
use Drupal\Core\Config\StorageInterface;

/**
 * Example configuration override.
 */
class ConfigExampleOverrides implements ConfigFactoryOverrideInterface {

  /**
   * {@inheritdoc}
   */
  public function loadOverrides($names) {
    $overrides = array();

    if (in_array('system.mail', $names)) { // (a)
      $overrides['system.mail'] = [
        'interface' => ['default' => 'SMTPMailSystem']
      ];
    }

    return $overrides;
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheSuffix() {
    return 'ConfigExampleOverrider'; // (c)
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheableMetadata($name) {
    return new CacheableMetadata();
  }

  /**
   * {@inheritdoc}
   */
  public function createConfigObject($name, $collection = StorageInterface::DEFAULT_COLLECTION) {
    return NULL;
  }

}

以下内容更改为适合您的情况:

(a)将in_array指针更改为system.mail,这是您要覆盖的YML。分配给$overrides['system.mail']的值将更改为您希望放置的值。

© www.soinside.com 2019 - 2024. All rights reserved.