【问题标题】:Drupal 8/9 Override twig with custom module带有自定义模块的 Drupal 8/9 覆盖树枝
【发布时间】:2020-10-15 13:57:27
【问题描述】:
我已经尝试了所有发现的方法来修改views_view_field,从official docs 开始。我还为钩子 HOOK_theme(有和没有参数'path'、'base hook')和 HOOK_theme_registry_alter 尝试了多种方法和参数,但我仍然无法在我的模块中制作树枝来覆盖原始文件。
为了让事情更简单,我在没有任何自定义主题的情况下进行测试,在 /templates 下没有任何文件夹,并且我尝试修改的视图链接在管理页面内。树枝建议阐明了正在显示的树枝是“稳定”主题。
【问题讨论】:
标签:
php
drupal
twig
drupal-8
drupal-9
【解决方案1】:
主题中的模板优先于模块中的模板,因此您需要实现HOOK_theme_registry_alter 以强制 Drupal 从您的模块文件夹中获取模板。
/**
* Implements hook_theme_registry_alter().
*/
function mymodule_theme_registry_alter(&$theme_registry) {
// Replace the path to the registered template so that Drupal looks for
// it in your module's templates folder.
$theme_registry['views_view_field']['path'] = drupal_get_path('module', 'mymodule') . '/templates';
}
确保清除缓存以强制更新主题注册表。