来自模块的 drupal 表单布局
我创建了自己的模块,名为“cssswitch”。我正在尝试创建自己的 html 布局来显示模块的管理表单部分。我了解如何使用 hook_form_alter() 修改表单元素,但我需要创建整个表单布局以使某些字段彼此相邻出现。 看来我需要类似使用 theme_cssswitch_display() 显示节点前端视图的方式,但对于节点的管理部分。
function cssswitch_form_alter(&$form, $form_state, $form_id) {
switch($form_id) {
case 'cssswitch_node_form':
$form['hour_start']['#prefix'] = '<div class="start-box">';
$form['ampm_start']['#suffix'] = '</div>';
$form['ampm_start']['#attributes'] = array("style" => "border-width:2px;");
break;
}
}
function cssswitch_theme() {
return array(
'cssswitch_display' => array(
'arguments' => array('node'),
),
);
}
// to display the view of the node
function theme_cssswitch_display($node) {
$output = '<div class="cssswitch-cssfilename">Filename: '.
check_plain($node->cssfilename). '</div>';
$output .= '<div class="cssswitch-time_start">Start: '.
check_plain($node->hour_start). ':'.check_plain($node->minute_start).' '.check_plain($node->ampm_start).'</div>';
$output .= '<div class="cssswitch-time_end">End: '.
check_plain($node->hour_end). ':'.check_plain($node->minute_end).' '.check_plain($node->ampm_end).'</div>';
return $output;
}
感谢您的帮助
I created my own module called "cssswitch". I'm trying to create my own html layout to display the admin form portion of the module. I understand how to use the hook_form_alter() to modify form elements, but I need to create the entire form layout to make some fields appear next to each other.
It seems I need something like the way I have the frontend view of the node displayed with theme_cssswitch_display(), but for the admin part of the node.
function cssswitch_form_alter(&$form, $form_state, $form_id) {
switch($form_id) {
case 'cssswitch_node_form':
$form['hour_start']['#prefix'] = '<div class="start-box">';
$form['ampm_start']['#suffix'] = '</div>';
$form['ampm_start']['#attributes'] = array("style" => "border-width:2px;");
break;
}
}
function cssswitch_theme() {
return array(
'cssswitch_display' => array(
'arguments' => array('node'),
),
);
}
// to display the view of the node
function theme_cssswitch_display($node) {
$output = '<div class="cssswitch-cssfilename">Filename: '.
check_plain($node->cssfilename). '</div>';
$output .= '<div class="cssswitch-time_start">Start: '.
check_plain($node->hour_start). ':'.check_plain($node->minute_start).' '.check_plain($node->ampm_start).'</div>';
$output .= '<div class="cssswitch-time_end">End: '.
check_plain($node->hour_end). ':'.check_plain($node->minute_end).' '.check_plain($node->ampm_end).'</div>';
return $output;
}
thanks for any help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我找到了我现在需要的大部分东西。我必须使用“Themer Info”给我的“建议”表单 ID,即“cssswitch_node_form”。我在名为“cssswitch_form()”的表单构建函数中使用了它,如下所示:
$form['#theme'] = 'cssswitch_node_form';
我还需要在此函数中使用该名称:
这是同名但前面带有“theme_”的函数:
I仍然存在 html 代码自动用 div 围绕表单元素的问题,这不是我想要的。在本例中,我想并排放置一些表单元素。我想这就是函数 drupal_render() 的作用。是否有一个函数调用只会给我表单元素而不包含所有 html 标记?
谢谢
I found most of what I need now. I had to use the "suggested" form id that Themer Info" gave me which was "cssswitch_node_form". I used that in my form builing function called "cssswitch_form() like this:
$form['#theme'] = 'cssswitch_node_form';
I also need to use that name in this function:
And here is the function of the same name but with "theme_" prepended to it:
I still have the problem of html code surrounding the form elements with divs automatically which is not what I wanted. In this instance, I want to place some form elements side by side. I guess that's what the function drupal_render() does. Is there a function call that will just give me the form element without all the html markup?
thanks
我现在已经把一切都解决了。
我的hook_theme是这样的:
我创建了文件themes/garland/cssswitch-node-form.tpl.php
然后我可以控制在哪里放置任何表单元素,如下所示:
I got it all sorted out now.
My hook_theme is this:
I created the file themes/garland/cssswitch-node-form.tpl.php
Then I can have control over where to put any form elements like this:
仅当您想更改另一个模块已定义的表单时才需要
hook_form_alter
。由于您正在定义表单,因此不需要更改它,而是可以按照您想要的方式定义它。要更改表单的标记,您需要使用表单本身和/或其元素的
#theme
属性。有了它,您可以使用自己的主题函数来呈现表单及其元素。请在 Drupal 的 FAPI 参考中寻求帮助。
hook_form_alter
is only needed, if you want to change a form that another module has defined. Since you are defining the form, you don't need to alter it, instead you can just define it like you want.What you need to use, to change the markup of the form, is the
#theme
attribute of the form itself and/or it's elements. With it you can use your own theme functions to render the form and it's elements.Look for help in Drupal's FAPI reference.