主题化节点表单时建议不同的模板 [英] Suggesting different templates when theming a node form
问题描述
function posts_theme($existing, $type, $theme, $path) {
return array(
'post_node_form' => array(
'arguments' => array('form' => NULL),
'template' => VARIABLE,
)
);
}
这是建议模板在Drupal中呈现 post_node_form的方式6.但我想从2个不同的路径获取节点编辑表单:
This is the way of suggesting a template to render the 'post_node_form' in Drupal 6. BUT I want to get the node editing form from 2 different paths:
- 通过AJAX通过drupal_get_form('post_node_form')
- 通过默认节点/添加/发布
如果我根据路径替换 VARIABLE(或不管其他条件如何),它似乎不会起作用?模板的名称已缓存,您需要刷新缓存以刷新它。
If I replace "VARIABLE" depending on the path (or whatever other condition), it will not work because it seems? the name of the template is cached and you need to flush caches to refresh it.
任何建议使用不同表单模板的解决方案吗?
Any solution of suggesting different form templates?
注意。节点模板不是这种情况(然后,您可以将模板建议放在预处理挂钩中)。
NOTE. This is not the case of node template, (then you can put the template suggestions in the preprocess hooks). It's about node FORM.
推荐答案
好,我回答了我自己的问题:
Ok, I answer my own question:
解决方案的关键是挂钩 preprocess_NAME_OF_MY_FORM
,该挂钩在每次页面加载时执行,可以在您的模块或主题中使用。
The key of the solution is the hook preprocess_NAME_OF_MY_FORM
, that is executed every page load and can be in your module or your theme.
因此,在我的情况下,我在帖子模块中写道:
So in my case, I wrote in my "posts" module:
/**
* Implementation of hook_theme().
*/
function posts_theme($existing, $type, $theme, $path) {
return array(
'post_node_form' => array(
'arguments' => array('form' => NULL),
'template' => 'post-form-custom',
)
);
}
function posts_preprocess_post_node_form(&$vars) {
// I check the path to know if node_form is retrieve through normal way or ajax way.
if (check_plain(arg(0)) == 'node'){
$vars['template_files'][] = 'post-form-default';
}
}
我的模块文件夹中有文件 post-form-custom.tpl.php
和 post-form-default.tpl.php
I had in my module folder the files post-form-custom.tpl.php
and post-form-default.tpl.php
这篇关于主题化节点表单时建议不同的模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!