跳转到主要内容
你行网

Main navigation

  • 首页
  • 文章
  • 教程
  • 视频
  • 下载
  • 社区
  • 网址导航
User account menu
  • 登录

面包屑

  1. 首页

Drupal 11 主题制作教程

由 hrs, 13 八月, 2025

Drupal 11 主题制作是一个将设计与功能结合的过程,以下是一个全面的教程指南:

1. 准备工作
系统要求
确保已安装 Drupal 11

PHP 8.1 或更高版本

Composer (用于依赖管理)

创建主题目录
在 /themes/custom/ 下创建你的主题文件夹,例如 mytheme

2. 基本主题结构

mytheme/
├── mytheme.info.yml       # 主题元数据
├── mytheme.libraries.yml  # CSS/JS 资源
├── mytheme.theme          # PHP 逻辑
├── templates/            # Twig 模板
├── src/                  # PHP 类
│   └── Plugin/
│       └── Block/        # 自定义区块
├── assets/
│   ├── css/
│   ├── js/
│   └── images/
└── config/               # 配置架构
   └── install/


3. 创建主题信息文件 (mytheme.info.yml)


name: 'My Theme'
type: theme
description: 'Custom Drupal 11 theme'
core_version_requirement: ^11
base theme: olivero  # 或 classy/stark
libraries:
 - mytheme/global-styling
regions:
 header: 'Header'
 content: 'Content'
 sidebar: 'Sidebar'
 footer: 'Footer'


4. 添加 CSS 和 JavaScript (mytheme.libraries.yml)


global-styling:
 version: 1.0
 css:
   theme:
     assets/css/style.css: {}
 js:
   assets/js/script.js: {}
 dependencies:
   - core/jquery
   - core/drupal


5. 创建基础模板
page.html.twig (在 templates 目录中)


<!DOCTYPE html>
<html{{ html_attributes }}>
 <head>
   <head-placeholder token="{{ placeholder_token }}">
   <title>{{ head_title|safe_join(' | ') }}</title>
   <css-placeholder token="{{ placeholder_token }}">
   <js-placeholder token="{{ placeholder_token }}">
 </head>
 <body{{ attributes }}>
   {{ page_top }}
   {{ page }}
   {{ page_bottom }}
   <js-bottom-placeholder token="{{ placeholder_token }}">
 </body>
</html>


6. 主题开发最佳实践
使用 Twig 调试
在 sites/default/services.yml 中启用调试:


parameters:
 twig.config:
   debug: true
   auto_reload: true
   cache: false


子主题创建
如果你想基于现有主题:


name: 'My Subtheme'
type: theme
description: 'Subtheme of Olivero'
core_version_requirement: ^11
base theme: olivero


7. 响应式设计
在 mytheme.libraries.yml 中添加:


responsive:
 css:
   theme:
     assets/css/responsive.css:
       media: 'all and (max-width: 768px)'


8. 主题预处理函数 (mytheme.theme)



<?php
/**
* Implements hook_preprocess_page().
*/
function mytheme_preprocess_page(&$variables) {
 // 添加自定义变量到模板
 $variables['custom_variable'] = 'Hello Drupal 11!';
}


9. 编译前端资源 (可选)
如果你使用 Sass/LESS 等预处理器:

安装 Node.js 和 npm/yarn

创建 package.json 文件

设置构建脚本

10. 启用主题
访问 /admin/appearance

找到你的主题并点击"安装并设为默认"

高级主题功能
创建自定义区块
在 src/Plugin/Block/ 下创建 PHP 类

主题设置表单
实现 hook_form_system_theme_settings_alter()

使用布局构建器
创建自定义布局模板和样式

调试工具
使用 Devel 模块

Twig 调试工具 ({{ dump(variable) }})

Chrome PHP 调试器

性能优化
聚合 CSS/JS

使用缓存

优化图片资源

延迟加载非关键资源

这个教程涵盖了 Drupal 11 主题制作的基础知识。随着你的需求增长,可以探索更高级的主题技术如组件化主题、使用 Storybook 等前端工具集成。

标签

  • drupal 11
  • 登录或注册以发表评论

评论

相关文章

Drupal 11.1:告别 Body 字段,迎接全新的 Hook 系统
Drupal 11:拥抱未来,构建更强大的数字体验

文章标签

CSS
Excel
ubuntu
php
五笔
vim
Linux
VPN
command
HTML
drupal 7
centos
composer
Mysql
drupal 8
模块(module)
drush
JavaSrcipt
apache
drupal 9
英语
Drupal
nerdtree
SEO
network
果树种植
surround
drupal10
主题(theme)
phpmyadmin
ssh
二进制
yum
权限
声音
ssl
selinux
HTML5
用户
password
Dreamweaver
flameshot
颜色
翻译
uploadprogress
DDEV
kernel
视图(views)
firewalld
WireGuard
快捷键
gzip
xdebug
错误
邮件验证
ckeditor
电脑基础
简介
mbstring
源地址
C语言
certbot
php扩展
git
apt
电子秤
httpd
压缩解压
表单
breadcrumb
AI
短信验证
superfish
摄影
重定向
diff
网站备份
csharp
tagclouds
java
docker
adsense
版本号
电脑技巧
RSS
simple_adsense
算法
端口
drupal 11
youtube
JavaScript
cookie
badblocks
VUE
计算机基础
网站运营
RFID
gimp
nginx
form
zip
mod_expires 模块
http验证
ip
dns
标签优化
curl
赚钱
重定向,301
netstat
maven
input
字段
npm
哈希值
wps
更多
RSS源

友情链接 网站地图 版权信息 联系我们 & 社交媒体链接

Copyright © 2019 - 2021 你行网 版权所有  粤ICP备19072650号