You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Drupal 9+安装配置文件:如何自定义安装阶段使用的主题?

在Drupal 9+安装阶段使用Seven/Claro子主题并自定义徽标与配色的实现方法

一、根据Drupal版本自动切换对应子主题

Drupal 9.5前后安装阶段默认主题不同,需通过安装配置文件的钩子判断版本,指定对应子主题作为安装主题:

  1. 在安装配置的.info.yml中声明依赖:
name: 'My Custom Profile'
type: profile
description: 'Custom installation profile with subtheme support.'
core_version_requirement: ^9 || ^10
dependencies:
  - drupal:seven
  - drupal:claro
  - my_custom_subtheme_seven
  - my_custom_subtheme_claro
  1. 在[profile_name].install文件中添加安装钩子:
/**
 * Implements hook_install().
 */
function my_profile_install() {
  $version_parts = explode('.', \Drupal::VERSION, 2);
  $major = $version_parts[0];
  $minor = $version_parts[1];

  // 按版本选择子主题并设为安装主题
  if ($major == 9 && $minor < 5) {
    \Drupal::service('theme_installer')->install(['my_custom_subtheme_seven']);
    \Drupal::configFactory()->getEditable('system.theme')->set('install', 'my_custom_subtheme_seven')->save();
  } else {
    \Drupal::service('theme_installer')->install(['my_custom_subtheme_claro']);
    \Drupal::configFactory()->getEditable('system.theme')->set('install', 'my_custom_subtheme_claro')->save();
  }
}

二、配置自定义徽标

静态配置(子主题.info.yml)

直接在子主题的.info.yml中指定徽标路径,以Seven子主题为例:

name: 'My Custom Seven Subtheme'
type: theme
description: 'Custom subtheme based on Seven.'
core_version_requirement: ^9
base theme: seven
logo: 'images/custom-logo.png' # 路径相对于子主题目录

Claro子主题只需将base theme改为claro,并替换对应徽标路径即可。

动态配置(安装阶段设置)

如果需要在安装时动态调整,可在hook_install中添加:

// 以Seven子主题为例
$config = \Drupal::configFactory()->getEditable('my_custom_subtheme_seven.settings');
$config->set('logo.path', 'profiles/my_profile/themes/my_custom_subtheme_seven/images/custom-logo.png')
       ->save();

三、配置自定义配色方案

方法1:子主题内置配色(推荐)

在子主题目录下创建color文件夹,添加color.inc定义配色方案:

<?php

$info = [
  'fields' => [
    'top' => t('Header top'),
    'bottom' => t('Header bottom'),
    'bg' => t('Background'),
  ],
  'schemes' => [
    'default' => [
      'title' => t('Custom Blue Scheme'),
      'colors' => [
        'top' => '#0071b3',
        'bottom' => '#005a87',
        'bg' => '#f6f6f6',
      ],
    ],
  ],
  'css' => [
    'css/colors.css',
  ],
];

然后在子主题css/colors.css中使用变量引用配色:

.header-top {
  background-color: {{ top }};
}
.header-bottom {
  background-color: {{ bottom }};
}
body {
  background-color: {{ bg }};
}

Claro子主题可参考官方Claro的color.inc结构调整配置。

方法2:安装时导入配色配置

提前导出子主题的配色设置(如my_custom_subtheme_seven.settings.yml),放在安装配置的config/install目录下,安装时自动导入:

logo:
  path: 'profiles/my_profile/themes/my_custom_subtheme_seven/images/custom-logo.png'
color:
  scheme: default
  colors:
    top: '#0071b3'
    bottom: '#005a87'
    bg: '#f6f6f6'

四、关键注意事项

  • 子主题需放在安装配置的themes目录或Drupal全局themes目录,确保安装配置能正确识别依赖。
  • Drupal 10沿用Claro作为默认安装主题,9.5+的逻辑可直接复用。
  • 需分别在Drupal 9.4及以下、9.5及以上版本测试安装流程,验证子主题加载和自定义配置生效。

内容的提问来源于stack exchange,提问作者GuyPaddock

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 03:18:37